好玩的游戏
查看原帖
好玩的游戏
865625
KobeBeanBryantCox楼主2023/4/8 16:53

本人原创象棋双人对战程序

版权所有:wangjianbo123(转载请注明)

需要VS(版本不知道)+easyX库运行

支持控制、吃子、检测将军、检测绝杀和困毙

VS下载和easyX库下载(VS的安装很麻烦,自行百度)

代码:

#include <iostream>
#include <graphics.h>
#include <conio.h>
#define Code using
#define by namespace
#define wjb std
Code by wjb;
int chessxy[15][15][3210], temp[15][15] = { {0,0,0,0,0,0,0,0,0,0},
											{0,1,2,3,4,5,4,3,2,1},
											{0,0,0,0,0,0,0,0,0,0},
											{0,0,6,0,0,0,0,0,6,0},
											{0,7,0,7,0,7,0,7,0,7},
											{0,0,0,0,0,0,0,0,0,0},
											{0,0,0,0,0,0,0,0,0,0},
											{0,14,0,14,0,14,0,14,0,14},
											{0,0,13,0,0,0,0,0,13,0},
											{0,0,0,0,0,0,0,0,0,0},
											{0,8,9,10,11,12,11,10,9,8} };//棋子所在位置
		  /* 1黑车  2黑马  3黑象  4黑士  5黑帅  6黑炮  7黑兵
			 9红车  9红马 10红象 11红士 12红帅 13红炮 14红兵 */
