C++ Rock Paper scissors. What do i do next?


#1
  1. Additional input to ask for player's name
  2. Create a structure to store Player's name, status of game (WIN or LOSS), title of user choice (rock, paper, scissors), title of computer choice(rock, paper, scissor)
  3. Create a record and store this structure on a file called games.
  4. At the beginning of the game, when user inputs name, look for that players name and if found , display player's name and wins and losses on the screen
  5. Provide an option to be able to list the top three scores stored on the file.

Remember, questions and answers are free

THIS IS WHAT I HAVE SO FAR.

// Project 5.cpp : Defines the entry point for the console application.
//

include "stdafx.h"

include

include

include

using namespace std;

enum object ROCK, PAPER, SCISSORS;

void displayRules();
object getPlay();
void play(int&, int&, int&);
object AI();
object determineWinner(object, object);
void print(int, int, int);

int main()
{
int menuChoice, p1Wins = 0, p2Wins = 0, tie = 0;
do
{
cout << "PAPER = ROCK = SCISSORS" << endl;
cout << "To see the rules, enter 1." << endl;
cout << "To play the game, enter 2." << endl;
cout << "To display results, enter 3." << endl;
cout << "To exit, enter 9." << endl;
cin >> menuChoice;
if (!cin)
{
cin.clear();
cin.ignore(100, '\n');
}
switch (menuChoice)
{
case 1:
displayRules();
break;
case 2:
play(p1Wins, p2Wins, tie);
break;
case 3:
print(p1Wins, p2Wins, tie);
break;
case 9:
break;
}
}while (menuChoice != 9);
return 0;
}

void displayRules()
{
cout << "\nWelcome to rock paper scissors." << endl;
cout << "Play is against the computer and you" << endl;
cout << "must select n object from paper, rock," << endl;
cout << "or scissors." << endl;
cout << "If both players pick the same object, game is a tie." << endl;
cout << "Rock breaks scissors: Rock wins." << endl;
cout << "Paper covers rock: Paper wins." << endl;
cout << "Scisoors cuts paper : Scissors wins." << endl << endl;
}
object getPlay()
{
object p1;
char ch;
bool isValid = false;
cout << "Enter R for rock, S for scissors, P for paper: ";
do
{
cin >> ch;
if (ch == 'R' || ch == 'r')
isValid = true;
else if (ch == 'S' || ch == 's')
isValid = true;
else if (ch == 'p' || ch == 'p')
isValid = true;
} while (isValid == false);

switch (ch)
{
case 'R':
case 'r':
	p1 = ROCK;
	break;
case 'S':
case 's':
	p1 = SCISSORS;
	break;
case 'P':
case 'p':
	p1 = PAPER;
	break;
}
return p1;

}

void play(int& win1, int& win2, int& tie)
{
object player1, player2, winner;
bool isWon = false;
do
{
cout << "Player one. " << endl;
player1 = getPlay();
switch (player1)
{
case 0: cout << "Rock." << endl;
break;
case 1:
cout << "Paper." << endl;
break;
case 2:
cout << "Scissors" << endl;
break;
}
cout << "Computer chooes ";
player2 = AI();
cout << endl;
winner = determineWinner(player1, player2);
if (player1 == player2)
{
cout << "Game is tie." << endl;
tie++;
}
else if (winner == player1)
{
cout << "Player 1 wins!" << endl;
win1++;
}
else if (winner == player2)
{
cout << "Computer wins." << endl;
win2++;
}
else
{
cout << "Game is tie." << endl;
tie++;
}
isWon = true;
} while (isWon == false);
}

object AI()
{
object computer;
int num = (rand() + time(0)) % 3;
switch (num)
{
case 0:
computer = ROCK;
cout << "Rock" << endl;
break;
case 1:
computer = PAPER;
cout << "Paper"
<< endl;
break;
case 2:
computer = SCISSORS;
cout << "Scissors" << endl;
break;
}
return computer;
}

object determineWinner(object p1, object p2)
{
if ((p1 == ROCK && p2 == SCISSORS) || (p2 == ROCK && p1 == SCISSORS))
return ROCK;
else if ((p1 == ROCK && p2 == PAPER) || (p2 == ROCK && p1 == PAPER))
return PAPER;
else
return SCISSORS;
}

void print(int p1, int p2, int tie)
{
cout << "Player 1 wins = " << p1 << endl;
cout << "Computer wins = " << p2 << endl;
cout << "Number of ties = " << tie << endl;
cout << "Total games = " << p1 + p2 + tie << endl << endl;
}


#2

@padronology, thanks for sharing your code! I am intrigued and very interested with the ideas you have to further improvise the exercise!

It could be very appreciated if you can edit your topic with proper code indentation using the
preformatted text function. Currently, your code is very long and a bit :dizzy_face: to read.

Thanks for the effort!


#3

This is where I'm stuck now. I'm trying to store the user's name, but it says (identifier "getline" is undefined)
PLEASE HELP

{
while (getline(myfile, line))
{
std::string token = line.substr(0, line.find(" "));
if (token == player.playerName)
cout << "line" << '\n';
}
myfile.close();
}


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.