C++实现“23”抽签小游戏

版权声明:本文为博主思不良原创文章,转载请注明出处。 https://blog.csdn.net/weixin_44566320/article/details/89960154

开发工具与关键技术:VS 2015,C++ 基础
作者:王雅思
撰写时间:2019-05-08

“23”游戏为老师布置的作业,其规则如下:
“23” 游戏是一个双人游戏,道具是23根牙签,玩家轮流去1,2或3根牙签。拿到最后一根牙签的是输家。写程序和计算机玩“23”。总是玩家先走,轮到计算机时,它根据以下规则采取行动:

a.如果剩余多于4根,计算机就取走4-x根,x为玩家上次取走的牙签数
b.如果剩余2-4根牙签,计算机取走足够多的牙签,确保只剩1根
c.如果剩余1根牙签,计算机只能取走它并它并认输

玩家输入要取走的牙签数量时,程序应对输入的有效性进行检查。要确定玩家输入的数在1到3之间,而且试图取走的不能超过当前剩余的。

其实现代码如下:

#include<iostream>
#include<time.h>
#include<string>
using namespace std;

//游戏规则:拿到最后一根牙签的人 输

unsigned int total = 23;//牙签总数
unsigned int player_Count = 0;//玩家抽走牙签的数量
unsigned int computer_Count = 0;//计算机抽走牙签的数量
bool whoIsGo = 0;// 0 :计算机,1:玩家   

void player();//验证玩家输入有效性
void computer();//电脑判断规则抽取牙签
void checkWin();//判断输赢

void main()
{
   player();//玩家先操作
   //computer();//电脑先操作
}
验证玩家输入有效性
//验证玩家输入有效性
void player()
{
  whoIsGo = 1;//修改是玩家在玩

  unsigned int myCin = 0;//暂时存放我的输入值
  string s;//将输入的一堆字母数字符号组合 赋值给 s,达到清空输入流的目的

  cout << "请【玩家】输入想抽取牙签的数量:";
  cin >> myCin; cout << endl;		
  	
  if (cin.fail())//输入验证还是有问题,乱输还是有问题,如果输入数字在前字母在后,仍多次只读取数字
  {
  	cout << "输入无效!" << endl << endl;

  	//清空输入流缓存
  	cin.clear();//重置cin状态
  	cin.sync();
  	cin >> s;//将输入的一堆字母数字符号组合 赋值给 s,达到清空输入流的目的
  	player();//重新进入方法,再次输入
  }
  else
  {
  	player_Count = myCin;//输入正确,获取输入值
  	if ((player_Count > 3) || (player_Count < 1))
  	{
  		cout << "【玩家】每次只能抽取 1 至 3 根牙签!" << endl << endl;
  		player();//重新进入方法,再次输入
  	}
  	else if (player_Count > total)
  	{
  		cout << "【玩家】抽取的数量不能超过当前剩余的!" << endl << endl;
  		player();//重新进入方法,再次输入
  	}
  	else
  	{
  		total -= player_Count;//牙签数量减去玩家抽取的数量	
  		checkWin();//检查输赢
  	}
  }	
}

电脑判断规则抽取牙签
//电脑判断规则抽取牙签
void computer()
{
   whoIsGo = 0;//修改是计算机在玩
   if (total > 4)
   {
   		if (player_Count > 0)
   		{
   			computer_Count = 4 - player_Count; // 电脑抽取数量
   		}
   		else//在换成电脑先玩的情况下
   		{
   			srand(time(0));
   			const int randd = rand() % 3;
   			cout << randd;
   			if (randd == 0)
   			{ 
   				computer_Count = 3; 
   			}
   			else 
   			{ 
   				computer_Count = randd;
   			}
   		}
   		total -= computer_Count;//牙签数量减去电脑抽取数量
   		cout << "【计算机】抽取了 " << computer_Count << " 根牙签!" << endl << endl;
   }
   else if ((total > 2) || (total < 4))
   {
   		computer_Count = total - 1;
   		total = 1;//牙签数量减去电脑抽取数量
   		cout << "【计算机】抽取了 " << computer_Count << " 根牙签!" << endl << endl;
   }
   else
   {		
   }
   checkWin();
}

判断输赢
//判断输赢
void checkWin()
{
	cout << "剩余牙签数:" << total << endl << endl;
	if (total == 1)
	{
		if (whoIsGo == 1) {
			cout << "【计算机】拿走了最后一根牙签!!!" << endl << endl;
			cout << "【玩家】赢了!" << endl;
			cout << "【计算机】输了!" << endl << endl;
		}
		else
		{
			cout << "【玩家】只剩最后一根牙签了!!!" << endl << endl;
			cout << "【玩家】输了!" << endl;
			cout << "【计算机】赢了!" << endl << endl;
		}
		return;
	}
	else if (total == 0)//只有玩家会手贱拿走最后一根
	{
		cout << "【玩家】拿走了最后一根牙签!!!" << endl << endl;
		cout << "【玩家】输了!" << endl;
		cout << "【计算机】输了!" << endl << endl;
		return;
	}
	else
	{
		//继续往下执行
		if (whoIsGo == 0) //计算机在走
		{
			player();//玩家走
		}
		else
		{
			computer();//计算机走
		}
	}
实现效果

在这里插入图片描述
以上内容如有不正确的地方,或者有更好的方案,还请提出。
感谢您的浏览。

猜你喜欢

转载自blog.csdn.net/weixin_44566320/article/details/89960154