栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

用户界面
--------
 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;
}

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

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778963.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号