C++游戏game | 井字棋游戏坤坤版(配资源+视频)【赋源码,双人对战】

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

 

博主主页Yu·仙笙🦄

专栏C++游戏game

资源C++井字棋游戏双人对战源码【编译通过】

视频C++井字棋坤坤版

如果不想看代码制作过程及原理想直接play者请自行划到文末取源码

目录

一、前期准备

二、获取时间

三、游戏初始背景

四、玩家信息录入

五、玩家投掷骰子确定出手先后

六、玩家选择棋子样式

六、游戏正文执行部分

七、游戏后期处理

源码


井字棋Tic Tac Toe又称井字游戏、"连城"游戏、OX棋是一种供两人玩的纸笔游戏。两个玩家轮流在九个空格中画上代表自己的O或X谁先将自己的符号连成一线横连、竖连、斜连皆可即获得胜利。倘若在游戏过程中双方都采取最佳策略那么游戏往往会以平局告终。井字棋也被应用于人工智能与博弈论的研究。


蔡徐坤KUN1998年8月2日出生中国内地男歌手、演员。

2012年4月蔡徐坤因参加综艺节目《向上吧少年》进入全国200强而进入娱乐圈8月参演个人首部偶像剧《童话二分之一》。2014年3月参演个人首部电影《完美假妻168》2015年7月蔡徐坤参加真人秀节目《星动亚洲》并成功进入全国前十五强2016年10月蔡徐坤通过10人男子组合SWIN正式出道2018年1月参加爱奇艺重点打造的中国首档偶像男团竞演养成类真人秀《偶像练习生》。2018年4月6日在《偶像练习生》决赛中蔡徐坤最终排名第一作为Nine Percent组合成员正式出道。6月15日参加的纪录片《NINEPERCENT花路之旅》在爱奇艺上线。2018年8月2日入选福布斯2018年中国“30位30岁以下精英”榜单。8月18日随组合参加2018微博粉丝嘉年华线下活动。12月18日担任2019年北京卫视春晚代言人。同年12月获第十二届音乐盛典咪咕汇年度“最佳彩铃销量歌手”、年度十大金曲《WaitWaitWait》、搜狐时尚盛典“年度人气男明星”以及今日头条年度盛典“年度偶像人物”。2019年2月首登北京台春晚便包揽词曲为其创作歌曲《那年春天》。2019年2月19日蔡徐坤合约案胜诉与原公司经纪合约解除。3月22日发布海外公演主题曲《Bigger》。4月19日发布单曲《Hard To Get》。12月25日加盟《青春有你第二季》担任青春制作人代表。

---------------------------------------------------------------------------------------------------------------------------------

如果不想看代码制作过程及原理想直接play者请自行划到文末取源码

一、前期准备

#include<iostream> 
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<ctime>
using namespace std;
char play[9] = { '1','2','3','4','5','6','7','8','9' };

二、获取时间

void system()
{
	system("color FD");
	system("date/t");
	system("time/t");
	system("title 井字棋 Ver:1.0.0");
}

三、游戏初始背景

void CSH()
{
	cout << "######井字棋小游戏######" << endl;
	cout << "(*游戏规则先掷骰子选出优先下子者然后轮流下子任一方若有三子连成一线该方即获胜)" << endl;
	cout << "+---+---+---+\n"
		    "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
		    "+---+---+---+\n"
	     	"| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
	    	"+---+---+---+\n"
		    "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
		    "+---+---+---+\n"<<endl;
	cout<<"鸡你太美"<<endl;
	cout<<"你干嘛哎呦"<<endl;
	cout<<"-------------------------------------------------------"<<endl;
}
void return_CSH()
{
	memset(play,0,9);
	play[0] = '1';
	play[1] = '2';
	play[2] = '3';
	play[3] = '4';
	play[4] = '5';
	play[5] = '6';
	play[6] = '7';
	play[7] = '8';
	play[8] = '9';
}

四、玩家信息录入

