Rock Paper Scissors Lizard Spock

I have most of this figured out but I am coming across one problem.

When the user either wins or loses on the first try the game functions as its supposed to.

If the user ties then it will go into an infinite loop no matter what choice the user enters it will continue to output a tie and ask the user for another choice.

Also The first time through it will state what the user and the cpu picked and then announce the results. During the loop it will only say its a tie and not list what the choices were that were chosen.

Thank You

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
 #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int ROCK      =1;
const int PAPER     =2;
const int SCISSORS  =3;
const int LIZARD    =4;
const int SPOCK     =5;
int     getUserChoice();
int     getComputerChoice();
void    determineWinner(int, int);
void    displayChoice(int);


int main()
{
    int userchoice;
    int computerchoice;
    
    
    userchoice = getUserChoice();
    computerchoice = getComputerChoice();
  
    
    determineWinner(userchoice, computerchoice);
    computerchoice = getComputerChoice();
    userchoice = getUserChoice();
        
    
       return 0;
}

int getComputerChoice ()
{
    int computerChoice;
    
    
    
    computerChoice = rand() % 5+1;
    
    return computerChoice;
    
}

int getUserChoice()
{
    int userChoice;
    
    cout << "Welcome to the Rock, Paper, Scissors, Lizard, Spock game\n\n";
    cout << "1) Rock\n";
    cout << "2) Paper\n";
    cout << "3) Scissors\n";
	cout << "4) Lizard\n";
	cout << "5) Spock\n";
    cout << "Plese enter a choice between 1-5\n";
    cin >> userChoice;
    
    while (userChoice < 1 || userChoice > 5)
    {
        cout << "Enter a number from 1-5 ";
        cin >> userChoice;
    }
    
    return userChoice;
    
}

void determineWinner(int user, int computer)
{
    
    cout << "You picked: ";
    displayChoice(user);
    cout << "The CPU picked ";
    displayChoice(computer);
    
    while (user == computer)
    
    {
        cout << "You have tied, Please try again\n";
        getUserChoice();
    }
    
    if (user == ROCK)
    {
        if (computer == SCISSORS || computer == LIZARD)
        {
            cout << "You Win!!\n";
        }
        else if (computer == PAPER || computer == SPOCK)
            cout << "You Lose :(\n";
        

    }
    
    if (user == PAPER)
    {
        if (computer == ROCK || computer == SPOCK)
        {
            cout << "You Win!!\n";
        }
        else if (computer == LIZARD || computer == SCISSORS)
            cout << "You Lose :(";
    }
    
    if (user == SCISSORS)
    {
        if (computer == PAPER || computer == LIZARD)
        {
            cout << "You Win!!\n";
        }
            else if (computer == ROCK || computer == SPOCK)
                cout << "You Lose :(";
        
    }
    if (user == LIZARD) {
        if (computer == SPOCK || computer == PAPER) {
            cout << "You Win!!\n";
        }
        else if (computer == SCISSORS || computer == ROCK)
            cout << "You Lose :(";
    }
    if (user == SPOCK) {
        if (computer == SCISSORS || computer == ROCK)
        {
            cout << "You Win!!\n";
        }
        else if (computer == LIZARD || computer == PAPER)
            cout << "You Lose :(";
    }
    
    

    
    exit(0);
}

void displayChoice(int choice)
{
    if (choice == ROCK)
        cout << "Rock\n";
    else if (choice == PAPER)
        cout << "Paper\n";
    else if (choice == SCISSORS)
        cout << "Scissors\n";
	else if (choice == LIZARD)
        cout << "Lizard\n";
    else if (choice == SPOCK)
        cout << "Spock\n";
    
}
Of course. How does the code on lines 81-82 affect the condition on line 78?


PS. Please, explain line 136.
The way I thought it works is that line 78 starts a loop only when the user choice is equal to the computer choice. So if the user chooses rock and the computer chooses rock it will call the getUserchoice function and starts the game over so that when the user chooses again it will compare it to the computer choice and either go to the tie loop or one of the appropriate if statements.

Line 136 was how I was ending the program after a win or a loss. I don't know how to get the code to go back to the main function return 0; function.

Also I don't have the random seed started in this code so the computer picks scissors every time. I did this on purpose so I could test the code and will put the random line of code in later when I get everything else working.

