Rock Paper Scissor Lizard Spock BOOL Won't End Program After Win/Lose

I am attempting the Rock Paper Scissors Lizard Spock program and believe I have everything working correctly except I cant seem to get BOOL to work are there any good resources for describing BOOL and its use.
Last edited on
determineWinner(...) should return bool which will be true if there is a winner and false otherwise. Do not call main().

The while on line 76 should be if.
Thanks for the reply. I have been trying to figure out how to make the determineWinner a bool return but am not sure if I am misplacing it or just completely getting it wrong.

If I should not call main () to restart after a draw would having a return false restart the game and a return true end it? Again thanks for any assistance.

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
153
154
155
156
157
158
159
#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;
const int EXIT		= 6;
int getUserChoice();
int getComputerChoice();
bool determineWinner(int, int);
void displayChoice(int);

int main()
{
	int userChoice;
	int computerChoice;


	computerChoice = getComputerChoice();
	userChoice = getUserChoice();

	while (userChoice != 6)
	{
		determineWinner(userChoice, computerChoice);
		computerChoice = getComputerChoice();
		userChoice = getUserChoice();
	}

	return 0;
}

int getComputerChoice()
{
	unsigned seed = time(0);
	srand(seed);

	int number = 1 + rand() % 5;

	return number;
}

int getUserChoice()
{
	int choice;

	cout << "\nRock, Paper, Scissors and Beyond\n";
	cout << "================================\n";
	cout << "1) Rock\n";
	cout << "2) Paper\n";
	cout << "3) Scissors\n";
	cout << "4) Lizard\n";
	cout << "5) Spock\n";
	cout << "\nEnter your choice: ";
	cin >> choice;

	while (choice < 1 || choice > 6)
	{
		cout << "Enter a number form 1-6 ";
		cin >> choice;
	}

	return choice;
}

bool determineWinner(int user, int computer)
{
	cout << "\nYou selected: \n";
	displayChoice(user);
	cout << "\nThe computer selected: \n";
	displayChoice(computer);
	bool determineWinner = true;

	if (user == computer)
	{
		cout << "You tied! There can be only one! Play again! \n" << endl;
		return false;
	}

	if (user == ROCK)
	{
		if (computer == SCISSORS)
			cout << "\nYou win! Rock breaks Scissors.\n" << endl;
		else if (computer == PAPER)
			cout << "\nYou lose! Paper covers Rock. \n" << endl;
		else if (computer == LIZARD)
			cout << "\nYou win! Rock crushes Lizard. \n" << endl;
		else if (computer == SPOCK)
			cout << "\nYou lose! Spock vaporizes Rock. \n" << endl;
		return true;
	}
	else if (user == PAPER)
	{
		if (computer == SCISSORS)
			cout << "\nYou lose! Scissors cut Paper. \n" << endl;
		else if (computer == ROCK)
			cout << "\nYou win! Paper covers Rock. \n" << endl;
		else if (computer == LIZARD)
			cout << "\nYou lose! Lizard eats Paper. \n" << endl;
		else if (computer == SPOCK)
			cout << "\nYou win! Paper disproves Spock. \n" << endl;
		return true;
	}
	else if (user == SCISSORS)
	{
		if (computer == ROCK)
			cout << "\nYou lose! Rock smashes Scissors. \n" << endl;
		else if (computer == PAPER)
			cout << "\nYou win! Scissor cuts Paper. \n" << endl;
		else if (computer == LIZARD)
			cout << "\nYou win! Scissors decapitate Lizard. \n" << endl;
		else if (computer == SPOCK)
			cout << "\nYou lose! Spock melts Scissors. \n" << endl;
		return true;
	}
	else if (user == LIZARD)
	{
		if (computer == SCISSORS)
			cout << "\nYou lose! Scissors decapitate Lizard. \n" << endl;
		else if (computer == ROCK)
			cout << "\nYou lose! Rock crushes Lizard. \n" << endl;
		else if (computer == PAPER)
			cout << "\nYou win! Lizard eats Paper. \n" << endl;
		else if (computer == SPOCK)
			cout << "\nYou win! Lizard poisons Spock. \n" << endl;
		return true;
	}
	else if (user == SPOCK)
	{
		if (computer == ROCK)
			cout << "\nYou win! Spock vaporizes Rock. \n" << endl;
		else if (computer == PAPER)
			cout << "\nYou lose! Paper disproves Spock. \n" << endl;
		else if (computer == LIZARD)
			cout << "\nYou lose! Lizard poisons Spock. \n" << endl;
		else if (computer == SCISSORS)
			cout << "\nYou win! Spock melts Scissors. \n" << endl;
		return true;
	}
}

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";
	return;
}
I have been working on your program on my lunch hour and found some ??? that maybe the reason your program does not close after 1st use.

1.-When using While loops make sure you really need it. If while is not really needed sometimes is better to use if statements when running something once.
2.- On line 11 you have a variable names 'EXIT' that has never been used in your program.
3.-userChoice = getUserChoice(); may not be needed since you are running the program once.
4.-Do you really need a while loop on main()?
5.-do you really need to return a value after each function?

Remember that you are only running the program once, and you do not what to repeat anything while the program runs. Except in special cases. Hope this helps. Good and fun program! =)
Lines 83-94: Regardless of what the computer selection is, you always return true. Each of the tests must return the appropriate boolean value.
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
if (user == ROCK)
  {  if (computer == SCISSORS)
      {  cout << "\nYou win! Rock breaks Scissors.\n" << endl;
          return true;  // winner
     }
     if (computer == PAPER)
     {  cout << "\nYou lose! Paper covers Rock. \n" << endl;
         return false;
     }
     if (computer == LIZARD)
     {  cout << "\nYou win! Rock crushes Lizard. \n" << endl;
         return true;
     }
     if (computer == SPOCK)
     {  cout << "\nYou lose! Spock vaporizes Rock. \n" << endl;
	return false;
     }
  }  // end user == ROCK 

Ditto for each of the other user selections.

Line 39: It's a poor practice to bury a call to srand(). srand() should be called ONCE at the beginning of main.

Lines 77-81: You may want to move your check for a tie out of determineWinner(). As written, determineWinner() is really a tri-state function (win, lose, tie).

Lines 6-11: You may want to look into enums for these values.
Last edited on
Topic archived. No new replies allowed.