class Player {
public:
	Player() {}
	void Name(int i)
	{
		cout << "请为玩家" << i << "设置玩家名称";
		cin >> name;
	}
	void Get_Name()
	{
		cout << name;
	}

五、玩家投掷骰子确定出手先后

int Order()
	{
		cout << "请玩家 " << name << " 掷骰子";
		system("pause");
		srand((unsigned)time(NULL));
		a = rand() % 6 + 1;
		cout << a << endl;
		return a;
	}
	int PD()
	{
		return a;
	}

六、玩家选择棋子样式

void XQ_1()
	{
		cout << "******游戏设置******" << endl;
		cout << "   1.O        2.X   " << endl;
		cout << "请玩家 " << name << " 选择执什么棋(1 或者 2)";
		while (xq == 0)
		{
			cin >> xq;
			if (xq == 1)
				q1 = 'O';
			else if (xq == 2)
				q1 = 'X';
			else
				cout << "命令错误请重新输入合法选项";
		}
	}
	void XQ_2()
	{
		cout << "请玩家 " << name << " 选择执什么棋(1 或者 2)";
		while (xq == 0)
		{
			cin >> xq;
			if (xq == 1)
				q2 = 'O';
			else if (xq == 2)
				q2 = 'X';
			else
				cout << "命令错误请重新输入合法选项";
		}
	}

六、游戏正文执行部分

void XS_Play()
	{
		int n;
		cout << "请玩家 " << name << " 输入下棋位置";
		cin >> n;
		if (play[n - 1] != 'O' && play[n - 1] != 'X'&&n<10)
		{
			play[n - 1] = q1;
		}
		else
		{
			cout << "位置不合法请重新选择";
			cin >> n;
			if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
			{
				play[n - 1] = q1;
			}
			else
			{
				cout << "我感觉你是故意的";
				exit(1);
			}
		}
		system("cls");
		cout << "+---+---+---+\n"
			    "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
			    "+---+---+---+\n"
			    "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
			    "+---+---+---+\n"
			    "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
			    "+---+---+---+\n";
	}
	void HS_Play()
	{
		int n;
		cout << "请玩家 " << name << " 输入下棋位置";
		cin >> n;
		if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
		{
			play[n - 1] = q2;
		}
		else
		{
			cout << "位置不合法请重新选择";
			cin >> n;
			if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
			{
				play[n - 1] = q2;
			}
			else
			{
				cout << "你又找茬是吧";
				exit(1);
			}
		}
		system("cls");
		cout << "+---+---+---+\n"
			    "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
		     	"+---+---+---+\n"
			    "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
		     	"+---+---+---+\n"
		    	"| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
		    	"+---+---+---+\n";
	}
	friend int Get_Q1(Player& p1);
	friend int Get_Q2(Player& p2);
private:
	string name;
	int a = 0;
	int xq = 0;
	char q1, q2;
};


int Get_Q1(Player& p1)
{
	return p1.q1;
}
int Get_Q2(Player& p2)
{
	return p2.q2;
}

int End()
{
	if (play[0] == play[1] && play[1] == play[2] && play[0] != ' ')
	{
		if (play[0] == 'X')
		{
			return 1;
		}
		if (play[0] == 'O')
		{
			return 2;
		}
	}
	if (play[3] == play[4] && play[4] == play[5] && play[3] != ' ')
	{
		if (play[3] == 'X')
		{
			return 1;
		}
		if (play[3] == 'O')
		{
			return 2;
		}
	}
	if (play[6] == play[7] && play[7] == play[8] && play[6] != ' ')
	{
		if (play[6] == 'X')
		{
			return 1;
		}
		if (play[6] == 'O')
		{
			return 2;
		}
	}

	if (play[0] == play[3] && play[3] == play[6] && play[0] != ' ')
	{
		if (play[0] == 'X')
		{
			return 1;
		}
		if (play[0] == 'O')
		{
			return 2;
		}
	}
	if (play[1] == play[4] && play[4] == play[7] && play[1] != ' ')
	{
		if (play[1] == 'X')
		{
			return 1;
		}
		if (play[1] == 'O')
		{
			return 2;
		}
	}
	if (play[2] == play[5] && play[5] == play[8] && play[2] != ' ')
	{
		if (play[1] == 'X')
		{
			return 1;
		}
		if (play[1] == 'O')
		{
			return 2;
		}
	}
	if (play[0] == play[4] && play[4] == play[8] && play[0] != ' ')
	{
		if (play[0] == 'X')
		{
			return 1;
		}
		if (play[0] == 'O')
		{
			return 2;
		}
	}
	if (play[2] == play[4] && play[4] == play[6] && play[2] != ' ')
	{
		if (play[2] == 'X')
		{
			return 1;
		}
		if (play[2] == 'O')
		{
			return 2;
		}
	}
	return 0;
}

