【c语言五子棋】简单ai算法初步(实际)

系列目录

目录

一、算法描述

二、代码实现


一、算法描述

本文只介绍作为在在五子棋的条件情况下(机器白子后手,防守策略大于进攻,无禁手,不具有随机性)ai的落子思路

1.评估函数

评估函数即对棋盘上每一个子进行活1,活2,活3,活4、死2,死3,死4的加权评估,最后排序落子位置即为分数最大值对应位置

【c语言五子棋】简单ai算法初步(实际)_第1张图片【c语言五子棋】简单ai算法初步(实际)_第2张图片

 如图如测试代码所示,每轮会进行棋局判定,分数高的位置即为落子位置。

2.对井字棋的特殊处理

井字棋的机器落子规则相比一般规则有所变化,因其后手故第一个落子尤为关键

【c语言五子棋】简单ai算法初步(实际)_第3张图片

 在井字棋中边角子>中间子>其他子。故我们对其简单处理即可确保后手的机器不输棋局。

代码如下:

int ai3()
{
	if (map[0][0] == 0)
	{
		map[0][0] = 2;
		return 1;
	}
	if (map[1][1] == 0)
	{
		map[1][1] = 2;
		return 1;
	}
	return 0;
}

运行结果:

【c语言五子棋】简单ai算法初步(实际)_第4张图片

二、代码实现

int aibeside(int x, int y)
{
	if (map[x + 1][y] == 1 || map[x - 1][y] == 1 || map[x][y + 1] == 1 || map[x][y - 1] == 1)return 2;
	if (map[x + 1][y + 1] == 1 || map[x - 1][y + 1] == 1 || map[x - 1][y - 1] == 1 || map[x + 1][y - 1] == 1)return 1;
	return 0;
}

int aibeside2(int x, int y)
{
	if (map[x + 1][y] == 1 && map[x + 2][y] == 1)return 10;
	if (map[x - 1][y] == 1 && map[x - 2][y] == 1)return 10;
	if (map[x][y + 1] == 1 && map[x][y + 2] == 1)return 10;
	if (map[x][y - 1] == 1 && map[x][y - 2] == 1)return 10;

	if (map[x + 1][y + 1] == 1 && map[x + 2][y + 2] == 1)return 10;
	if (map[x - 1][y + 1] == 1 && map[x - 2][y + 2] == 1)return 10;
	if (map[x + 1][y - 1] == 1 && map[x + 2][y - 2] == 1)return 10;
	if (map[x - 1][y - 1] == 1 && map[x - 2][y - 2] == 1)return 10;
	return 0;
}

int aibeside3(int x, int y)
{
	if (map[x + 1][y] == 1 && map[x + 2][y] == 1 && map[x + 3][y] == 1)return 100;
	if (map[x - 1][y] == 1 && map[x - 2][y] == 1 && map[x - 3][y] == 1)return 100;
	if (map[x][y + 1] == 1 && map[x][y + 2] == 1 && map[x][y + 3] == 1)return 100;
	if (map[x][y - 1] == 1 && map[x][y - 2] == 1 && map[x][y - 3] == 1)return 100;

	if (map[x - 1][y - 1] == 1 && map[x - 2][y - 2] == 1 && map[x - 3][y - 3] == 1)return 100;
	if (map[x - 1][y + 1] == 1 && map[x - 2][y + 2] == 1 && map[x - 3][y + 3] == 1)return 100;
	if (map[x + 1][y - 1] == 1 && map[x + 2][y - 2] == 1 && map[x + 3][y - 3] == 1)return 100;
	if (map[x + 1][y + 1] == 1 && map[x + 2][y + 2] == 1 && map[x + 3][y + 3] == 1)return 100;
	return 0;
}

