实现人机版石头剪子布游戏,三局两胜。

实现人机版石头剪子布游戏,三局两胜。,第1张

用户界面
--------
 0.石头
 1.剪刀
 2.布
--------

人机随机出
用户输入0,人机随机出剪刀打印:

user:石头  
computer:剪刀
第一局  你胜了

--------
 0.石头
 1.剪刀
 2.布
--------
人机随机出
用户输入0,人机随机出剪刀打印:

user:石头  
computer:剪刀
第二局  你胜了

--------
 0.石头
 1.剪刀
 2.布
--------
人机随机出
用户输入0,人机随机出布打印:

user:石头  
computer:布
第三局  你输了

打印:你胜利了
询问是否继续,输入Y 继续 输入N 退出程序

#include 
#include 
#include 
#include 
int count = 1;				//第几局,引入计数
int score[2] = { 0 };
void printMenu() 
{
	printf("----------------------\n");
	printf("\t0.石头  \n");    //key computer)  //2:布 0:石头
	{
		//电脑赢了
		printResult("你输了");
		score[1] += 1;
		

	}
	else if (userKey == computer)
	{
		//平局
		printResult("平局");
	}
	else 
	{
		printResult("你赢了");
		score[0] += 1;
		

	}
	count++;
	//三局两胜
	//赢1次  2平
	//赢2次
	//赢3次
	//只要赢了2次或者赢的次数大于机器的次数就可以了
}
int main()
{
	srand((unsigned int)time(NULL));	
	while (1) 
	{
		printMenu();
		keyDown(count);
		if (count == 4) 
		{
			if (score[0] > score[1]) 
			{
				printf("你赢了!\n");
			}
			else if(score[0] == score[1])
			{
				printf("平局!\n");
			}
			else 
			{
				printf("你输了!\n");
			}
			printf("是否继续(Y/N):");
			while (getchar() != '\n');
			int userkey = getchar();
			if (userkey == 'y' || userkey == 'Y') 
			{
				count = 1;
				score[0] = 0;
				score[1] = 0;
				system("cls");
				continue;
			}
			else 
			{
				break;
			}
		}
		system("pause");
		system("cls");
	}
	return 0;
}

注:读者可适当的进行优化,比如一方已经赢了两局,那么可以提前结束了!

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/562334.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-01
下一篇 2022-04-01

发表评论

登录后才能评论

评论列表(0条)

保存