int main()
{
	system();
	CSH();
	Player p1;
	Player p2;
	int xz;
	int j = 0;
	int xs = 0;
	p1.Name(1);
	p2.Name(2);
	while (xs == 0)
	{
		p1.Order();
		p2.Order();
		if (p1.PD() > p2.PD())
			cout << "\n玩家 ", p1.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 1;
		else if (p1.PD() < p2.PD())
			cout << "\n玩家 ", p2.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 2;
		else
			cout << "双方点数一样大请重新掷点数" << endl;
	}
	if (xs == 1)
	{
		p1.XQ_1(), p2.XQ_2();
	}
	else if (xs == 2)
	{
		p2.XQ_1(), p1.XQ_2();
	}
	CSH();
	while (j < 5)
	{
		if (xs == 1)
		{
			p1.XS_Play();
			End();
			if (End() != 0)
			{
				break;
			}
			p2.HS_Play();
			End();
			if (End() != 0)
			{
				break;
			}
		}
		if (xs == 2)
		{
			p2.XS_Play();
			End();
			if (End() != 0)
			{
				break;
			}
			p1.HS_Play();
			End();
			if (End() != 0)
			{
				break;
			}
		}
		j++;
	}
	system("cls");
	cout<<"******游戏结束******"<<endl;
	cout << "+---+---+---+\n"
		    "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
		    "+---+---+---+\n"
			"| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
		    "+---+---+---+\n"
		    "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
		    "+---+---+---+\n";
	if (End() == 1)
	{
		if (Get_Q1(p1) == 'x')
			cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利"<<endl;
		else
			cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利"<<endl;
	}
	if (End() == 2)
	{
		if (Get_Q1(p1) == 'O')
			cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利"<<endl;
		else
			cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利"<<endl;
	}

七、游戏后期处理

cout<<"------------------------------"<<endl; 
	cout<<"********是否继续游戏********"<<endl;
	cout<<"1.重新开始        2.退出游戏"<<endl;
	cin>>xz;
	switch(xz)
	{
		case 1:return_CSH(),main();break;
		case 2:return 0;break;
		default:cout<<"指令错误游戏终止";break; 
	}
}

源码

#include<iostream> 
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<ctime>
using namespace std;
char play[9] = { '1','2','3','4','5','6','7','8','9' };
void system()
{
	system("color FD");
	system("date/t");
	system("time/t");
	system("title 井字棋 Ver:1.0.0");
}

void CSH()
{
	cout << "######井字棋小游戏######" << endl;
	cout << "(*游戏规则先掷骰子选出优先下子者然后轮流下子任一方若有三子连成一线该方即获胜)" << endl;
	cout << "+---+---+---+\n"
		    "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
		    "+---+---+---+\n"
	     	"| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
	    	"+---+---+---+\n"
		    "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
		    "+---+---+---+\n"<<endl;
	cout<<"鸡你太美"<<endl;
	cout<<"你干嘛哎呦"<<endl;
	cout<<"-------------------------------------------------------"<<endl;
}
void return_CSH()
{
	memset(play,0,9);
	play[0] = '1';
	play[1] = '2';
	play[2] = '3';
	play[3] = '4';
	play[4] = '5';
	play[5] = '6';
	play[6] = '7';
	play[7] = '8';
	play[8] = '9';
}
class Player {
public:
	Player() {}
	void Name(int i)
	{
		cout << "请为玩家" << i << "设置玩家名称";
		cin >> name;
	}
	void Get_Name()
	{
		cout << name;
	}
	int Order()
	{
		cout << "请玩家 " << name << " 掷骰子";
		system("pause");
		srand((unsigned)time(NULL));
		a = rand() % 6 + 1;
		cout << a << endl;
		return a;
	}
	int PD()
	{
		return a;
	}
	void XQ_1()
	{
		cout << "******游戏设置******" << endl;
		cout << "   1.O        2.X   " << endl;
		cout << "请玩家 " << name << " 选择执什么棋(1 或者 2)";
		while (xq == 0)
		{
			cin >> xq;
			if (xq == 1)
				q1 = 'O';
			else if (xq == 2)
				q1 = 'X';
			else
				cout << "命令错误请重新输入合法选项";
		}
	}
	void XQ_2()
	{
		cout << "请玩家 " << name << " 选择执什么棋(1 或者 2)";
		while (xq == 0)
		{
			cin >> xq;
			if (xq == 1)
				q2 = 'O';
			else if (xq == 2)
				q2 = 'X';
			else
				cout << "命令错误请重新输入合法选项";
		}
	}
	void XS_Play()
	{
		int n;
		cout << "请玩家 " << name << " 输入下棋位置";
		cin >> n;
		if (play[n - 1] != 'O' && play[n - 1] != 'X'&&n<10)
		{
			play[n - 1] = q1;
		}
		else
		{
			cout << "位置不合法请重新选择";
			cin >> n;
			if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
			{
				play[n - 1] = q1;
			}
			else
			{
				cout << "我感觉你是故意的";
				exit(1);
			}
		}
		system("cls");
		cout << "+---+---+---+\n"
			    "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
			    "+---+---+---+\n"
			    "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
			    "+---+---+---+\n"
			    "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
			    "+---+---+---+\n";
	}
	void HS_Play()
	{
		int n;
		cout << "请玩家 " << name << " 输入下棋位置";
		cin >> n;
		if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
		{
			play[n - 1] = q2;
		}
		else
		{
			cout << "位置不合法请重新选择";
			cin >> n;
			if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
			{
				play[n - 1] = q2;
			}
			else
			{
				cout << "你又找茬是吧";
				exit(1);
			}
		}
		system("cls");
		cout << "+---+---+---+\n"
			    "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
		     	"+---+---+---+\n"
			    "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
		     	"+---+---+---+\n"
		    	"| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
		    	"+---+---+---+\n";
	}
	friend int Get_Q1(Player& p1);
	friend int Get_Q2(Player& p2);
private:
	string name;
	int a = 0;
	int xq = 0;
	char q1, q2;
};


