Rock paper scissors 2

closed account (4wpL6Up4)
Hi,
I am not sure what is wrong with this code and how to fix it.
It should stop when comp or user reaches 10 wins.
Besides MAXNUMgames I am not sure what else is wrong.


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
   #include"pch.h"
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;


enum weapon {
	ROCK = 10,   
	PAPER,
	SCISSORS = 30
};

int userscore = 0;
int compscore = 0;
const int MAXNUMGames = (userscore == 10 || compscore == 10);

string show(weapon h) {
	if (h == ROCK) return "ROCK";
	if (h == SCISSORS) return "SCISSORS";
	if (h == PAPER) return "PAPER";
}

string displayWinner(weapon h, weapon c) {
	if (h == ROCK && c == SCISSORS)
	{
		return "ROCK smashes SCISSORS! you won";
		userscore++;
   
	}
	if (h == ROCK && c == PAPER)
	{
		return "PAPER covers ROCK!  Computer won";
		compscore++;
	}
	if (h == SCISSORS && c == PAPER)
	{
		return "SCISSORS cuts PAPER ! you won";
		userscore++;
	}
	if (h == SCISSORS && c == ROCK)
	{
		return "ROCK smashes SCISSORS! computer won";
		compscore++;
	}
	if (h == PAPER && c == ROCK)
	{
		return " PAPER covers ROCK! you won";
		userscore++;
	}
	if (h == PAPER && c == SCISSORS)
	{
		return "SCISSORS cuts PAPER! Computer won";
		compscore++;
	}
	if (h == c) return " ... DRAW!! ....";
}



int main()
{
	int games_played = 0;
	srand(time(NULL));

	int human_choice;
	weapon computer_weapon;
	weapon human_weapon;

	while (games_played < MAXNUMGames)
	{

		cout << "What do you choose [ROCK is default]? ROCK (0), PAPER (1) or SCISSORS (2)  ";
		cin >> human_choice;
		if (human_choice == 0) human_weapon = ROCK;
		else if (human_choice == 1) human_weapon = PAPER;
		else if (human_choice == 2) human_weapon = SCISSORS;
		else human_weapon = ROCK;

		int x = rand() % 3;
		if (x == 0) computer_weapon = ROCK;
		if (x == 1) computer_weapon = PAPER;
		if (x == 2) computer_weapon = SCISSORS;
		cout << "computer chooses " << show(computer_weapon) << endl;
		cout << displayWinner(human_weapon, computer_weapon) << "\n next game" << endl;
		games_played++;
	}
	
}
1
2
3
int userscore = 0;
int compscore = 0;
const int MAXNUMGames = (userscore == 10 || compscore == 10);


userscore == 10 comes out as false, compscore == 10 comes out as false, so this is:

const int MAXNUMGames = (false || false );
so
const int MAXNUMGames = false ;

What exactly are you trying to set MAXNUMGames to? I bet you didn't mean to set it to false.
Last edited on
closed account (4wpL6Up4)
MAXNUMGames is supposed to be the maximum number of games, and should end when one of the participants (user or computer) is the first reaching 10 wins.
variable names!
if I see maxgames, I expect to play 10 rounds.
if I see matchwincondition or words to that effect, I know that it takes that many won rounds to stop.

that is if I see maxgames is 10, I play 10 games, win 3, lose 3, draw 4, total 10 games.
if i see matchwincondition is 10, I could play 50 games, win 10, lose 9, and draw 31.

MAXNUMGames is not helping you as a boolean expression.

you just need
do
{
play_games();
} while(userscore <10 && compscore < 10); //as soon as either is 10, this is false.

or
const int MAXNUMGames = 10;

while(userscore <MAXNUMGames && compscore < MAXNUMGames ); //better to use a named constant, so this is the way to go (apart from the name of it, which I would still adjust)
Last edited on
Topic archived. No new replies allowed.