0

I've been asked to create a Rock, Paper, scissors game between a human player and a computer player using classes in C++.

Everything is pretty much straight forward other than that they want the human player to have a strategy and it will loop through that strategy to play the game. The user can input how long the strategy will be and then will input what those strategies will be as well after. So for example, if the user inputs 4 R P S S, that means the human strategy is to play Rock then Paper then Scissors then Scissors and then loop back to the start if asked to play more. The computer in this case will always play Rock. And there is a Referee class which will determine the winner here. The output in this case is suppose to look like 4 T L W W. T = tie , L = lose , W = win

Making the .h and .cpp file for the Computer is straight forward as it will always play Rock.

My Problem is that the StrategyCounter gets stuck on 0 and the StrategyCounter++; in the Human::getMove() doesn't increment it.

Keep in mind I can't change the parameters of the functions.

Here is the main file:

int main()
{   
    Referee ref;
    HumanPlayer human;
    ComputerPlayer computer;

    for (int i=0 ; i<5; i++){
        cout<< ref.refGame(human, computer)<< " ";
    }
}

Referee.cpp

Referee::Referee(){
}

char Referee::refGame(HumanPlayer human,ComputerPlayer computer){

    char s = human.makeMove();
    if (s == 'R'){
        score = 'T';}
    else if (s == 'P'){
        score = 'W';}
    else if (s == 'S'){
        score = 'L';
    }
    return score;
}

Human.cpp

 HumanPlayer::HumanPlayer(){
    int r;
    cin>>r;
    k = r;
    cout<< k << " ";
    StrategyCounter = 0;

    for (int i=0; i<k; i++){
        cout << "What the next move?: ";
        cin>>Strategy[i];
    }

}


char HumanPlayer::makeMove(){
    // he should have the startegy here to get results
    if (StrategyCounter > k){
        StrategyCounter = 0;
    }
    cout<<StrategyCounter<<endl;

    char HumanMove_p;
    HumanMove_p = Strategy[StrategyCounter];

    StrategyCounter++;

    HumanMove = HumanMove_p;
    return HumanMove;
}

Human.h

class HumanPlayer
{
    public:
    HumanPlayer();
    int k;
    int StrategyCounter;
    char *Strategy = new char[k];
    char HumanMove;
    char makeMove();
};
lopho
  • 177
  • 10
  • 1
    Constraint "I can't change the parameters of the functions" doesn't make sense. Fixing your code will probably require such a change: you will need [pass-by-reference](https://stackoverflow.com/q/2139224/509868) – anatolyg Aug 08 '20 at 06:46

1 Answers1

0

As anatolyg pointed out, the solution could require passing human by reference. StrategyCounter is stuck in zero because in each iteration you pass-by-value and make a local copy of human for the function Referee::refGame.

Consider modifying the main for loop and adding human.StrategyCounter++;

Cristian
  • 13
  • 4