int aibeside4(int x, int y)
{
	if (map[x + 1][y] == 1 && map[x + 2][y] == 1 && map[x + 3][y] == 1 && map[x + 4][y] == 1)return 1000;
	if (map[x - 1][y] == 1 && map[x - 2][y] == 1 && map[x - 3][y] == 1 && map[x - 4][y] == 1)return 1000;
	if (map[x][y + 1] == 1 && map[x][y + 2] == 1 && map[x][y + 3] == 1 && map[x][y + 4] == 1)return 1000;
	if (map[x][y - 1] == 1 && map[x][y - 2] == 1 && map[x][y - 3] == 1 && map[x][y - 4] == 1)return 1000;

	if (map[x - 1][y - 1] == 1 && map[x - 2][y - 2] == 1 && map[x - 3][y - 3] == 1 && map[x - 4][y - 4] == 1)return 100;
	if (map[x - 1][y + 1] == 1 && map[x - 2][y + 2] == 1 && map[x - 3][y + 3] == 1 && map[x - 4][y + 4] == 1)return 100;
	if (map[x + 1][y - 1] == 1 && map[x + 2][y - 2] == 1 && map[x + 3][y - 3] == 1 && map[x + 4][y - 4] == 1)return 100;
	if (map[x + 1][y + 1] == 1 && map[x + 2][y + 2] == 1 && map[x + 3][y + 3] == 1 && map[x + 4][y + 4] == 1)return 100;
	return 0;
}

int corner(int x, int y)
{
	if (x == 0 || y == 0 || x == length - 1 || y == length - 1)return -1;
	return 0;
}

int attack(int x, int y)
{
	if (map[x + 1][y] == 2 && map[x + 2][y] == 2 && map[x + 3][y] == 2)return 1000;
	if (map[x - 1][y] == 2 && map[x - 2][y] == 2 && map[x - 3][y] == 2)return 1000;
	if (map[x][y + 1] == 2 && map[x][y + 2] == 2 && map[x][y + 3] == 2)return 1000;
	if (map[x][y - 1] == 2 && map[x][y - 2] == 2 && map[x][y - 3] == 2)return 1000;

	if (map[x - 1][y - 1] == 2 && map[x - 2][y - 2] == 2 && map[x - 3][y - 3] == 2)return 1000;
	if (map[x - 1][y + 1] == 2 && map[x - 2][y + 2] == 2 && map[x - 3][y + 3] == 2)return 1000;
	if (map[x + 1][y - 1] == 2 && map[x + 2][y - 2] == 2 && map[x + 3][y - 3] == 2)return 1000;
	if (map[x + 1][y + 1] == 2 && map[x + 2][y + 2] == 2&& map[x + 3][y + 3] == 2)return 1000;
	return 0;
}

int score[15][15];
int ai()
{
	if (ai3() == 1)return 0;
	//计分归零
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < length; j++)
		{
			score[i][j] = 0;
		}
	}
	//每个子计分
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < length; j++)
		{
			if (map[j][i] == 2)
			{
				score[j][i] = -1;
				continue;
			}

			if (map[j][i] == 1)
			{
				score[j][i] = -1;
				continue;
			}

			score[j][i] += aibeside(j, i);
			score[j][i] += aibeside2(j, i);
			score[j][i] += aibeside3(j, i);
			score[j][i] += aibeside4(j, i);
			score[j][i] += corner(j, i);
			score[j][i] += attack(j, i);
		}
	}

back:
	int maxx = 0, maxy = 0, maxscore = 0;
	for (int j = 0; j < length; j++)
	{
		for (int i = 0; i < length; i++)
		{
			if (score[j][i] > maxscore)
			{
				maxscore = score[j][i];
				maxx = j;
				maxy = i;
			}
		}
	}
	if (map[maxx][maxy] != 0)
	{
		score[maxx][maxy] = 0; goto back;
	}
	map[maxx][maxy] = 2;
	positionx = maxx;
	positiony = maxy;
	IsBlack = 1;
	return 0;
}

int ai3()
{
	if (map[0][0] == 0)
	{
		map[0][0] = 2;
		return 1;
	}
	if (map[1][1] == 0)
	{
		map[1][1] = 2;
		return 1;
	}
	return 0;
}

你可能感兴趣的:(小项目,c语言)