基于c语言的象棋游戏-附带人机算法

2.1 设计流程

进入象棋游戏后,输出开始界面(直接调用图片如图2.1所示)根据玩家的选择(鼠标点击的位置)进入不同模式,双人模式中,根据鼠标点击移动棋子,人机模式中玩家移动之后,电脑自动选择路径并移动棋子。

基于c语言的象棋游戏-附带人机算法_第1张图片

                                                    图2.1 开始界面展示 

输出开始界面,向玩家提供双人模式/人机模式选择。

2.3.1 棋牌展示

 输出棋盘背景,包含方格线、“楚河”、“汉界”等如图2.2。

基于c语言的象棋游戏-附带人机算法_第2张图片

                                                  图 2.2 棋盘背景

2.3.2 棋子展示

    按照象棋棋子的摆法,在指定坐标出画出棋子。

2.3.3 各棋子子移动

俥,马,象,士,炮,将,兵分别写移动规则[3](根据起始坐标和目的坐标判断是否可以移动)。

2.3.4 判断胜负

棋盘信息没改变一次就扫描一次棋盘,如果一方的将被覆盖(被对方所吃),则输出另一方获胜。游戏结束。

2.4 人机模块

2.4.1玩家移动

同双人模式的玩家移动一样,利用鼠标信息进行判断和移动。

2.4.2 虚拟棋盘

建立虚拟棋盘,棋盘信息与实际棋盘一致、并随着实际棋盘信息同步改变。

2.4.3 遍历当前虚拟棋盘

遍历棋盘,找到每一颗黑方的棋子,查询该棋子是否下一步可能被玩家吃掉,如果是则加权,并找到每一种可行的路径。

2.4.4 模拟行棋

利用虚拟棋盘按照找到的路径去进行模拟棋子移动,查询在移动的时候是否能吃掉红方棋子,如果是则加权。

2.4.5 遍历模拟之后的棋盘

在虚拟棋盘棋子移动的之后,遍历棋盘,判断当前状态下,当红方行棋时,自己可能被吃掉那些棋子,并做加权。

2.4.6 选择每颗棋子的最优路径

通过三次加权后,得分最高的路径作为该棋子的最优路径。

2.4.7 恢复虚拟棋盘

选择完一颗黑方棋子的最优路径后,使虚拟棋盘恢复,继续选择下一个棋子的最优路径。

2.4.8 最终移动选择

通过比较每颗棋子的加权分,得到最优棋子和该棋子的最优路径。在实际棋盘上移动该棋子。

2.4.9 判断胜负

棋盘信息没改变一次就扫描一次棋盘,如果一方的将被覆盖,则输出另一方获胜。游戏结束。

代码运行流程:

基于c语言的象棋游戏-附带人机算法_第3张图片

 完整代码:

#include
#include
#include
void execute(int a, int b, int c, int d);
bool jiang(int a, int b, int  c, int d);
bool pao(int a, int b, int c, int d);
bool ma(int a, int b, int c, int d);
bool AIifdown(int a, int b,int c,int d);
bool bing(int a, int b, int c, int d);
float ALLifdown();
float  ALifdowntow();
float  AIpower(int a, int b);
void llll();
void AIControl();//获取鼠标点击信息并完响应
IMAGE img;
#define distance  35
#define   longth  65
#define   high   61
struct AIcoord {
	int beginx;
	int beginy;
	int endx;
	int endy;
	float power;
};
struct AIcoord AIblack[20];//将:100;俥:8;炮:6;马:4;象:3;兵:3,仕:2;
struct movecoordinate
{
	long x;
	long y;
};
struct  Chesscoordinate//棋子综合信息
{
	int x;
	int y;
	DWORD type;  //颜色
	bool river;//是否过河
	int id;
};