Thanks
You do call the getUserChoice() on line 23 too. Does that restart the game or what does happen there?

Similarly, the call to getUserChoice() on line 82 does return to the caller, and as next action the condition on line 78 is re-evaluated. However, the 'user' has not changed and the 'computer has not changed, so the result is again and forever true.


It is possible to call return; on a function that "returns" a void.
I changed some things around and the game is working correctly now. I'm sure there is a better way to do it, but how does this look?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int ROCK      =1;
const int PAPER     =2;
const int SCISSORS  =3;
const int LIZARD    =4;
const int SPOCK     =5;
int     getUserChoice();
int     getComputerChoice();
void    determineWinner(int, int);
void    displayChoice(int);
int     userchoice;
int     computerchoice;

int main()
{
    
    
    userchoice = getUserChoice();
    computerchoice = getComputerChoice();
    determineWinner(userchoice, computerchoice);
   
        
    
       return 0;
}

int getComputerChoice ()
{
    int computerChoice;
    
    srand(time(NULL));
    
    computerChoice = rand() % 5+1;
    
    return computerChoice;
    
}

int getUserChoice()
{
    int userChoice;
    
    cout << "Welcome to the Rock, Paper, Scissors, Lizard, Spock game\n\n";
    cout << "1) Rock\n";
    cout << "2) Paper\n";
    cout << "3) Scissors\n";
	cout << "4) Lizard\n";
	cout << "5) Spock\n";
    cout << "Plese enter a choice between 1-5\n";
    cin >> userChoice;
    
    while (userChoice < 1 || userChoice > 5)
    {
        cout << "Enter a number from 1-5 ";
        cin >> userChoice;
    }
    
    return userChoice;
    
}

void determineWinner(int user, int computer)
{
    
    cout << "You picked: ";
    displayChoice(user);
    cout << "The CPU picked: ";
    displayChoice(computer);
    
    if (user == computer)
    {
        cout << "It's a tie, try again\n";
        main();
    }

    
    else if (user == ROCK && computer != ROCK)
    {
        if (computer == SCISSORS || computer == LIZARD)
        {
            cout << "You Win!!\n";
        }
        else if (computer == PAPER || computer == SPOCK)
            cout << "You Lose :(\n";
        return;

    }
    
    else if (user == PAPER && computer != PAPER)
    {
        if (computer == ROCK || computer == SPOCK)
        {
            cout << "You Win!!\n";
        }
        else if (computer == LIZARD || computer == SCISSORS)
            cout << "You Lose :(";
        return;
    }
    
    else if (user == SCISSORS && computer != SCISSORS)
    {
        if (computer == PAPER || computer == LIZARD)
        {
            cout << "You Win!!\n";
        }
            else if (computer == ROCK || computer == SPOCK)
                cout << "You Lose :(";
        return;
    }
    else if (user == LIZARD && computer != LIZARD)
    {
        if (computer == SPOCK || computer == PAPER)
        {
            cout << "You Win!!\n";
            return;
        }
        else if (computer == SCISSORS || computer == ROCK)
            cout << "You Lose :(";
        return;
    }
    else if (user == SPOCK && computer != SPOCK)
    {
        if (computer == SCISSORS || computer == ROCK)
        {
            cout << "You Win!!\n";
        }
        else if (computer == LIZARD || computer == PAPER)
            cout << "You Lose :(";
        return;
    }
    
}

void displayChoice(int choice)
{
    if (choice == ROCK)
        cout << "Rock\n";
    else if (choice == PAPER)
        cout << "Paper\n";
    else if (choice == SCISSORS)
        cout << "Scissors\n";
	else if (choice == LIZARD)
        cout << "Lizard\n";
    else if (choice == SPOCK)
        cout << "Spock\n";
    
}
Last edited on
Are you on the same class as: http://www.cplusplus.com/forum/beginner/138077/

Thou shall not call main() in your program.

Do not determine the tie within the determineWinner(). Do it before. You must change both user and computer choices there, because if the user alone changes his choice after a tie, he already knows what the computer has.

Same thing within the determineWinner(). If you have ensured that there is no tie, then rechecking for that is not necessary. Furthermore, if player is, say Spock, and computer is neither scissors or rock, then the computer is a paper lizard; no IF required.
Topic archived. No new replies allowed.