Undeclared Scope in Rock Paper Scissors Simple Game
- by Rianelle
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
bool win;
int winnings;
int draws;
int loses;
string comChoice;
string playerChoice;
void winGame () {
cout << "You won! Play again?" <<endl;
cout << "Type y/n" <<endl;
char x;
cin >> x;
if (x == 'y') {
beginGame();
} else if ('n'){
cout << "Game Stopped." <<endl;
cout << "Number of Draws: " <<draws << endl;
cout << "Number of Loses: " <<loses << endl;
cout << "Number of Wins: " << winnings << endl;
win = true;
}
}
void drawGame (){
++draws;
cout << "Draw! Try again" << endl;
return;
}
void lose () {
cout << "You lose! Try again?" <<endl;
cout << "Type y/n" <<endl;
char feedback;
cin >> feedback;
if (feedback == 'y') {
beginGame();
} else if ('n'){
cout << "Game Stopped." <<endl;
cout << "Number of Draws: " <<draws << endl;
cout << "Number of Loses: " <<loses << endl;
cout << "Number of Wins: " << winnings << endl;
}
}
void beginGame() {
cout << "Welcome to the Rock, Paper and Scissors Game!" <<endl;
cout << "Let's begin. Type <rock, paper, scissors> for your choice!" <<endl;
cin >> playerChoice;
srand(time(0));
int randomizer = 1+(rand()%3);
if (randomizer == 1)
comChoice = "rock";
if (randomizer == 2)
comChoice = "paper";
if (randomizer == 3)
comChoice = "scissors";
do {
if (playerChoice == comChoice) {
drawGame();
}
if (playerChoice == "rock" && comChoice == "paper")
++loses;
lose();
if (playerChoice == "rock" && comChoice == "scissors")
++winnings;
winGame();
if (playerChoice == "paper" && comChoice == "rock")
++winnings;
winGame();
if (playerChoice == "paper" && comChoice == "scissors")
++loses;
lose();
if (playerChoice == "scissors" && comChoice == "rock")
++loses;
lose();
if (playerChoice == "scissors" && comChoice == "paper")
++winnings;
winGame();
}while (win != true);
}
int main () {
beginGame();
return 0;
}