bool candown[15][15];
string player = "red";
int tot = 1;
void draw_board_sign(int x,int y)//棋盘炮兵标志
{
	if (!(x == 50))
	{
		line(x - 5, y - 15, x - 5, y - 5);
		line(x - 5, y + 15, x - 5, y + 5);
		line(x - 15, y - 5, x - 5, y - 5);
		line(x - 15, y + 5, x - 5, y + 5);
	}
	if (!(x == 450))
	{
		line(x + 5, y - 15, x + 5, y - 5);
		line(x + 5, y + 15, x + 5, y + 5);
		line(x + 15, y - 5, x + 5, y - 5);
		line(x + 15, y + 5, x + 5, y + 5);
	}
}
void draw_board()//画棋盘
{
	setlinestyle(0, 3);
	setfillcolor(RGB(218, 165, 32));//框
	setcolor(BLACK);
	fillrectangle(20, 20, 630, 580);
	rectangle(50, 50, 450, 500);

	rectangle(50, 100, 450, 450);//横线
	rectangle(50, 150, 450, 400);
	rectangle(50, 200, 450, 350);
	rectangle(50, 250, 450, 300);

	rectangle(100, 50, 400, 250);//纵线
	rectangle(150, 50, 350, 250);
	rectangle(200, 50, 300, 250);
	line(250, 50, 250, 250);
	rectangle(100, 300, 400, 500);
	rectangle(150, 300, 350, 500);
	rectangle(200, 300, 300, 500);
	line(250, 300, 250, 500);

	line(200, 50, 300, 150);//王所在的那俩框
	line(200, 150, 300, 50);
	line(200, 400, 300, 500);
	line(200, 500, 300, 400);

	draw_board_sign(100, 150);//炮和兵所在的位置的标志(上)
	draw_board_sign(400, 150);
	draw_board_sign(50, 200);
	draw_board_sign(150, 200);
	draw_board_sign(250, 200);
	draw_board_sign(350, 200);
	draw_board_sign(450, 200);

	draw_board_sign(100, 400);//炮和兵所在的位置的标志(下)
	draw_board_sign(400, 400);
	draw_board_sign(50, 350);
	draw_board_sign(150, 350);
	draw_board_sign(250, 350);
	draw_board_sign(350, 350);
	draw_board_sign(450, 350);

	setfont(40, 0, _T("楷体"));//楚河汉界
	setbkmode(TRANSPARENT);
	outtextxy(135, 255, _T("楚河"));
	outtextxy(285, 255, _T("汉界"));

	setfont(30, 0, _T("楷体"));//操作提示
	outtextxy(55, 530, _T("WASD或方向键移动,空格选择"));
	if (player == "red")
	{
		setcolor(RED);
		outtextxy(480, 50, _T("红方回合"));
	}
	else
	{
		setcolor(BLACK);
		outtextxy(480, 50, _T("黑方回合"));
	}
	setcolor(BLACK);
	setfillcolor(YELLOW);
	fillellipse(480, 100, 600, 175);
	outtextxy(490, 122, _T("悔棋1键"));
}
void draw_chess(int x, int y)//画棋子
{
	setlinestyle(0, 2);
	setbkmode(TRANSPARENT);
	setfillcolor(RGB(251, 234, 140));
	for (int i = 1; i <= 10; i++)
		for (int j = 1; j <= 9; j++)
		{
			int xx= j * 50 - 15, yy= i * 50 - 15;
			if (i == x && j == y)setfont(40, 0, _T("楷体")), xx = j * 50 - 20, yy = i * 50 - 20;
			else setfont(30, 0, _T("楷体"));
			setcolor(BLACK);
			if (chessxy[i][j][tot] != 0)
				if (i == x && j == y)fillcircle(j * 50, i * 50, 25);
				else fillcircle(j * 50, i * 50, 20);
			if (chessxy[i][j][tot] >= 1 && chessxy[i][j][tot] <= 7)setcolor(BLACK);
			else setcolor(RED);
			switch (chessxy[i][j][tot])
			{
			case 1:case 8:outtextxy(xx, yy, _T("車")); break;
			case 2:case 9:outtextxy(xx, yy, _T("馬")); break;
			case 10:outtextxy(xx, yy, _T("相")); break;
			case 3:outtextxy(xx, yy, _T("象")); break;
			case 4:case 11:outtextxy(xx, yy, _T("士")); break;
			case 12:outtextxy(xx, yy, _T("帥")); break;
			case 5:outtextxy(xx, yy, _T("將")); break;
			case 6:case 13:outtextxy(xx, yy, _T("炮")); break;
			case 14:outtextxy(xx, yy, _T("兵")); break;
			case 7:outtextxy(xx, yy, _T("卒")); break;
			}
		}
}
void draw_pointer(int y, int x)//绘制光标
{
	setcolor(BLACK);
	line(x * 50 - 22, y * 50 - 22, x * 50 - 22, y * 50 - 10);
	line(x * 50 - 22, y * 50 - 22, x * 50 - 10, y * 50 - 22);
	line(x * 50 + 22, y * 50 - 22, x * 50 + 22, y * 50 - 10);
	line(x * 50 + 22, y * 50 - 22, x * 50 + 10, y * 50 - 22);
	line(x * 50 - 22, y * 50 + 22, x * 50 - 22, y * 50 + 10);
	line(x * 50 - 22, y * 50 + 22, x * 50 - 10, y * 50 + 22);
	line(x * 50 + 22, y * 50 + 22, x * 50 + 22, y * 50 + 10);
	line(x * 50 + 22, y * 50 + 22, x * 50 + 10, y * 50 + 22);
}
bool checkhorse(int x, int y, string player1)//马将
{
	int u = 9, dx[8] = { -1,-2,-2,-1,1,2,2,1 }, dy[8] = { -2,-1,1,2,2,1,-1,-2 }, cx[8] = { -1,-1,-1,-1,1,1,1,1 }, cy[8] = { -1,-1,1,1,1,1,-1,-1 };
	if (player1 == "red")u = 2;
	for (int i = 0; i < 8; i++)
	{
		int xx = x + dx[i], yy = y + dy[i], xxx = x + cx[i], yyy = y + cy[i];
		if (xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9 && temp[xx][yy] == u && temp[xxx][yyy] == 0)return true;
	}
	return false;
}
bool checkcar(int x, int y, string player1)//车将
{
	int u = 8, d[5] = { 1,0,-1,0,1 };
	if (player1 == "red")u = 1;
	for (int i = 0; i < 4; i++)
	{
		int xx = x + d[i], yy = y + d[i + 1];
		for (; xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9 && temp[xx][yy] == 0; xx += d[i], yy += d[i + 1]);
		if (temp[xx][yy] == u)return true;
	}
	return false;
}
bool checkcannon(int x, int y, string player1)//炮将
{
	int u = 13, d[5] = { 1,0,-1,0,1 };
	if (player1 == "red")u = 6;
	for (int i = 0; i < 4; i++)
	{
		int xx = x + d[i], yy = y + d[i + 1];
		for (; xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9 && temp[xx][yy] == 0; xx += d[i], yy += d[i + 1]);
		for (xx += d[i], yy += d[i + 1]; xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9 && temp[xx][yy] == 0; xx += d[i], yy += d[i + 1]);
		if (yy < 1)continue;
		if (temp[xx][yy] == u)return true;
	}
	return false;
}
bool checksoldier(int x, int y, string player1)//兵将
{
	int u = 14, dx[3] = { 0,1,0 }, dy[3] = { -1,0,1 };
	if (player1 == "red")u = 7, dx[1] = -1;
	for (int i = 0; i < 3; i++)
	{
		int xx = x + dx[i], yy = y + dy[i];
		if (xx >= 1 && xx <= 10 && yy >= 1 && yy <= 10 && temp[xx][yy] == u)return true;
	}
	return false;
}
bool checkking(int x, int y, string player1)//双王对面
{
	if (player1 == "black")
	{
		int xx = x + 1;
		for (; xx <= 10 && temp[xx][y] == 0; xx++);
		if (xx <= 10 && temp[xx][y] == 12)return true;
	}
	else
	{
		int xx = x - 1;
		for (; xx >= 1 && temp[xx][y] == 0; xx--);
		if (xx >= 1 && temp[xx][y] == 5)return true;
	}
	return false;
}
bool check(string player1)//判断将军
{
	int u = 5, x, y;
	if (player1 == "red")u = 12;
	for (int i = 1; i <= 10; i++)//王的位置
		for (int j = 1; j <= 9; j++)
			if (temp[i][j] == u)
			{
				x = i, y = j;
				break;
			}
	if (checkhorse(x, y, player1))return true;
	if (checkcar(x, y, player1))return true;
	if (checkcannon(x, y, player1))return true;
	if (checksoldier(x, y, player1))return true;
	if (checkking(x, y, player1))return true;
	return false;
}
void Car(int x, int y)//车走法
{
	int xx, yy, d[5] = { 1,0,-1,0,1 };
	for (int i = 0; i < 4; i++)
	{
		xx = x + d[i], yy = y + d[i + 1];
		for (; xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9 && chessxy[xx][yy][tot] == 0; xx += d[i], yy += d[i + 1])
		{
			temp[xx][yy] = temp[x][y], temp[x][y] = 0;
			if (!check(player))candown[xx][yy] = true;
			for (int j = 1; j <= 10; j++)
				for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
		}
		if (!(xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9))continue;
		temp[xx][yy] = temp[x][y], temp[x][y] = 0;
		if (((chessxy[xx][yy][tot] >= 1 && chessxy[xx][yy][tot] <= 7 && player == "red") || (chessxy[xx][yy][tot] >= 8 && chessxy[xx][yy][tot] <= 14 && player == "black")) && !check(player))candown[xx][yy] = true;
		for (int j = 1; j <= 10; j++)
			for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
	}
}
void Horse(int x, int y)//马走法
{
	int xx, yy, dx[8] = { -2,-2,-1,1,2,2,1,-1 }, dy[8] = { -1,1,2,2,1,-1,-2,-2 }, cx[8] = { -1,-1,0,0,1,1,0,0 }, cy[8] = { 0,0,1,1,0,0,-1,-1 };
	for (int i = 0; i < 8; i++)
	{
		xx = x + cx[i], yy = y + cy[i];
		if (chessxy[xx][yy][tot] != 0)continue;
		xx = x + dx[i], yy = y + dy[i];
		if (!(xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9))continue;
		temp[xx][yy] = temp[x][y], temp[x][y] = 0;
		if ((chessxy[xx][yy][tot] == 0 || (chessxy[xx][yy][tot] >= 1 && chessxy[xx][yy][tot] <= 7 && player == "red") || (chessxy[xx][yy][tot] >= 8 && chessxy[xx][yy][tot] <= 14 && player == "black")) && !check(player))candown[xx][yy] = true;
		for (int j = 1; j <= 10; j++)
			for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
	}
}
void Elephant(int x, int y)//象走法
{
	int xx, yy, dx[4] = { -2,-2,2,2 }, dy[4] = { -2,2,-2,2 }, cx[4] = { -1,-1,1,1 }, cy[4] = { -1,1,1,-1 };
	for (int i = 0; i < 4; i++)
	{
		xx = x + cx[i], yy = y + cy[i];
		if (chessxy[xx][yy][tot] != 0)continue;
		xx = x + dx[i], yy = y + dy[i];
		if (!(xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9))continue;
		temp[xx][yy] = temp[x][y], temp[x][y] = 0;
		if (player == "red" && xx >= 6 && xx <= 10 && yy >= 1 && yy <= 9 && (chessxy[xx][yy][tot] == 0 || (chessxy[xx][yy][tot] >= 1 && chessxy[xx][yy][tot] <= 7)) && !check(player))candown[xx][yy] = true;
		else if (player == "black" && xx >= 1 && xx <= 5 && yy >= 1 && yy <= 9 && (chessxy[xx][yy][tot] == 0 || (chessxy[xx][yy][tot] >= 8 && chessxy[xx][yy][tot] <= 14)) && !check(player))candown[xx][yy] = true;
		for (int j = 1; j <= 10; j++)
			for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
	}
}
void Scholar(int x, int y)//士走法
{
	int xx, yy, dx[4] = { -1,-1,1,1 }, dy[4] = { -1,1,1,-1 };
	for (int i = 0; i < 4; i++)
	{
		xx = x + dx[i], yy = y + dy[i];
		if (!(xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9))continue;
		temp[xx][yy] = temp[x][y], temp[x][y] = 0;
		if (player == "red" && xx >= 8 && xx <= 10 && yy >= 4 && yy <= 6 && (chessxy[xx][yy][tot] == 0 || (chessxy[xx][yy][tot] >= 1 && chessxy[xx][yy][tot] <= 7)) && !check(player))candown[xx][yy] = true;
		else if (player == "black" && xx >= 1 && xx <= 3 && yy >= 4 && yy <= 6 && (chessxy[xx][yy][tot] == 0 || (chessxy[xx][yy][tot] >= 8 && chessxy[xx][yy][tot] <= 14)) && !check(player))candown[xx][yy] = true;
		for (int j = 1; j <= 10; j++)
			for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
	}
}
void King(int x, int y)//王走法
{
	int xx, yy, d[5] = { 1,0,-1,0,1 };
	for (int i = 0; i < 4; i++)
	{
		xx = x + d[i], yy = y + d[i + 1];
		if (!(xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9))continue;
		temp[xx][yy] = temp[x][y], temp[x][y] = 0;
		if (player == "red" && xx >= 8 && xx <= 10 && yy >= 4 && yy <= 6 && (chessxy[xx][yy][tot] == 0 || (chessxy[xx][yy][tot] >= 1 && chessxy[xx][yy][tot] <= 7)) && !check(player))candown[xx][yy] = true;
		else if (player == "black" && xx >= 1 && xx <= 3 && yy >= 4 && yy <= 6 && (chessxy[xx][yy][tot] == 0 || (chessxy[xx][yy][tot] >= 8 && chessxy[xx][yy][tot] <= 14)) && !check(player))candown[xx][yy] = true;
		for (int j = 1; j <= 10; j++)
			for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
	}
}
void Cannon(int x, int y)//炮走法
{
	int xx, yy, d[5] = { 1,0,-1,0,1 };
	for (int i = 0; i < 4; i++)
	{
		xx = x + d[i], yy = y + d[i + 1];
		for (; xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9 && chessxy[xx][yy][tot] == 0; xx += d[i], yy += d[i + 1])
		{
			temp[xx][yy] = temp[x][y], temp[x][y] = 0;
			if (!check(player))candown[xx][yy] = true;
			for (int j = 1; j <= 10; j++)
				for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
		}
		for (xx += d[i], yy += d[i + 1]; xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9 && chessxy[xx][yy][tot] == 0; xx += d[i], yy += d[i + 1]);
		if (!(xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9))continue;
		temp[xx][yy] = temp[x][y], temp[x][y] = 0;
		if (((chessxy[xx][yy][tot] >= 1 && chessxy[xx][yy][tot] <= 7 && player == "red") || (chessxy[xx][yy][tot] >= 8 && chessxy[xx][yy][tot] <= 14 && player == "black")) && !check(player))candown[xx][yy] = true;
		for (int j = 1; j <= 10; j++)
			for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
	}
}
void Soldier(int x, int y)//兵走法
{
	if (player == "red")
	{
		if (x <= 5)
		{
			int dx[3] = { 0,-1,0 }, dy[3] = { -1,0,1 };
			for (int i = 0; i < 3; i++)
			{
				int xx = x + dx[i], yy = y + dy[i];
				if (!(xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9))continue;
				temp[xx][yy] = temp[x][y], temp[x][y] = 0;
				if ((chessxy[xx][yy][tot] == 0 || (chessxy[xx][yy][tot] >= 1 && chessxy[xx][yy][tot] <= 7)) && !check(player))candown[xx][yy] = true;
				for (int j = 1; j <= 10; j++)
					for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
			}
		}
		else
		{
			if (!(x - 1 >= 1 && x - 1 <= 10 && y >= 1 && y <= 9))return;
			temp[x - 1][y] = temp[x][y], temp[x][y] = 0;
			if ((chessxy[x - 1][y][tot] == 0 || (chessxy[x - 1][y][tot] >= 1 && chessxy[x - 1][y][tot] <= 7)) && !check(player))candown[x - 1][y] = true;
			for (int j = 1; j <= 10; j++)
				for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
		}
	}
	else
	{
		if (x >= 6)
		{
			int dx[3] = { 0,1,0 }, dy[3] = { -1,0,1 };
			for (int i = 0; i < 3; i++)
			{
				int xx = x + dx[i], yy = y + dy[i];
				if (!(xx >= 1 && xx <= 10 && yy >= 1 && yy <= 9))continue;
				temp[xx][yy] = temp[x][y], temp[x][y] = 0;
				if ((chessxy[xx][yy][tot] == 0 || (chessxy[xx][yy][tot] >= 8 && chessxy[xx][yy][tot] <= 14)) && !check(player))candown[xx][yy] = true;
				for (int j = 1; j <= 10; j++)
					for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
			}
		}
		else
		{
			if (!(x + 1 >= 1 && x + 1 <= 10 && y >= 1 && y <= 9))return;
			temp[x + 1][y] = temp[x][y], temp[x][y] = 0;
			if ((chessxy[x + 1][y][tot] == 0 || (chessxy[x + 1][y][tot] >= 8 && chessxy[x + 1][y][tot] <= 14)) && !check(player))candown[x + 1][y] = true;
			for (int j = 1; j <= 10; j++)
				for (int k = 1; k <= 9; k++)temp[j][k] = chessxy[j][k][tot];
		}
	}
}
void draw_position(int a, int x, int y)//判断落子位置
{
	for (int i = 1; i <= 10; i++)
		for (int j = 1; j <= 9; j++)temp[i][j] = chessxy[i][j][tot];
	memset(candown, false, sizeof(candown));//各个棋子能下位置
	if (a == 1 || a == 8)Car(x, y);
	else if (a == 2 || a == 9)Horse(x, y);
	else if (a == 3 || a == 10)Elephant(x, y);
	else if (a == 4 || a == 11)Scholar(x, y);
	else if (a == 5 || a == 12)King(x, y);
	else if (a == 6 || a == 13)Cannon(x, y);
	else if (a == 7 || a == 14)Soldier(x, y);
	
	if (a >= 1 && a <= 7)setcolor(BLACK);//绘制能下位置
	else if (a >= 8 && a <= 14)setcolor(RED);
	for (int i = 1; i <= 10; i++)
		for (int j = 1; j <= 9; j++)
			if (candown[i][j])circle(j * 50, i * 50, 10);
}
void sign(int x, int y, string whatsign)//画将军,吃子,绝杀等标志
{
	cleardevice();
	draw_board();
	draw_chess(0, 0);
	draw_pointer(x, y);
	setcolor(BLACK);
	setfillcolor(YELLOW);
	fillcircle(250, 275, 100);
	setfont(85, 0, _T("楷体"));
	if (player == "black")setcolor(RED);
	if (whatsign == "吃子")
	{
		outtextxy(175, 200, _T("吃"));
		outtextxy(250, 265, _T("子"));
		Beep(500, 1500);
		Sleep(600);
	}
	else if (whatsign == "将军")
	{
		outtextxy(175, 200, _T("将"));
		outtextxy(250, 265, _T("军"));
		for (int i = 1; i <= 3; i++)Beep(1000, 200), Beep(1000, 200), Sleep(300);
	}
	else if (whatsign == "绝杀" || whatsign == "困毙")
	{
		if (whatsign == "绝杀")outtextxy(175, 200, _T("绝")), outtextxy(250, 265, _T("杀"));
		else outtextxy(175, 200, _T("困")), outtextxy(250, 265, _T("毙"));
		for (int i = 4; i >= 1; i--)Beep(i * 400, 500);
		Sleep(100);
	}
}
bool win()//绝杀和困毙
{
	for (int i = 1; i <= 10; i++)
		for (int j = 1; j <= 9; j++)temp[i][j] = chessxy[i][j][tot];
	memset(candown, false, sizeof(candown));
	for(int i=1;i<=10;i++)
		for(int j=1;j<=9;j++)
			if(player=="black")
				switch (chessxy[i][j][tot])
				{
				case 1:Car(i, j); break;
				case 2:Horse(i, j); break;
				case 3:Elephant(i, j); break;
				case 4:Scholar(i, j); break;
				case 5:King(i, j); break;
				case 6:Cannon(i, j); break;
				case 7:Soldier(i, j); break;
				}
			else
				switch (chessxy[i][j][tot])
				{
				case 8:Car(i, j); break;
				case 9:Horse(i, j); break;
				case 10:Elephant(i, j); break;
				case 11:Scholar(i, j); break;
				case 12:King(i, j); break;
				case 13:Cannon(i, j); break;
				case 14:Soldier(i, j); break;
				}
	int s = 0;
	for (int i = 1; i <= 10; i++)
		for (int j = 1; j <= 9; j++,s++)
			if (candown[i][j])return false;
	return true;
}
void play()//对弈
{
	for (int i = 1; i <= 10; i++)//棋盘初始化
		for (int j = 1; j <= 9; j++)chessxy[i][j][1] = temp[i][j];
	int x = 8, y = 5;
	while (true)
	{
		if (tot == 3201)return;//超过步数限制
		draw_board();
		draw_chess(0, 0);
		draw_pointer(x, y);
		if (win())//困毙判断
		{
			sign(x, y, "困毙");
			return;
		}
		char c = _getch();
		if (c == 72 || c == 'w' || c == 'W')x = max(1, x - 1);//上
		else if (c == 80 || c == 's' || c == 'S')x = min(10, x + 1);//下
		else if (c == 75 || c == 'a' || c == 'A')y = max(1, y - 1);//左
		else if (c == 77 || c == 'd' || c == 'D')y = min(9, y + 1);//右
		else if (c == ' ')//选择棋子
		{
			if (chessxy[x][y][tot] != 0 && ((player == "black" && chessxy[x][y][tot] >= 1 && chessxy[x][y][tot] <= 7) || (player == "red" && chessxy[x][y][tot] >= 8 && chessxy[x][y][tot] <= 14)))//棋子符合行走方
			{
				int xx = x, yy = y;
				while (true)
				{
					cleardevice();
					draw_board();
					draw_chess(x, y);
					draw_pointer(xx, yy);
					draw_position(chessxy[x][y][tot], x, y);
					char ch = _getch();
					if (ch == 72 || ch == 'w' || ch == 'W')xx = max(1, xx - 1);//上
					else if (ch == 80 || ch == 's' || ch == 'S')xx = min(10, xx + 1);//下
					else if (ch == 75 || ch == 'a' || ch == 'A')yy = max(1, yy - 1);//左
					else if (ch == 77 || ch == 'd' || ch == 'D')yy = min(9, yy + 1);//右
					else if (ch == ' ')//选择落子位置
					{
						if (candown[xx][yy])//判断是否能下
						{
							bool f = false;
							for (int i = 1; i <= 10; i++)
								for (int j = 1; j <= 9; j++)chessxy[i][j][tot + 1] = chessxy[i][j][tot];
							tot++;
							if ((chessxy[xx][yy][tot] >= 1 && chessxy[xx][yy][tot] <= 7 && player == "red") || (chessxy[xx][yy][tot] >= 8 && chessxy[xx][yy][tot] <= 14 && player == "black"))f = true;
							chessxy[xx][yy][tot] = chessxy[x][y][tot];
							chessxy[x][y][tot] = 0;
							for (int i = 1; i <= 10; i++)
								for (int j = 1; j <= 9; j++)temp[i][j] = chessxy[i][j][tot];
							if (player == "red")player = "black", x = 3, y = 5;
							else player = "red", x = 8, y = 5;
							if (f)sign(xx, yy, "吃子");
							if (check(player))
							{
								if (win())//绝杀判断
								{
									sign(xx, yy, "绝杀");
									return;
								}
								else sign(xx, yy, "将军");
							}
							break;
						}
						else if ((!(xx == x && yy == y)) && chessxy[xx][yy][tot] != 0 && ((player == "black" && chessxy[xx][yy][tot] >= 1 && chessxy[xx][yy][tot] <= 7) || (player == "red" && chessxy[xx][yy][tot] >= 8 && chessxy[xx][yy][tot] <= 14)))x = xx, y = yy;
						else
						{
							x = xx, y = yy;
							break;
						}
					}
					else if (c == '1')break;//悔棋
				}
			}
		}
		else if (c == '1' && tot > 1)//悔棋
		{
			tot--;
			if (player == "red")player = "black", x = 3, y = 5;
			else player = "red", x = 8, y = 5;
		}
		cleardevice();
	}
}
void work()
{
	initgraph(650, 600);//打开画板
	play();
	cleardevice();
	draw_board();
	draw_chess(0, 0);
	system("pause");
	closegraph();
}
int main()
{
	work();
	return 0;
}
2023/4/8 16:53
加载中...