I am creating a rock, paper, scissors game (RPS) for my C++ class. I got most of the code working but I can't figure out how to implement the Log and the printLog functions an. I've been working on this assignment for the past 6 hrs and this is the only thing that is stopping my code from working correctly.
Assignment:
# Requirements:
* You must implement the following :
1. you have to invoke the rand() function from < cmath> to provide the choice of play for computer.
2. enum RPS{ Rock, Paper, Scissors} ; C++ enumerate type must be used.
3. An array of structure must be used for the game log.
4. void displayRules() // display the rule for RPS game.
5. RPS retrievePlay(char selection): // convert input R/r to Rock, P/p to Paper, S/s to Scissors.
6. bool validSelection(char selection); // check to see if the user's choice is valid (R P S or r p s )
7. void GameSummary(int gCount, int wCount1, int wCount2); // display the final statistics of the series.
8. RPS winningPlay(RPS play1, RPS play2); // return the winner of play1 vs play2
9. void gameResult(RPS play1, RPS play2, int& winner); // display playes' choices and the winner
10. void Log(RPS p1, RPS p2, int winner, int n); // store the current game result to the Log
11. void printLog(int) ; // display the log of the series.
# There is no need to validate the user input. (Assume the users are intelligent and not try to crash your system in purpose .... which ,in genereal , is not true.)
Code:
// Assignment 4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
enum RPS{Rock, Paper, Scissors} ;
void displayRules();
RPS retrievePlay(char selection);
bool validSelection(char selection);
void convertEnum(RPS object);
void GameSummary(int gCount, int wCount1, int wCount2, int tieCount);
RPS winningPlay(RPS play1, RPS play2);
void gameResult(RPS play1, RPS play2, int& winner);
void Log(RPS p1, RPS p2, int winner, int n);
void printLog(int gameCount) ;
void displayRules(){
cout << "Welcome to the game of Rock, Paper, and Scissors." << endl;
cout << "This is a game for two players (one could be computer." << endl;
cout << "For eachgame, each player selects one of the objects:" << endl;
cout << "Rock, Paper or Scissors." << endl;
cout << "The rules for winning the game are:" << endl;
cout << endl;
cout << "1. If both players selects the same object, it is a tie." << endl;
cout << "2. Rock breaks Scissors: So player who selects Rock wins." << endl;
cout << "3. Paper covers Rock: So player who selects Paper wins." << endl;
cout << "4. Scissors cuts Paper: So player who selects Scissors wins." << endl;
cout << endl;
cout << "Enter R or r to select Rock," << endl;
cout << " P or p to select Paper, and" << endl;
cout << " S or s to select Scissors." << endl;
cout << endl;
};
RPS retrievePlay(char selection){
RPS object;
switch (selection)
{
case 'P':
case 'p':
object = Paper;
break;
case 'R':
case 'r':
object = Rock;
break;
case 'S':
case 's':
object = Scissors;
}
return object;
};
bool validSelection(char selection)
{
switch (selection)
{
case 'R':
case 'r':
case 'P':
case 'p':
case 'S':
case 's':
return true;
default:
return false;
}
}
void convertEnum(RPS object){
switch (object)
{
case Rock:
cout << "Rock";
break;
case Paper:
cout << "Paper";
break;
case Scissors:
cout << "Scissors";
}
};
void GameSummary(int gCount, int wCount1, int wCount2, int tieCount){
cout << "The total number of plays : " << gCount << endl;
cout << "The number of plays won by You : " << wCount1 << endl;
cout << "The number of plays won by Computer: " << wCount2 << endl;
cout << "The number of tie plays : " << tieCount << endl;
};
RPS winningPlay(RPS play1, RPS play2){
if ((play1 == Rock && play2 == Scissors) || (play2 == Rock && play1 == Scissors)){
return Rock;
}
else if ((play1 == Rock && play2 == Paper) || (play2 == Rock && play1 == Paper)){
return Paper;
}
else{
return Scissors;
}
};
void gameResult(RPS play1, RPS play2, int& winner){
RPS winnerPlay;
if (play1 == play2){
winner = 0;
cout << "Both players selected ";
convertEnum(play1);
cout << ". This game is a tie." << endl;
}
else {
winnerPlay = winningPlay(play1, play2);
cout << "Player 1 selected ";
convertEnum(play1);
cout << " and Computer chose ";
convertEnum(play2);
cout << ". " << endl;
if (play1 == winnerPlay){
winner = 1;
}
else if (play2 == winnerPlay) {
winner = 2;
}
cout << endl;
cout << "Player " << winner << " wins the game." << endl;
}
};
const int N = 100 ;
struct game{
RPS play1 ;
RPS play2 ;
int winner ;
} GameLog[N] ;
void Log(RPS p1, RPS p2, int winner, int n){
GameLog[n].play1 = p1;
GameLog[n].play2 = p2;
GameLog[n].winner = winner;
};
void printLog(int gameCount){
int n = 0;
//retrievePlay(selection1);
//retrievePlay(selection2);
RPS play1 = GameLog[n].play1;
RPS play2 = GameLog[n].play2;
int gamewinner = 0;
int winner = 0;
Log(play1,play2,gamewinner, gameCount);
for(int i = 0; i < gameCount; i++){
//gameResult(play1, play2, winner);
cout << "Game# " << i+1;
gameResult(play1, play2, winner);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int compChoice;
int gameCount= 0; //variable to store the number of
//games played
int winCount1 = 0 ; //variable to store the number of games
//won by you
int winCount2 = 0; //variable to store the number of games
//won by computer
int tieCount = 0;
int gamewinner;
char response; //variable to get the user's response to
//play the game
//play the game
char selection1;
char selection2;
RPS play1; //player1's selection
RPS play2; //player2's selection
displayRules();
cout << "Enter Y/y to play the game: ";
cin >> response;
cout << endl;
while (response == 'Y' || response == 'y')
{
cout << "Player 1 enter your choice: " << endl;
cin >> selection1;
cout << endl;
// instead of prompt for player2's choice, you need to invoke the rand() function and
// assign the choice for the computer
compChoice = rand() % 3;
if (compChoice == 0){
selection2 = 'r';
}
if (compChoice == 1){
selection2 = 'p';
}
if (compChoice == 2){
selection2 = 's';
}
if (validSelection(selection1)
&& validSelection(selection2))
{
play1 = retrievePlay(selection1);
play2 = retrievePlay(selection2);
gameCount++;
gameResult(play1, play2, gamewinner);
if (gamewinner == 1)
winCount1++;
else if (gamewinner == 2)
winCount2++;
else //(gamewinner == 1 && gamewinner == 2)
tieCount++;
//Log(play1, play2, gamewinner, gameCount);
}//end if
cout << "\nEnter Y/y to play the game: ";
cin >> response;
cout << endl;
}//end while
GameSummary(gameCount, winCount1,
winCount2,tieCount);
cout << "\n\nDo you want a Game Log ? " ;
cin >> response ;
cout << endl;
if (response == 'Y' || response == 'y') printLog(gameCount);
return 0;
system("pause");
}
Can anyone please tell me how to get the Log() function to work?