rock paper scissors
Feb 28, 2019 at 9:55am UTC
Hi,
I am writing a program that leads the user to play a rock/paper/scissors game
against the computer.
The first one reaching 10 wins, has won the game.
There is indeed a logical error since the program runs but the command prompt is exited right away, but i am not sure where.
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++;
}
}
Feb 28, 2019 at 10:11am UTC
> const int MAXNUMGames = (userscore == 10 || compscore == 10);
1. This is a boolean result, so either 0 or 1.
2. It's initialised ONCE when the program starts.
Feb 28, 2019 at 10:27am UTC
I have written MAXNUMGames=10 before and it worked, but it stopped at 10 games no matter who won.
I am looking to write a program that stops the game as soon as either/or reaches 10 wins.
Topic archived. No new replies allowed.