int Get_Q1(Player& p1)
{
	return p1.q1;
}
int Get_Q2(Player& p2)
{
	return p2.q2;
}

int End()
{
	if (play[0] == play[1] && play[1] == play[2] && play[0] != ' ')
	{
		if (play[0] == 'X')
		{
			return 1;
		}
		if (play[0] == 'O')
		{
			return 2;
		}
	}
	if (play[3] == play[4] && play[4] == play[5] && play[3] != ' ')
	{
		if (play[3] == 'X')
		{
			return 1;
		}
		if (play[3] == 'O')
		{
			return 2;
		}
	}
	if (play[6] == play[7] && play[7] == play[8] && play[6] != ' ')
	{
		if (play[6] == 'X')
		{
			return 1;
		}
		if (play[6] == 'O')
		{
			return 2;
		}
	}

	if (play[0] == play[3] && play[3] == play[6] && play[0] != ' ')
	{
		if (play[0] == 'X')
		{
			return 1;
		}
		if (play[0] == 'O')
		{
			return 2;
		}
	}
	if (play[1] == play[4] && play[4] == play[7] && play[1] != ' ')
	{
		if (play[1] == 'X')
		{
			return 1;
		}
		if (play[1] == 'O')
		{
			return 2;
		}
	}
	if (play[2] == play[5] && play[5] == play[8] && play[2] != ' ')
	{
		if (play[1] == 'X')
		{
			return 1;
		}
		if (play[1] == 'O')
		{
			return 2;
		}
	}
	if (play[0] == play[4] && play[4] == play[8] && play[0] != ' ')
	{
		if (play[0] == 'X')
		{
			return 1;
		}
		if (play[0] == 'O')
		{
			return 2;
		}
	}
	if (play[2] == play[4] && play[4] == play[6] && play[2] != ' ')
	{
		if (play[2] == 'X')
		{
			return 1;
		}
		if (play[2] == 'O')
		{
			return 2;
		}
	}
	return 0;
}

int main()
{
	system();
	CSH();
	Player p1;
	Player p2;
	int xz;
	int j = 0;
	int xs = 0;
	p1.Name(1);
	p2.Name(2);
	while (xs == 0)
	{
		p1.Order();
		p2.Order();
		if (p1.PD() > p2.PD())
			cout << "\n玩家 ", p1.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 1;
		else if (p1.PD() < p2.PD())
			cout << "\n玩家 ", p2.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 2;
		else
			cout << "双方点数一样大请重新掷点数" << endl;
	}
	if (xs == 1)
	{
		p1.XQ_1(), p2.XQ_2();
	}
	else if (xs == 2)
	{
		p2.XQ_1(), p1.XQ_2();
	}
	CSH();
	while (j < 5)
	{
		if (xs == 1)
		{
			p1.XS_Play();
			End();
			if (End() != 0)
			{
				break;
			}
			p2.HS_Play();
			End();
			if (End() != 0)
			{
				break;
			}
		}
		if (xs == 2)
		{
			p2.XS_Play();
			End();
			if (End() != 0)
			{
				break;
			}
			p1.HS_Play();
			End();
			if (End() != 0)
			{
				break;
			}
		}
		j++;
	}
	system("cls");
	cout<<"******游戏结束******"<<endl;
	cout << "+---+---+---+\n"
		    "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
		    "+---+---+---+\n"
			"| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
		    "+---+---+---+\n"
		    "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
		    "+---+---+---+\n";
	if (End() == 1)
	{
		if (Get_Q1(p1) == 'x')
			cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利"<<endl;
		else
			cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利"<<endl;
	}
	if (End() == 2)
	{
		if (Get_Q1(p1) == 'O')
			cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利"<<endl;
		else
			cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利"<<endl;
	}
	cout<<"------------------------------"<<endl; 
	cout<<"********是否继续游戏********"<<endl;
	cout<<"1.重新开始        2.退出游戏"<<endl;
	cin>>xz;
	switch(xz)
	{
		case 1:return_CSH(),main();break;
		case 2:return 0;break;
		default:cout<<"指令错误游戏终止";break; 
	}
}

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: c++

“C++游戏game | 井字棋游戏坤坤版(配资源+视频)【赋源码,双人对战】” 的相关文章