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

C++井字棋(人机对战)

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

C++井字棋(人机对战)

直接上代码了

玩家自己输出位置

电脑的话用随机数控制(简称人工智障)

#include
#include
using namespace std;
void choose();            //电脑或者玩家先手判断
void chessboard();        //打印棋盘
void start(int x);        //游戏开始
void judge();             //判断胜负
void menu();              //打印菜单
char chess[3][3] =
{
	{'  ','  ','  '},
	{'  ','  ','  '},
	{'  ','  ','  '}
};
void menu()
{
	for (int i = 0; i < 26; i++)
		cout << "*";
	cout << endl;
	cout << "**t1.开始游戏t**" << endl;
	cout << "**t0.退出游戏t**" << endl;
	for (int i = 0; i < 26; i++)
	{
		cout << "*";
	}
	cout << endl;
	int n;
	cin >> n;
	if (n == 1)
	{
		chessboard();
		choose();
	}
	else exit(0);
}
void chessboard()
{
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << chess[i][j];
			if (j < 2)
			{
				cout << " | ";
			}
		}
		cout << "n";
		if (i < 2)
		{
			cout << "—————" << endl;
		}
	}
	cout << "n";
}
void choose()
{
	int x;
	cout << "请选择先手(1 - 电脑  0 - 玩家)" << endl;
	cin >> x;
	int z = 0;
	if (x == 1)
		start(x);
	else if (x == 0)
		start(x);
}
void start(int x)
{
	bool flag = x;//flag=0,为人,反之则为电脑
	int m, n;
	if (flag == 0)
	{
	flag0:
		cout << "请输入横坐标1-3,空格隔开后输入纵坐标1-3" << endl;
		cin >> m >> n;
		while (chess[m - 1][n - 1] != ' ')
		{
			cout << "非法输入,请重新输入" << endl;
			cin >> m >> n;
		}
		chess[m - 1][n - 1] = 'X';//人画X,电脑画O
		chessboard();
		judge();
		goto flag1;
	}
	if (flag == 1)
	{
	flag1:
		srand((unsigned)time(NULL));
		int p = rand() % 3 + 1;
		int q = rand() % 3 + 1;
		if (chess[p - 1][q - 1] != ' ')
		{
			goto flag1;
		}
		chess[p - 1][q - 1] = 'O';
		chessboard();
		judge();
		goto flag0;
	}
}
void judge()
{
	for (int h = 0; h < 3; h++)
	{
		if (chess[h][0] == chess[h][1] && chess[h][1] == chess[h][2] && chess[h][0] != ' ')
		{
			if (chess[h][0] == 'X')			//横向判断
			{
				cout << "!!玩家获胜!!" << endl;
				system("pause");
				menu();
			}
			else if (chess[h][0] == 'O')
			{
				cout << "!!电脑获胜!!" << endl;
				system("pause");
				menu();
			}
		}
		if (chess[0][h] == chess[1][h] && chess[1][h] == chess[2][h] && chess[2][h] != ' ')
		{
			if (chess[0][h] == 'X')			//纵向判断
			{
				cout << "!!玩家获胜!!" << endl;
				system("pause");
				menu();
			}
			else if (chess[0][h] == 'O')
			{
				cout << "!!电脑获胜!!" << endl;
				system("pause");
				menu();
			}
		}
		if ((chess[0][0] == chess[1][1] && chess[1][1] == chess[2][2] && chess[2][2] != ' ') ||
			(chess[2][0] == chess[1][1] && chess[1][1] == chess[1][2] && chess[1][2] != ' '))
		{
			if (chess[1][1] == 'X')
			{
				cout << "!!玩家获胜!!" << endl;
				system("pause");
				menu();
			}
			if (chess[1][1] == 'O')
			{
				cout << "!!电脑获胜!!" << endl;
				system("pause");
				menu();
			}
		}
	}
	if (chess[0][0] != ' ' && chess[0][1] != ' ' && chess[0][2] != ' '
		&& chess[1][0] != ' ' && chess[1][1] != ' ' && chess[1][2] != ' '
		&& chess[2][0] != ' ' && chess[2][1] != ' ' && chess[2][2] != ' ')
	{
		cout << "平局" << endl;
		system("pause");
		menu();
	}
}
int main()
{
	menu();
	return 0;
}

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

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

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