I appreciate you all let me know how to do it. I tried to 3 different way into 1 code, which are value, pointer and reference. Probably still I don't understand them and it's need to be correction in my code, however the compile is pass and I can see what I want to see in the console, so I am grad to it!
// g++ -o rps.exe rps.cpp
#include <iostream>
#include <random>
int random(int const low, int const high)
{
static std::mt19937 mt{ std::random_device{}() };
static std::uniform_int_distribution<int> random_number;
using param_type = std::uniform_int_distribution<int>::param_type;
return random_number(mt, param_type{ low, high });
}
class User
{
public :
User(){}; //constructor
int hand;
void setHand()
{
std::cout << "What you choose? (ROCK = 1, SCISSORS = 2, PAPER = 3) := ";
std::cin>> hand;
}
};
class Computer
{
public :
Computer(){}; //constructor
int hand;
void setHand()
{
hand = random(1, 3);
std::cout << "Computer choose := " << hand << std::endl;
}
};
class Judge_R //By Reference
{
public :
User& user_R;
Computer& computer_R;
Judge_R(User& user_R, Computer& computer_R) : user_R(user_R), computer_R(computer_R) {};
void doJudge()
{
std::cout << "User CHOSE hand in class Judge_R := " << user_R.hand << std::endl;
std::cout << "Computer CHOSE hand in class Judge_R := " << computer_R.hand << std::endl;
}
};
class Judge_P //By Pointer
{
public :
User* user_P;
Computer* computer_P;
Judge_P(User* user_P, Computer* computer_P) : user_P(user_P), computer_P(computer_P) {};
void doJudge()
{
std::cout << "User CHOSE hand in class Judge_P := " << user_P->hand << std::endl;
std::cout << "Computer CHOSE hand in class Judge_P := " << computer_P->hand << std::endl;
}
};
class Judge_V //By Value
{
public :
void doJudge(int const user_hand, int const computer_hand)
{
std::cout << "User CHOSE hand in class Judge_V := " << user_hand << std::endl;
std::cout << "Computer CHOSE hand in class Judge_V := " << computer_hand << std::endl;
}
};
int main()
{
User user;
user.setHand();
//std::cout << " user.hand in main() := " << user.hand << std::endl;
Computer computer;
computer.setHand();
//std::cout << " computer.hand in main() := " << computer.hand << std::endl;
//By Reference
Judge_R judge_R(user, computer);
judge_R.doJudge();
//By Pointer
Judge_P judge_P(&user, &computer);
judge_P.doJudge();
//By value
Judge_V judge_V;
judge_V.doJudge(user.hand, computer.hand);
}
C:\RPS>g++ -o rps.exe rps.cpp
C:\RPS>rps.exe
What you choose? (ROCK = 1, SCISSORS = 2, PAPER = 3) := 1
Computer choose := 2
User CHOSE hand in class Judge_R := 1
Computer CHOSE hand in class Judge_R := 2
User CHOSE hand in class Judge_P := 1
Computer CHOSE hand in class Judge_P := 2
User CHOSE hand in class Judge_V := 1
Computer CHOSE hand in class Judge_V := 2