0

This is the first piece of code I wrote on my own. I have a program that uses 2 different functions to compute who would win in a game of Rock Paper Scissors but it is giving me the same output everytime.

I think there is a problem with the random function as it not generating the random numbers but I am unable to find out the problem.

Any help would be greatly appreciated.

struct RPS //Rock Paper Scissors {
    unsigned seed = time(0);
    int RPS1 = rand() % 3; // Random Number Generator for Player 1
    int RPS2 = rand() % 3; // Random Number Generator for Player 2
};

int Player_1(int Rock = 0, int Paper = 1, int Scissors = 2) {
    RPS get; // Calls the number from the "struck RPS" data structure
    if (get.RPS1 == 0) {
        cout << "Player 1 selected Rock" << endl;
        return Rock;
        }
    else if (get.RPS1 == 1){
        cout << "Player 1 selected Paper" << endl;
        return Paper;
    }
    else{
        cout << "Player 1 selected Scissors" << endl;
        return Scissors;
    }
}

int Player_2(int Rock = 0, int Paper = 1, int Scissors = 2) {
    RPS get; // Calls the number from the "struck RPS" data structure
    if (get.RPS2 == 0) {
        cout << "Player 2 selected Rock" << endl;
        return Rock;
    }
    else if (get.RPS2 == 1) {
        cout << "Player 2 selected Paper" << endl;
        return Paper;
    }
    else {
        cout << "Player 2 selected Scissors" << endl;
        return Scissors;
    }
}

int Who_Won(int tie = 0, int Player_1_Won = 1, int Player_2_Won = 2) {
    RPS get; // Calls the number from the "struck RPS" data structure

    if ((get.RPS1 == 0 && get.RPS2 == 2)
        || (get.RPS1 == 1 && get.RPS2 == 0)
        || (get.RPS1 == 2 && get.RPS2 == 1)) {
        cout << "Player 1 Won" << endl;
        return Player_1_Won;
    }

    else if ((get.RPS2 == 0 && get.RPS1 == 2)
        || (get.RPS2 == 1 && get.RPS1 == 0)
        || (get.RPS2 == 2 && get.RPS1 == 1)) {
        cout << "Player 2 Won" << endl;
        return Player_2_Won;
    }

    else {
        cout << "It's a tie" << endl;
        return tie;
    }
}

int main() {
    RPS get; // Calls the number from the "struck RPS" data structure

    clock_t begin, end;
    double elapsed_seconds;

    // This clock runs the time for player 1.
    begin = clock();
    Player_1(0, 1, 2);
    end = clock();
    cout << endl;
    elapsed_seconds = double(end - begin) / CLOCKS_PER_SEC;
    cout << "The time it took to run the player_1 function was: " << elapsed_seconds << endl << endl;

    cout << "************************************************************" << endl << endl;

    // This clock runs the time for player 2.
    begin = clock();
    Player_2(0, 1, 2);
    end = clock();
    cout << endl;
    elapsed_seconds = double(end - begin) / CLOCKS_PER_SEC;
    cout << "The time it took to run the player_2 function was: " << elapsed_seconds << endl << endl;

    cout << "************************************************************" << endl << endl;

    // Here, we calculate who won
    Who_Won(0, 1, 2);
    cout << endl;

    system("pause");
    return 0;
}
hashing
  • 3
  • 1
  • You never call `srand()` to initialize the random number generator. – πάντα ῥεῖ Apr 27 '19 at 19:51
  • Just as a code structuring point, you have in every block of your code, a new instance of your RPS data structure, which I would think would not be consistent with what you want. I would think you would want the same RPS get being passed around to every function call versus a different instance due to you declaring a new RPS instance in every function block and in main() itself. – CodeoftheWarrior Apr 27 '19 at 20:45

1 Answers1

0

You need to specify a seed with srand(). Based on your code, you'd do so using srand(time(nullptr));

Pierce Griffiths
  • 733
  • 3
  • 15