人输入坐标玩五子棋(与电脑对抗)

人输入坐标,与电脑对抗(电脑是随机下子的,没实现人工智能)

代码如下:

#include <iostream>
//设置随机数的头文件
#include <cstdlib>
#include <ctime>
using namespace std;

const int length = 7;   //全局变量,棋盘的长与宽,可调整
int function(char a[length][length],char letter);   //函数原型声明

int main()
{
	cout << "---Game of Chess---" << endl << endl;
	cout << "Human against the Computer" << endl;

	char a[length][length];
	int i, j, h1, h2, judge;
	char letter;

	//给二维数组赋*
	for (i = 0; i<length; ++i)
	{
		for (j = 0; j<length; ++j)
		{
			a[i][j] = '*';
		}
	}

	//输出棋盘
	for (i = 0; i<length; ++i)
	{
		for (j = 0; j<length; ++j)
		{
			cout << a[i][j] << '\t';
			if (j == 6) cout << endl << endl;
		}
	}

number1:
	cout << "Enter X,Y coordinates for your move:";
	
	//人下子
	cin >> h1 >> h2;
	a[h1 - 1][h2 - 1] = 'H';

	//注意,每下完一步就要判断是否赢
	
	//1.判断人是否赢
	letter = 'H';   //标识符,让函数知道判断的是谁
	judge = function(a, letter);   //注意传递数组时,不用写数组长宽,直接写数组名即可
	if (judge==1)
	{
		//若赢,打印出最终的棋盘
		for (i = 0; i<length; ++i)
		{
			for (j = 0; j<length; ++j)
			{
				cout << a[i][j] << '\t';
				if (j == 6) cout << endl << endl;
			}
		}
		cout << endl << "Human win!" << endl;
		system("pause");
		return 0;   //若赢,程序结束
	}
	
	//电脑下子
number0:
	srand((unsigned)time(NULL));
	h1 = rand() % length ;
	h2 = rand() % length ;   //因为不会写人工智能,所以用随机数下子
	
	cout << h1 << " " << h2 << endl;
	
	if (a[h1][h2] == 'H') goto number0;   //注意不能用a[i - 1][j - 1],因为若有0,则变成负数了
	else if (a[h1][h2] == 'C') goto number0;   //注意不能写成if (a[h1][h2] == 'H'|'C'),因为会短路求值
	else a[h1][h2] = 'C';

	//2.判断电脑是否赢
	letter = 'C';
	judge = function(a, letter);
	if (judge == 1)
	{
		for (i = 0; i<length; ++i)
		{
			for (j = 0; j<length; ++j)
			{
				cout << a[i][j] << '\t';
				if (j == 6) cout << endl << endl;
			}
		}
		cout << endl << "Computer win!" << endl;
		system("pause");
		return 0;  
	}
	
	//若未分出胜负,先打印这一次的棋盘结果,再goto回循环
	for (i = 0; i<length; ++i)
	{
		for (j = 0; j<length; ++j)
		{
			cout << a[i][j] << '\t';
			if (j == 6) cout << endl << endl;
		}
	}
	goto number1;

	return 0;
}

int function(char a[length][length], char letter)
{
	int i, j;
	const char character=letter;

	//情况1:横着
	for (i = 0; i<length; ++i)
	{
		for (j = 0; j<length; ++j)
		{
			if (a[i][j] == '*') break;   //没下过的地方不予考虑
			if (j > length - 5) break;   //那行剩下的不足以凑成5子,故跳出
			if ((a[i][j] == character)&(a[i][j + 1] == character)&(a[i][j + 2] == character)&(a[i][j + 3] == character)&(a[i][j + 4] == character)) return 1;
		}
	}

	//情况2:竖着
	for (j = 0; j<length; ++j)
	{
		for (i = 0; i<length; ++i)
		{
			if (a[i][j] == '*') break;   //没下过的地方不予考虑
			if (i > length - 5) break;   //那列剩下的不足以凑成5子,故跳出
			if ((a[i][j] == character)&(a[i + 1][j] == character)&(a[i + 2][j] == character)&(a[i + 3][j] == character)&(a[i + 4][j] == character)) return 1;
		}
	}
	
	//情况3:右下斜着
	for (i = 0; i<length; ++i)
	{
		for (j = 0; j<length; ++j)
		{
			if (a[i][j] == '*') break;   //没下过的地方不予考虑
			if ((i + 4 >= length)|(j + 4 >= length)) break;   //排除超出的情况
			if ((a[i][j] == character)&(a[i + 1][j + 1] == character)&(a[i + 2][j + 2] == character)&(a[i + 3][j + 3] == character)&(a[i + 4][j + 4] == character)) return 1;
		}
	}
	
	//情况4:左上斜着
	for (i = 6; i>=0; --i)
	{
		for (j = 0; j<length; ++j)
		{
			if (a[i][j] == '*') break;   //没下过的地方不予考虑
			if ((i - 4 <= 0) | (j + 4 >= length)) break;   //排除超出的情况
			if ((a[i][j] == character)&(a[i - 1][j + 1] == character)&(a[i - 2][j + 2] == character)&(a[i - 3][j + 3] == character)&(a[i - 4][j + 4] == character)) return 1;
		}
	}

	return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值