enum pieces
{
	SPACE = -1,
	車, 馬, 象, 士, 将, 炮, 卒,
	车, 马, 相, 仕, 帥, 砲, 兵,
	BEGIN, END,

};
enum pieces redchess[] = { 車,馬,象,士,将,炮,卒, };
enum pieces blackchess[] = { 车,马,相,仕,帥,砲,兵, };
enum pieces state = BEGIN;
struct move {  //鼠标选中的棋子
	int beginx;
	int beginy;
	int endx;
	int endy;
	int state;
}moving = { -1,-1,-1,-1,BEGIN };
const char* chessname[] = { "車","馬","象","士","将","炮","卒","车","马","相","仕","帥","砲","兵", };
struct Chesscoordinate  map[9][10];//坐标
struct Chesscoordinate  AImap[9][10];//坐标
movecoordinate begin = { -1,-1 }, end = { -1,-1 };
int  xx(int a)//数组下标转换成坐标
{
	a = distance + longth * a;
	return a;
}
int  yy(int a)
{
	a = distance + high * a;
	return a;
}
void AIfollowchess()
{
	for (int i = 0; i <= 8; i++)
	{
		for (int j = 0; j <= 9; j++)
		{
			AImap[i][j].id = map[i][j].id;
			AImap[i][j].river = map[i][j].river;
			AImap[i][j].type = map[i][j].type;
			AImap[i][j].x = map[i][j].x;
			AImap[i][j].y = map[i][j].y;
		}
	}
}
void begining()
{
	loadimage(&img, "C:/Users/ASUS/Desktop/shess/66.jpg");
	initgraph(img.getwidth(), img.getheight(), 1);
	putimage(0, 0, &img);//输出开始界面;

}
void  qiban()
{
	loadimage(&img, "C:/Users/ASUS/Desktop/shess/5.jpg");
	initgraph(img.getwidth(), img.getheight(), 1);
	putimage(0, 0, &img);//输出棋盘
}
void  coord()//棋子信息赋值
{
	loadimage(&img, "C:/Users/ASUS/Desktop/shess/5.jpg");
	putimage(0, 0, &img);//输出棋盘

	for (int i = 0; i <= 8; i++)
	{

		for (int j = 0; j <= 9; j++)//遍历二维数组
		{
			enum pieces chessid = SPACE;//先把全部位置的id赋值为SAPCE
			DWORD  chesstype;//定义颜色中间变量
			if (j <= 4)
			{
				chesstype = BLACK;//黑方
				if (j == 0)
				{
					if (i <= 4)
					{
						chessid = blackchess[i];//
					}
					else
					{
						chessid = blackchess[8 - i];
					}

				}
				if (j == 2 && (i == 1 || i == 7))
				{
					chessid = blackchess[5];

				}

				if (j == 3 && (i == 0 || i == 2 || i == 4 || i == 4 || i == 6 || i == 8))
				{
					chessid = blackchess[6];
				}

			}
			else
			{
				chesstype = RED;//红方
				if (j == 6 && (i == 0 || i == 2 || i == 4 || i == 6 || i == 8))
				{
					chessid = redchess[6];
				}
				if (j == 7 && (i == 1 || i == 7))
				{
					chessid = redchess[5];

				}

				if (j == 9)
				{

					if (i <= 4)
					{
						chessid = redchess[i];
					}
					else
					{
						chessid = redchess[8 - i];
					}
				}

			}//依次赋值
			map[i][j].id = chessid;
			map[i][j].river = false;
			map[i][j].type = chesstype;
			map[i][j].x = distance + longth * i;
			map[i][j].y = distance + high * j;
			AIfollowchess();


		}
	}
	for (int i = 0; i <= 8; i++)
	{
		for (int j = 0; j <= 9; j++)
		{
			if (map[i][j].id == SPACE)
			{
				map[i][j].type = YELLOW;
			}
		}
	}

}
void getbackground() //画棋子
{
	int x_start, y_start;
	x_start = distance;
	y_start = distance;



	settextstyle(30, 0, "黑体");//棋子大小颜色
	setbkmode(0);
	for (int i = 0; i <= 8; i++)
	{

		for (int j = 0; j <= 9; j++)
		{

			if (map[i][j].id != SPACE)//在棋盘上输出
			{
				setfillcolor(RGB(253, 216, 161));
				//	setlinestyle(BLACK);
				settextcolor(map[i][j].type);
				fillcircle(map[i][j].x, map[i][j].y, 24);
				fillcircle(map[i][j].x, map[i][j].y, 18);
				outtextxy(map[i][j].x - 13, map[i][j].y - 13, chessname[map[i][j].id]);

			}
		}
	}

}
void movechess(int a, int b, int c, int d)//移动棋子,改变其坐标
{

	map[c][d].id = map[a][b].id;
	map[c][d].river = map[a][b].river;
	map[c][d].type = map[a][b].type;
	map[c][d].x = xx(c);
	map[c][d].y = yy(d);
	map[a][b].id = SPACE;
	map[a][b].type = YELLOW;
	AIfollowchess();

}
void AImovechess(int a, int b, int c, int d)//移动棋子,改变其坐标
{

	AImap[c][d].id = map[a][b].id;
	AImap[c][d].river = map[a][b].river;
	AImap[c][d].type = map[a][b].type;
	AImap[c][d].x = c;
		AImap[c][d].y = d;
	AImap[a][b].id = SPACE;
	AImap[a][b].type = YELLOW;

}
void MouseControl()//获取鼠标点击信息并完响应
{
	//getbackground();
	if (MouseHit())
	{

		float beginrow, beginrol, endrow, endrol;//第一次按下时的坐标
		int intbeginrow, intbeginrol, intendrow, intendrol;//第二次按下时的坐标
		MOUSEMSG msg = GetMouseMsg();
		if (msg.uMsg == WM_LBUTTONDOWN)
		{
			//获取鼠标点击的数组的下标
		//	printf("(%d,%d)", msg.x, msg.y);
			beginrow = (float)(msg.x - distance) / longth;
			beginrol = (float)(msg.y - distance) / high;
			intbeginrow = round(beginrow);
			intbeginrol = round(beginrol);
			if (moving.state == BEGIN)
			{
				moving.state = END;
				moving.beginx = intbeginrow;
				moving.beginy = intbeginrol;
				//	printf("(%d,%d)  \n", moving.beginx, moving.beginy);
			}
			else if (moving.state == END)
			{



				moving.state = BEGIN;
				moving.endx = intbeginrow;
				moving.endy = intbeginrol;
				
				execute(moving.beginx, moving.beginy, moving.endx, moving.endy);
			
				
			}


		}

	}



}
int  MouseControlone()//获取鼠标点击信息并完响应
{
	if (MouseHit())
	{

		MOUSEMSG msg = GetMouseMsg();
		if (msg.uMsg == WM_LBUTTONDOWN)
		{
			//printf("(%d,%d)", msg.x, msg.y);
			//获取鼠标点击的数组的下标
			if (msg.y > 550 && msg.y < 617)
			{
				if (msg.x > 160 && msg.x < 280)
				{
					return 1;
				}
				else if (msg.x > 302 && msg.x < 414)
				{
					return 2;
				}
				else
				{
					return  3;
				}
			}
			else
			{
				return 4;
			}
		}

	}
	else
	{
		return 0;
	}
	
}
void  AIControl()//获取鼠标点击信息并完响应
{
	//getbackground();
	if (MouseHit())
	{

		float beginrow, beginrol, endrow, endrol;//第一次按下时的坐标
		int intbeginrow, intbeginrol, intendrow, intendrol;//第二次按下时的坐标
		MOUSEMSG msg = GetMouseMsg();
		if (msg.uMsg == WM_LBUTTONDOWN)
		{
			//获取鼠标点击的数组的下标
		//	printf("(%d,%d)", msg.x, msg.y);
			beginrow = (float)(msg.x - distance) / longth;
			beginrol = (float)(msg.y - distance) / high;
			intbeginrow = round(beginrow);
			intbeginrol = round(beginrol);
			if (moving.state == BEGIN)
			{
				moving.state = END;
				moving.beginx = intbeginrow;
				moving.beginy = intbeginrol;

				//	printf("(%d,%d)  \n", moving.beginx, moving.beginy);
			}
			else if (moving.state == END)
			{

				moving.state = BEGIN;
				moving.endx = intbeginrow;
				moving.endy = intbeginrol;
				if (map[moving.beginx][moving.beginy].type == RED)
				{
					execute(moving.beginx, moving.beginy, moving.endx, moving.endy);
					llll();
				}
				else
				{
					printf("请选择您的棋子!\n");
				}

				
			}


		}

	}



}
int  win()
{
	int redgeneral = 0;
	int blackgeneral = 0;
	for (int i = 0; i <= 8; i++)
	{
		for (int j = 0; j <= 9; j++)
		{
			if (map[i][j].id == blackchess[4])
			{
				blackgeneral++;
			}
			else if (map[i][j].id == redchess[4])
			{
				redgeneral++;
			}
			else
			{
				blackgeneral = blackgeneral;
				redgeneral = redgeneral;
			}
		}
	}

	//printf("%d %d\n", blackgeneral, redgeneral);



	if (blackgeneral == 0)
	{
		return 0;

	}
	else if (redgeneral == 0)
	{

		return 1;
	}
	else
	{
		return 2;
	}


}
bool jiang(int a, int b, int  c, int d)//判断是否只移动了一格(将军、兵的规则)
{

	float h;
	h = sqrt(abs(d - b)*abs(d - b) + abs(c - a)*abs(c - a));
	if (b<4&&c > 2 && c < 6 && d < 3)
	{
		if (h == 1 && map[c][d].type != map[a][b].type)
			return true;
		else
			return false;
	}
	if (b > 4 && c > 2 && c < 6 && d >6)
	{
		if (h == 1 && map[c][d].type != map[a][b].type)
			return true;
		else
			return false;
	}
	else
	{
		return false;
	}
	
}
bool bing(int a, int b, int c, int d)
{
	float h;
	h = sqrt(abs(d - b)*abs(d - b) + abs(c - a)*abs(c - a));
	if (map[a][b].type == BLACK)
	{
		if (map[a][b].river == false)
		{
			if (d == b + 1 && h == 1 && map[c][d].type != map[a][b].type)
			{
				return true;

			}
			else
			{
				return false;
			}
		}
		else
		{
			if (d >= b && h == 1 && map[c][d].type != map[a][b].type)
			{
				return true;

			}
			else
			{
				return false;
			}
		}
	}
	else if(map[a][b].type == RED)
	{
		if (map[a][b].river == false)
		{
			if (d == b - 1 && h == 1 && map[c][d].type != map[a][b].type)
			{
				return true;

			}
			else
			{
				return false;
			}
		}
		else
		{
			if (d <= b && h == 1 && map[c][d].type != map[a][b].type)
			{
				return true;

			}
			else
			{
				return false;
			}
		}
	}
	else
	{
		return false;
	}
}
bool pao(int a, int b, int c, int d)//炮的移动
{
	if (c == a && d != b)
	{
		int time = 0;
		int max = d > b ? d : b;
		int min = b < d ? b : d;
		for (int i = min; i <= max; i++)
		{
			if (map[c][i].id != SPACE)
			{
				time++;
			}

		}
		//	printf("%d\n", time);
		if (map[c][d].id == SPACE)
		{
			if (time == 1)
				return  true;
			else
				return false;
		}
		if (map[c][d].id != SPACE)
		{

			if (time != 3)
			{
				return false;
			}
			else
			{
				if (map[c][d].type == map[a][b].type)
				{
					return false;
				}
				else
				{
					return true;
				}
			}


		}

	}
	else if (d == b && c != a)
	{
		int time = 0;
		int max = a > c ? a : c;
		int min = c < a ? c : a;
		for (int i = min; i <= max; i++)
		{
			if (map[i][d].id != SPACE)
			{
				time++;
			}
		}
		//	printf("%d\n", time);
		if (map[c][d].id == SPACE)
		{
			if (time == 1)
				return  true;
			else
				return false;
		}
		if (map[c][d].id != SPACE)
		{

			if (time != 3)
			{
				return false;
			}
			else 
			{
				if (map[c][d].type == map[a][b].type)
				{
					return false;
				}
				else
				{
					return true;
				}
			}


		}
	}
	else
	{
		return false;
	}
}
bool che(int a, int b, int c, int d)
{
 
	if (c == a && d != b)//是否为直线
	{
		
		int time = 0;
		int max = d > b ? d : b;
		int min = b < d ? b : d;
		for (int i = min; i <= max; i++)//遍历路径
		{

			
			if (map[c][i].id != SPACE)
			{
				time++;
			}
			

		}
		//	printf("%d", time);
		if (time == 1)//车移动不吃棋子
		{
			return true;
			
		}
	  if (time == 2)//车移动并且吃目的坐标的棋子
		{
			if (map[c][d].type == map[a][b].type)//如果是目的坐标是自己的棋子,则返回false
			{
				return false;
			}
			if (map[c][d].type == YELLOW)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			return false;
		}
	}
	else	if (d == b && c != a)
	{
		int time = 0;
		int max = c > a ? c : a;
		int min = a < c ? a : c;
		for (int i = min; i <= max; i++)//遍历路径
		{
			if (map[i][d].id != SPACE)
			{
				time++;
			}

		}
		//	printf("%d", time);
		if (time == 1)//车是否车跳棋
		{
			return true;
		}
		else if (time == 2)
		{
			if (map[c][d].type == map[a][b].type)
			{
				return false;
			}
			if (map[c][d].type == YELLOW)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			return false;
		}
	}
	else
	{
		return 0;
	}
}
bool ma(int a, int b, int c, int d)
{
	float h;
	h = sqrt(abs(d - b)*abs(d - b) + abs(c - a)*abs(c - a));
	//	printf("%f", h);
	if (h <= 2 || h >= 2.5)//根号8=2.8.根号5=2.2
	{
		//	printf("太远了!\n");
		return  false;
	}
	else
	{
		int xx, yy, max, min;//关键点的坐标和中间值
		max = abs(d - b) > abs(c - a) ? abs(d - b) : abs(c - a);
		min = abs(c - a) < abs(d - b) ? abs(c - a) : abs(d - b);
		//printf("max\min:(%d,%d)", max, min);
		if (max == abs(d - b))
		{
			yy = b + (d - b) / 2;
			xx = a;
		}
		else
		{
			xx = a + (c - a) / 2;
			yy = b;
		}
		//	printf("xx\yy:(%d,%d)\n", xx, yy);
		if (map[xx][yy].id == SPACE)
		{
			if (map[c][d].type != map[a][b].type)
			{
				//	printf("目的坐标(%d,%d)\n", c, d);
				//	printf("那是你自己的棋子!\n");
				return true;
			}
			else
			{
				//	printf("那是你的棋子!\n");
				return false;
			}

		}
		else
		{
			//	printf("关键位置有棋子!\n");
			return false;
		}
	}
}
bool xiang(int a, int b, int c, int d)
{

	float h;
	h = sqrt(abs(d - b)*abs(d - b) + abs(c - a)*abs(c - a));
	if(b<=4)
	{  
		if (d > 4)
		{
			return false;
		}
		else
		{
			if (h<2.4 || h>2.9)
			{
				return false;
			}
			else
			{
				int xx = (a + c) / 2;
				int yy = (b + d) / 2;
				if (map[xx][yy].id == SPACE)
				{
					if (map[c][d].type == map[a][b].type)
					{
						return false;
					}
					else
					{
						return true;

					}
				}
				else
				{
					return false;
				}
			}
		}
		
	}	
	else
	{
		if (d < 5)
		{
			return false;
		}
		else
		{
			if (h<2.4 || h>2.9)
			{
				return false;
			}
			else
			{
				int xx = (a + c) / 2;
				int yy = (b + d) / 2;
				if (map[xx][yy].id == SPACE)
				{
					if (map[c][d].type == map[a][b].type)
					{
						return false;
					}
					else
					{
						return true;

					}
				}
				else
				{
					return false;
				}
			}
		}
	}
	

}
bool shi(int a, int b, int c, int d)
{
	float h = sqrt(abs(d - b)*abs(d - b) + abs(c - a)*abs(c - a));
	//  printf("%f", h)
	
	if (b < 5)
	{
		if (c >= 3 && c <= 5 && d <= 2)
		{

			if (1.2 < h &&h < 1.5)
			{
				if (map[c][d].type != map[a][b].type)
					return true;
				else
					return false;

			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	else if (b >5)
	{
		if (c >= 3 && c <= 5 && d >= 7)
		{

			if (1.2 < h &&h < 1.5)
			{
				if (map[c][d].type != map[a][b].type)
					return true;
				else
					return false;

			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	else
		return false;
}
void execute(int a, int b, int c, int d)//行棋
{
	if (map[a][b].id == blackchess[4])//黑方将
	{
		if (jiang(a, b, c, d) )
		{

			movechess(a, b, c, d);

		}
		else
		{
			printf("你不能这样做\n");
		}

	}
	else if (map[a][b].id == redchess[4])//红方将
	{

		if (jiang(a, b, c, d) )
		{
			movechess(a, b, c, d);
		}
		else
		{
			printf("你不能这样做!\n");
		}
	}

	else if (map[a][b].id == blackchess[6])//黑方兵
	{
		if (map[a][b].river == false)
		{
			if ( bing( a,b, c, d))
			{
				movechess(a, b, c, d);

				if (d > 4)
				{
					map[c][d].river = true;
				}
			}
			else
			{
				printf("你不可以这样做!\n");
			}
		}
		else
		{
			if (bing(a, b, c, d) && d >= b)
			{
				movechess(a, b, c, d);

			}
			else
			{
				printf("你不可以这样做\n");
			}
		}
	}
	else if (map[a][b].id == redchess[6])//红方兵
	{
		if (map[a][b].river == false)
		{
			if (bing(a, b, c, d))
			{
				movechess(a, b, c, d);

				if (d < 5)
				{
					map[c][d].river = true;
				}
			}
			else
			{
				printf("你不可以这样做!\n");
			}
		}
		else
		{
			if (bing(a, b, c, d) && d <= b)
			{
				movechess(a, b, c, d);

			}
			else
			{
				printf("你不可以这样做!\n");
			}
		}
	}
	else if (map[a][b].id == blackchess[5] || map[a][b].id == redchess[5])
	{
		if (pao(a, b, c, d))
		{
			movechess(a, b, c, d);

		}
		else
		{
			printf("你不能这样做!\n");
		}

	}
	else if (map[a][b].id == blackchess[0] || map[a][b].id == redchess[0])
	{
		if (che(a, b, c, d))
		{
			movechess(a, b, c, d);

		}
		else
		{
			printf("你不可以这样做!\n");
		}
	}
	else if (map[a][b].id == blackchess[1] || map[a][b].id == redchess[1])
	{


		if (ma(a, b, c, d))
		{
			movechess(a, b, c, d);


		}
		else
		{
			printf("你不能这样做!\n");
		}

	}
	else if (map[a][b].id == blackchess[2] || map[a][b].id == redchess[2])
	{
		if (xiang(a, b, c, d))
		{
			movechess(a, b, c, d);

		}
		else
			printf("你不能这样做!\n");

	}
	else if (map[a][b].id == blackchess[3])
	{
		if (shi(a, b, c, d) )
		{
			movechess(a, b, c, d);

		}
		else
			printf("你不能这样做!");
	}
	else if (map[a][b].id == redchess[3])
	{
		if (shi(a, b, c, d))
		{
			movechess(a, b, c, d);

		}
		else
			printf("你不能这样做!");
	}
}
bool AIifdown(int a, int b,int c,int d)//判断该坐标下是否会被吃
{
          //	printf("(%d %d)\n", i, j);
			if(AImap[a][b].id == redchess[0]||AImap[a][b].id==blackchess[0])//俥
			{
			//	printf("俥:(%d %d)", i, j);
				if (che(a, b, c, d))
				{
				//	printf("危险:(%d %d)\n", a, b);
					return true;
				}
				else
				{
					//printf("都不危险!");
					return false;
				}

			}
			else if (AImap[a][b].id == redchess[1] || AImap[a][b].id == blackchess[1])//马
			{
			   //printf("马:(%d %d)", i, j);
				if (ma(a, b, c, d))
				{
				//	printf("危险:(%d %d)\n", a, b);
					return true;
				}
				else
				{
					//printf("都不危险!");
					return false;
				}

			}
			else	if (AImap[a][b].id == redchess[2]|| AImap[a][b].id == blackchess[2])//象
			{
					//printf("象:(%d %d)", i, j);
				if (xiang(a, b,c, d))
				{
					//printf("危险:(%d %d)\n", a, b);
					return true;
				}
				else
				{
					//printf("都不危险!");
					return false;
				}

			}
			else	if (AImap[a][b].id == redchess[3]|| AImap[a][b].id == blackchess[3])//士
			{
					//printf("士:(%d %d)", i, j);
				if (shi(a, b, c, d))
				{
				//	printf("危险:(%d %d)\n", a, b);
					return true;
				}

				else
				{
					//printf("都不危险!");
					return false;
				}
			}
			else	if (AImap[a][b].id == redchess[4]|| AImap[a][b].id == blackchess[4])//将
			{
					//printf("将:(%d %d)", i, j);
				if (jiang(a, b, c, d))
				{
					//printf("危险:(%d %d)\n", a, b);
					return true;
				}
				else
				{
					//printf("都不危险!");
					return false;
				}

			}
			else	if (AImap[a][b].id == redchess[5]|| AImap[a][b].id == blackchess[5])//炮
			{
					//printf("炮:(%d %d)", i, j);
				if (pao(a, b, c, d))
				{
					//printf("危险:(%d %d)\n", a, b);
					return true;
				}
				else
				{
					//printf("都不危险!");
					return false;
				}

			}
			else	if (AImap[a][b].id == redchess[6]|| AImap[a][b].id == blackchess[6])//兵
			{
					//printf("兵:(%d %d)", i, j);
				if (jiang(a, b, c, d))
				{
					//printf("危险:(%d %d)", a, b);
					return true;
				}

				else
				{
					//printf("都不危险!");
					return false;
				}
			}
			else
			{
				//printf("都不危险!");
				return false;
			}

		
	
}
float  AIpower(int a,int b)
{

		if (AImap[a][b].id == blackchess[4] || AImap[a][b].id == redchess[4])
		{
			return  100;
		}
		if (AImap[a][b].id == blackchess[0] || AImap[a][b].id == redchess[0])
		{
			return  8;
		}
		if (AImap[a][b].id == blackchess[1] || AImap[a][b].id == redchess[1])
		{
			return  4;
		}
		if (AImap[a][b].id == blackchess[2] || AImap[a][b].id == redchess[2])
		{
			return  3;
		}
		if (AImap[a][b].id == blackchess[3] || AImap[a][b].id == redchess[3])            
		{
		      return  1;
		}
		if (AImap[a][b].id == blackchess[5] || AImap[a][b].id == redchess[5])
		{
			return  6;
		}
		if (AImap[a][b].id == blackchess[6] || AImap[a][b].id == redchess[6])
		{
			return  2;
		}
		else
		{
			return 0;
		}
	
}
void  AIblackclean()
{
	for (int i = 0; i <= 20; i++)
	{
		AIblack[i].beginx = 0;
		AIblack[i].beginy = 0;
		AIblack[i].endx = 0;
		AIblack[i].endy = 0;
		AIblack[i].power = 0;
	}
}
float ALLifdown()
{
	float weight = 0;
	for (int a = 0; a <= 8; a++)
	{
		for (int b = 0; b <= 9; b++)
		{
			
			if (AImap[a][b].type == RED)
			{
				for (int c = 0; c <= 8; c++)
				{
					for (int d = 0; d <=9; d++)
					{
						if (AImap[c][d].type==BLACK&& AIifdown(a,b,c,d))
						{
							
							weight += AIpower(c,d);

						}
					
					}
				}

			}
		
		 }
   }
   return -weight;

}
float  ALifdowntow()
{
	float weight = 0;
	for (int a = 0; a <= 8; a++)
	{
		for (int b = 0; b <= 9; b++)
		{

			if (AImap[a][b].id == redchess[0])
			{
				for (int c = 0; c <= 8; c++)
				{
					for (int d = 0; d <= 9; d++)
					{

						if (AImap[c][d].id == blackchess[0] && che(a, b, c, d))
							weight += 8;
						if (AImap[c][d].id == blackchess[1] && che(a, b, c, d))
							weight += 4;
						if (AImap[c][d].id == blackchess[2] && che(a, b, c, d))
							weight += 3;
						if (AImap[c][d].id == blackchess[3] && che(a, b, c, d))
							weight += 2;
						if (AImap[c][d].id == blackchess[4] && che(a, b, c, d))
							weight += 100;
						if (AImap[c][d].id == blackchess[5] && che(a, b, c, d))
							weight += 6;
						if (AImap[c][d].id == blackchess[6] && che(a, b, c, d))
							weight += 3;
					}
				}
			}
			if (AImap[a][b].id == redchess[1])
			{
				for (int c = 0; c <= 8; c++)
				{
					for (int d = 0; d <= 9; d++)
					{

						if (AImap[c][d].id == blackchess[0] && ma(a, b, c, d))
							weight += 8;
						if (AImap[c][d].id == blackchess[1] && ma(a, b, c, d))
							weight += 4;
						if (AImap[c][d].id == blackchess[2] && ma(a, b, c, d))
							weight += 3;
						if (AImap[c][d].id == blackchess[3] && ma(a, b, c, d))
							weight += 2;
						if (AImap[c][d].id == blackchess[4] && ma(a, b, c, d))
							weight += 100;
						if (AImap[c][d].id == blackchess[5] && ma(a, b, c, d))
							weight += 6;
						if (AImap[c][d].id == blackchess[6] && ma(a, b, c, d))
							weight += 3;
					}
				}
			}
			if (AImap[a][b].id == redchess[2])
			{
				for (int c = 0; c <= 8; c++)
				{
					for (int d = 0; d <= 9; d++)
					{

						if (AImap[c][d].id == blackchess[0] && xiang(a, b, c, d))
							weight += 8;
						if (AImap[c][d].id == blackchess[1] && xiang(a, b, c, d))
							weight += 4;
						if (AImap[c][d].id == blackchess[2] && xiang(a, b, c, d))
							weight += 3;
						if (AImap[c][d].id == blackchess[3] && xiang(a, b, c, d))
							weight += 2;
						if (AImap[c][d].id == blackchess[4] && xiang(a, b, c, d))
							weight += 100;
						if (AImap[c][d].id == blackchess[5] && xiang(a, b, c, d))
							weight += 6;
						if (AImap[c][d].id == blackchess[6] && xiang(a, b, c, d))
							weight += 3;
					}
				}
			}
			if (AImap[a][b].id == redchess[3])
			{
				for (int c = 0; c <= 8; c++)
				{
					for (int d = 0; d <= 9; d++)
					{

						if (AImap[c][d].id == blackchess[0] && shi(a, b, c, d))
							weight += 8;
						if (AImap[c][d].id == blackchess[1] && shi(a, b, c, d))
							weight += 4;
						if (AImap[c][d].id == blackchess[2] && shi(a, b, c, d))
							weight += 3;
						if (AImap[c][d].id == blackchess[3] && shi(a, b, c, d))
							weight += 2;
						if (AImap[c][d].id == blackchess[4] && shi(a, b, c, d))
							weight += 100;
						if (AImap[c][d].id == blackchess[5] && shi(a, b, c, d))
							weight += 6;
						if (AImap[c][d].id == blackchess[6] && shi(a, b, c, d))
							weight += 3;
					}
				}
			}
			if (AImap[a][b].id == redchess[4])
			{
				for (int c = 0; c <= 8; c++)
				{
					for (int d = 0; d <= 9; d++)
					{

						if (AImap[c][d].id == blackchess[0] && jiang(a, b, c, d))
							weight += 8;
						if (AImap[c][d].id == blackchess[1] && jiang(a, b, c, d))
							weight += 4;
						if (AImap[c][d].id == blackchess[2] && jiang(a, b, c, d))
							weight += 3;
						if (AImap[c][d].id == blackchess[3] && jiang(a, b, c, d))
							weight += 2;
						if (AImap[c][d].id == blackchess[4] && jiang(a, b, c, d))
							weight += 100;
						if (AImap[c][d].id == blackchess[5] && jiang(a, b, c, d))
							weight += 6;
						if (AImap[c][d].id == blackchess[6] && jiang(a, b, c, d))
							weight += 3;
					}
				}
			}
			if (AImap[a][b].id == redchess[5])
			{
				for (int c = 0; c <= 8; c++)
				{
					for (int d = 0; d <= 9; d++)
					{

						if (AImap[c][d].id == blackchess[0] && pao(a, b, c, d))
							weight += 8;
						if (AImap[c][d].id == blackchess[1] && pao(a, b, c, d))
							weight += 4;
						if (AImap[c][d].id == blackchess[2] && pao(a, b, c, d))
							weight += 3;
						if (AImap[c][d].id == blackchess[3] && pao(a, b, c, d))
							weight += 2;
						if (AImap[c][d].id == blackchess[4] && pao(a, b, c, d))
							weight += 100;
						if (AImap[c][d].id == blackchess[5] && pao(a, b, c, d))
							weight += 6;
						if (AImap[c][d].id == blackchess[6] && pao(a, b, c, d))
							weight += 3;
					}
				}
			}
			if (AImap[a][b].id == redchess[6])
			{
				for (int c = 0; c <= 8; c++)
				{
					for (int d = 0; d <= 9; d++)
					{

						if (AImap[c][d].id == blackchess[0] && bing(a, b, c, d))
							weight += 8;
						if (AImap[c][d].id == blackchess[1] && bing(a, b, c, d))
							weight += 4;
						if (AImap[c][d].id == blackchess[2] && bing(a, b, c, d))
							weight += 3;
						if (AImap[c][d].id == blackchess[3] && bing(a, b, c, d))
							weight += 2;
						if (AImap[c][d].id == blackchess[4] && bing(a, b, c, d))
							weight += 100;
						if (AImap[c][d].id == blackchess[5] && bing(a, b, c, d))
							weight += 6;
						if (AImap[c][d].id == blackchess[6] && bing(a, b, c, d))
							weight += 3;
					}
				}
			}
		}
	}
	return -weight;
}
void llll()
{

	int  time = 1;
	int twotime = 1;
	AIblack[0].power = -200;
	for (int i = 0; i <= 8; i++)
	{
		for (int j = 0; j <= 9; j++)
		{
			if (AImap[i][j].type == BLACK)//遍历黑方棋子
			{


				if (AImap[i][j].id == blackchess[0])//黑棋是俥
				{
					int twotime = 0;
					struct AIcoord AIsecendblack[20];
					// printf("马(%d %d)", i, j);
					AIsecendblack[0].power = -200;
					for (int twoi = 0; twoi <= 8; twoi++)
					{
						for (int twoj = 0; twoj <= 9; twoj++)
						{
							if (che(i, j, twoi, twoj))
							{

								AIblack[time].beginx = i;
								AIblack[time].beginy = j;
								AIblack[time].power = 0;
								for (int a = 0; a <= 8; a++)
								{
									for (int b = 0; b <= 9; b++)
									{
									 	if (AIifdown(a, b, i, j))
										{
											AIblack[time].power += AIpower(i, j);
											//		printf("危险:(%d %d)会被红方(%d,%d)攻击---攻击权值为:%g     ", i, j, a, b, AIblack[time].power);
															//	printf("%f", AIpower(i, j));
										}
									
									}
								}
								//	printf("逃跑权值:%g  \n ", AIblack[time].power);
								AIsecendblack[twotime].endx = twoi;
								AIsecendblack[twotime].endy = twoj;
								AIsecendblack[twotime].power = 0;
								if (AIifdown(i, j, twoi, twoj) && AImap[twoi][twoj].type == RED)
								{
									AIsecendblack[twotime].power += AIpower(twoi, twoj)  ;
								}
								
									AImovechess(i, j, twoi, twoj);
									AIsecendblack[twotime].power = AIsecendblack[twotime].power + ALLifdown();
							
				
								if (AIsecendblack[0].power <= AIsecendblack[twotime].power)
								{
									AIsecendblack[0].power = AIsecendblack[twotime].power;
									AIsecendblack[0].endx = AIsecendblack[twotime].endx;
									AIsecendblack[0].endy = AIsecendblack[twotime].endy;
								}
								twotime++;
								AIfollowchess();
							}

						}
					}
			
					AIblack[time].beginx = i;
					AIblack[time].beginy = j;
					AIblack[time].endx = AIsecendblack[0].endx;
					AIblack[time].endy = AIsecendblack[0].endy;
					AIblack[time].power = AIsecendblack[0].power;
						printf("俥(%d %d)最有价值的路径是(%d %d)到(%d %d)权值是%g\n", AIblack[time].beginx, AIblack[time].beginy, AIblack[time].beginx, AIblack[time].beginy, AIblack[time].endx, AIblack[time].endy, AIblack[time].power);
				//		printf("俥(%d %d)最有价值的路径是(%d %d)到(%d %d)权值是0\n", AIblack[time].beginx, AIblack[time].beginy, AIblack[time].beginx, AIblack[time].beginy, AIblack[time].endx, AIblack[time].endy);
							//	printf("俥(%d %d)最有价值的路径是(%d %d)到(%d %d)权值是%g",i,j,i,j, AIblack[0].endx, AIblack[0].endy, AIblack[0].power);
						//	AIsecendblackclean();
							if (AIblack[0].power < AIblack[time].power)
							{
								AIblack[0].beginx = AIblack[time].beginx;
								AIblack[0].beginy = AIblack[time].beginy;
								AIblack[0].endx = AIblack[time].endx;
								AIblack[0].endy = AIblack[time].endy;
								AIblack[0].power = AIblack[time].power;
							}

					time++;
				}
				if (AImap[i][j].id == blackchess[1])//黑棋是马
				{
					int twotime = 0;
					struct AIcoord AIsecendblack[20];
					//f("马(%d %d)", i, j);
					AIsecendblack[0].power = -200;
					for (int twoi = 0; twoi <= 8; twoi++)
					{
						for (int twoj = 0; twoj <= 9; twoj++)
						{
							if (ma(i, j, twoi, twoj))
							{

								AIblack[time].beginx = i;
								AIblack[time].beginy = j;
								AIblack[time].power = 0;
								for (int a = 0; a <= 8; a++)
								{
									for (int b = 0; b <= 9; b++)
									{
										if (AIifdown(a, b, i, j))
										{
											AIblack[time].power += AIpower(i, j);
											//		printf("危险:(%d %d)会被红方(%d,%d)攻击---攻击权值为:%g     ", i, j, a, b, AIblack[time].power);
															//	printf("%f", AIpower(i, j));
										}
									}
								}
								//	printf("逃跑权值:%g  \n ", AIblack[time].power);
							
								AIsecendblack[twotime].endx = twoi;
								AIsecendblack[twotime].endy = twoj;
								AIsecendblack[twotime].power = 0;
								if (AIifdown(i, j, twoi, twoj) && AImap[twoi][twoj].type == RED)
								{
									AIsecendblack[twotime].power += AIpower(twoi, twoj);
									//	printf("我方(%d %d)可以攻击(%d %d)的红方棋子,移动权值为%g\n", i, j, twoi, twoj, AIsecendblack[twotime].power);
								}
								///		printf("吃子权值:%g", AIsecendblack[twotime].power);

								//     	printf("总的权值:%g\n+", AIsecendblack[twotime].power);							

								//		printf("\n");

									//	printf("马的可移动坐标(%d,%d)--权重: %g\n", AIsecendblack[twotime].endx, AIsecendblack[twotime].endy, AIsecendblack[twotime].power);
									
										float h = AIsecendblack[twotime].power;
									//	printf("---------移动前:%g", AIsecendblack[twotime].power);
										AImovechess(i, j, twoi, twoj);
								    	AIsecendblack[twotime].power = AIsecendblack[twotime].power + ALLifdown();
								//		printf("---------移动后:%g\n", ALLifdown());;
						
								if (AIsecendblack[0].power <= AIsecendblack[twotime].power)
								{
									AIsecendblack[0].power = AIsecendblack[twotime].power;
									AIsecendblack[0].endx = AIsecendblack[twotime].endx;
									AIsecendblack[0].endy = AIsecendblack[twotime].endy;
								}
								//		printf("俥(%d %d)可以移动的路径是(%d %d)到(%d %d)权值是%g", i,j,i, j, AIsecendblack[0].endx, AIsecendblack[0].endy, AIsecendblack[0].power);
								AIfollowchess();
								twotime++;
							}

						}
					}
					//	printf("俥(%d %d)可以移动的路径是(%d %d)到(%d %d)权值是%g", i, j, i, j, AIsecendblack[0].endx, AIsecendblack[0].endy, AIsecendblack[0].power);
					AIblack[time].beginx = i;
					AIblack[time].beginy = j;
					AIblack[time].endx = AIsecendblack[0].endx;
					AIblack[time].endy = AIsecendblack[0].endy;
					AIblack[time].power = AIsecendblack[0].power;
		   printf("马(%d %d)最有价值的路径是(%d %d)到(%d %d)权值是%g\n", AIblack[time].beginx, AIblack[time].beginy, AIblack[time].beginx, AIblack[time].beginy, AIblack[time].endx, AIblack[time].endy, AIblack[time].power);
			//   printf("马(%d %d)最有价值的路径是(%d %d)到(%d %d)权值是0\n", AIblack[time].beginx, AIblack[time].beginy, AIblack[time].beginx, AIblack[time].beginy, AIblack[time].endx, AIblack[time].endy);
			   //	printf("俥(%d %d)最有价值的路径是(%d %d)到(%d %d)权值是%g",i,j,i,j, AIblack[0].endx, AIblack[0].endy, AIblack[0].power);
					//	AIsecendblackclean();
				if (AIblack[0].power < AIblack[time].power)
				{
					AIblack[0].beginx = AIblack[time].beginx;
					AIblack[0].beginy = AIblack[time].beginy;
					AIblack[0].endx = AIblack[time].endx;
					AIblack[0].endy = AIblack[time].endy;
					AIblack[0].power = AIblack[time].power;
				}
					time++;
				}
				if (AImap[i][j].id == blackchess[2])//黑棋是象
				{
					int twotime = 0;
					struct AIcoord AIsecendblack[20];
					// printf("马(%d %d)", i, j);
					AIsecendblack[0].power = -200;
					for (int twoi = 0; twoi <= 8; twoi++)
					{
						for (int twoj = 0; twoj <= 9; twoj++)
						{
							if (xiang(i, j, twoi, twoj))
							{

								AIblack[time].beginx = i;
								AIblack[time].beginy = j;
								AIblack[time].power = 0;
								for (int a = 0; a <= 8; a++)
								{
									for (int b = 0; b <= 9; b++)
									{
										if (AIifdown(a, b, i, j))
										{
											AIblack[time].power += AIpower(i, j);
											//		printf("危险:(%d %d)会被红方(%d,%d)攻击---攻击权值为:%g     ", i, j, a, b, AIblack[time].power);
															//	printf("%f", AIpower(i, j));
										}
									}
								}
								//	printf("逃跑权值:%g  \n ", AIblack[time].power);
								AIsecendblack[twotime].endx = twoi;
								AIsecendblack[twotime].endy = twoj;
								AIsecendblack[twotime].power = 0;

								if (AIifdown(i, j, twoi, twoj) && AImap[twoi][twoj].type == RED)
								{
									AIsecendblack[twotime].power += AIpower(twoi, twoj);
									//	printf("我方(%d %d)可以攻击(%d %d)的红方棋子,移动权值为%g\n", i, j, twoi, twoj, AIsecendblack[twotime].power);
								}
								///		printf("吃子权值:%g", AIsecendblack[twotime].power);

								//     	printf("总的权值:%g\n+", AIsecendblack[twotime].power);							

								//		printf("\n");

						//		printf("象的可移动坐标(%d,%d)--权重: %g\n", AIsecendblack[twotime].endx, AIsecendblack[twotime].endy, AIsecendblack[twotime].power);
								float h = AIsecendblack[twotime].power;
							//	printf("---------移动前:%g", AIsecendblack[twotime].power);
								AImovechess(i, j, twoi, twoj);
									AIsecendblack[twotime].power = AIsecendblack[twotime].power + ALLifdown();
							//	printf("---------移动后:%g\n", ALLifdown());
								if (AIsecendblack[0].power 

你可能感兴趣的:(c语言,游戏,算法)