64,653
社区成员
发帖
与我相关
我的任务
分享
编译器: Default compiler
执行 g++.exe...
执行结束
成功编译
#include <iostream>
using namespace std;
enum objectType{ Rock, Paper, Scissors};
void displayRules();
objectType retrievePlay(char selection);
bool validSelection(char selection);
void convertEnum(objectType object);
objectType winningObject(objectType play1, objectType play2);
void gameResult(objectType play1, objectType play2, int& winner);
void displayResults(int gCount, int wCount1, int wCount2);
int main()
{
int gameCount;
int winCount1;
int winCount2;
int gamewinner;
char response;
char selection1;
char selection2;
objectType play1;
objectType play2;
gameCount = 0;
winCount1 = 0;
winCount2 = 0;
displayRules();
cout <<"Enter Y/y to play the game: ";
cin>> response;
cout <<endl;
while (response == 'Y') // response == 'y')
{
cout <<"Player 1 enter your choice: ";
cin>> selection1;
cout <<endl;
cout <<"Player2 enter your choice: ";
cin>> selection2;
cout <<endl;
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++;
}
cout <<"Enter Y/y to play the game:";
cin>> response;
cout <<endl;
}
displayResults(gameCount, winCount1, winCount2);
return 0;
}
void displayRules()
{
cout <<" Welcome to the game of Rock, Paper, and Scissors." <<endl;
cout <<" This is agame for two players.For each game, each" <<endl;
cout <<" player selects one of the objiects, Rock, Paper, or"
<<" Scissors." <<endl;
cout <<" The rules for winning the game are:" <<endl;
cout <<"1. If both players select the same object, it is a"
<<" tie." <<endl;
cout <<"2. Rock Breaks Scissors: So the player who selects Rock"
<<" wins." <<endl;
cout <<"3. Paper covers Rock: So the player who selects Scissors"
<<" ins." <<endl;
cout <<"4. Scissors cut Paper: So the player who selects Scissors"
<<" wins." <<endl <<endl;
cout <<"Enter R or r to select Rock, P or p to select Paper, and "
<<" S or s to select Scissors." <<endl;
}
bool validSelection(char selection)
{
switch (selection)
{
case 'R': case 'r':
case 'S': case 's':
case 'P': case 'p': return true;
default: return false;
}
}
objectType retrievePlay(char selection)
{
objectType object;
switch (selection)
{
case 'R': case 'r': object = Rock;
case 'P': case 'p': object = Paper;
case 'S': case 's': object = Scissors;
}
return object;
}
void convertEnum(objectType object)
{
switch (object)
{
case Rock: cout <<"Rock";
break;
case Paper: cout <<"Paper";
break;
case Scissors: cout <<"Scissors";
}
}
objectType winningObject(objectType play1, objectType 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(objectType play1, objectType play2, int& winner)
{
objectType winnerObject;
if(play1 == play2)
{
winner = 0;
cout <<"Both players selected";
convertEnum(play1);
cout <<". This game is a tie." <<endl;
}
else
{
winnerObject = winningObject(play1, play2);
cout <<"Player 1 selected ";
convertEnum(play1);
cout <<" and player 2 selected ";
convertEnum(play2);
cout <<". ";
if (play1 == winnerObject)
winner = 2;
cout <<"Player " <<winner <<" wins this game." <<endl;
}
}
void displayResults(int gCount, int wCount1, int wCount2)
{
cout <<"The total number of plays: " <<gCount <<endl;
cout <<"The number of plays won by player 1:" <<wCount1 <<endl;
cout <<"The number of plays won by player 2:" <<wCount2 <<endl;
}
//编译已通过
--------------------Configuration: test2 - Win32 Debug--------------------
Compiling...
main.cpp
main.obj - 0 error(s), 0 warning(s)
#include <iostream>
using namespace std;
enum objectType{ Rock, Paper, Scissors};
void displayRules();
objectType retrievePlay(char selection);
bool validSelection(char selection);
void convertEnum(objectType object);
objectType winningObject(objectType play1, objectType play2);
void gameResult(objectType play1, objectType play2, int& winner);
void displayResults(int gCount, int wCount1, int wCount2);
int main()
{
int gameCount;
int winCount1;
int winCount2;
int gamewinner;
char response;
char selection1;
char selection2;
objectType play1;
objectType play2;
gameCount = 0;
winCount1 = 0;
winCount2 = 0;
displayRules();
cout <<"Enter Y/y to play the game: ";
cin>> response;
cout <<endl;
while (response == 'Y') // response == 'y')
{
cout <<"Player 1 enter your choice: ";
cin>> selection1;
cout <<endl;
cout <<"Player2 enter your choice: ";
cin>> selection2;
cout <<endl;
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++;
}
cout <<"Enter Y/y to play the game:";
cin>> response;
cout <<endl;
}
displayResults(gameCount, winCount1, winCount2);
return 0;
}
void displayRules()
{
cout <<" Welcome to the game of Rock, Paper, and Scissors." <<endl;
cout <<" This is agame for two players.For each game, each" <<endl;
cout <<" player selects one of the objiects, Rock, Paper, or"
<<" Scissors." <<endl;
cout <<" The rules for winning the game are:" <<endl;
cout <<"1. If both players select the same object, it is a"
<<" tie." <<endl;
cout <<"2. Rock Breaks Scissors: So the player who selects Rock"
<<" wins." <<endl;
cout <<"3. Paper covers Rock: So the player who selects Scissors"
<<" ins." <<endl;
cout <<"4. Scissors cut Paper: So the player who selects Scissors"
<<" wins." <<endl <<endl;
cout <<"Enter R or r to select Rock, P or p to select Paper, and "
<<" S or s to select Scissors." <<endl;
}
bool validSelection(char selection)
{
switch (selection)
{
case 'R': case 'r':
case 'S': case 's':
case 'P': case 'p': return true;
default: return false;
}
}
objectType retrievePlay(char selection)
{
objectType object;
switch (selection)
{
case 'R': case 'r': object = Rock;
case 'P': case 'p': object = Paper;
case 'S': case 's': object = Scissors;
}
return object;
}
void converEnum(objectType object)
{
switch (object)
{
case Rock: cout <<"Rock";
break;
case Paper: cout <<"Paper";
break;
case Scissors: cout <<"Scissors";
}
}
objectType winningObject(objectType play1, objectType 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(objectType play1, objectType play2, int& winner)
{
objectType winnerObject;
if(play1 == play2)
{
winner = 0;
cout <<"Both players selected";
converEnum(play1);
cout <<". This game is a tie." <<endl;
}
else
{
winnerObject = winningObject(play1, play2);
cout <<"Player 1 selected ";
convertEnum(play1);
cout <<" and player 2 selected ";
convertEnum(play2);
cout <<". ";
if (play1 == winnerObject)
winner = 2;
cout <<"Player " <<winner <<" wins this game." <<endl;
}
}
void displayResults(int gCount, int wCount1, int wCount2)
{
cout <<"The total number of plays: " <<gCount <<endl;
cout <<"The number of plays won by player 1:" <<wCount1 <<endl;
cout <<"The number of plays won by player 2:" <<wCount2 <<endl;
}