⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 石剪布.cpp

📁 两个小游戏----王婆卖瓜和石头剪子布
💻 CPP
字号:
#include<iostream.h>
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<<"Player 2 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 Scissor.\n"<<endl;
	cout<<"This is a game for two Players .For each game,each player selects one of \n the objects,"
		 "Rock,Paper,or Scissors.\n"<<endl;
	cout<<"The rules for winning the game are:"<<endl;
	cout<<"  1.If both players select the 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 selects Paper wins."<<endl;
	cout<<"  4 Scissors cut Paper: So the player selects Scissors wins."<<endl;
	cout<<"\nEnter R or r to select Rock,P or p to select Paper,\n and s or S to select Scissors."<<endl;
	cout<<endl;




}

objectType retrievePlay(char selection)
{
  objectType object;
	switch(selection)
	{
		case 'R':case'r':object=Rock;
			break;
	case 'S':case's':object=Scissors;
		break;

	case'P':case'p':object=Paper;
	}
	return object;
	


}
bool validSelection(char selection)
{
    switch(selection)
	{
	case 'R':case'r':
	case 'S':case's':
	case'P':case'p':return true;
	default:return false;
	}
	

}
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=1;
			else
				if(play2==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;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -