//在此之前,我要先确保各位都下载了easyx图形库,如果你还没有下载easyx图形库,请下载一个,安装包很小,在官网 Eeayx.cn 点击下载即可
1.演示工具:visual studio 2022
2.安装库:easyx图形库
废话不多说上代码:
#include<easyx.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define ROW 10
#define COL 9
#define INTERSIZE 50 //前面间隔
#define CHESSSIZE 70 //格子间隔
static int type = RED;// 设置先手后手
static int g = 1; // 记录判断先后手
struct chess
{
int x;
int y;
int id;
int type;
int river = 0;
};
struct START
{
int x;
int y;
};
enum piece
{
SPACE = -1,
車, 馬, 象, 士, 将, 砲, 卒,
俥, 马, 相, 仕, 帥, 炮, 兵,
BEGIN, END
};
struct chess map[ROW][COL];
struct START begin = { -1, -1 };
struct START end = { -1, -1 };
int start = BEGIN;
enum piece blackpiece[] = { 車, 馬, 象, 士, 将, 砲, 卒 };
enum piece redpiece[] = { 俥, 马, 相, 仕, 帥, 炮, 兵 };
const char* chessname[] = { "車", "馬", "象", "士", "将", "砲", "卒", "俥", "马", "相", "仕", "帥", "炮", "兵" };
void printfboard() //打印棋盘
{
setbkcolor(RGB(252, 215, 162)); //设置背景
cleardevice();
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 2); //设置线形
for (int i = 0; i < 10; i++) //划线
{
line(INTERSIZE, i*CHESSSIZE + INTERSIZE, 8 * CHESSSIZE + INTERSIZE, i*CHESSSIZE + INTERSIZE);
if (i < 9)
{
line(i*CHESSSIZE + INTERSIZE, INTERSIZE, i*CHESSSIZE + INTERSIZE, 9 * CHESSSIZE + INTERSIZE);
}
}
//绘制矩形框
rectangle(INTERSIZE - 5, INTERSIZE - 5, 8 * CHESSSIZE + INTERSIZE + 5, 9 * CHESSSIZE + INTERSIZE + 5);
//设置河道填充
setfillcolor(RGB(252, 215, 162));
fillrectangle(INTERSIZE, 4 * CHESSSIZE + INTERSIZE, 8 * CHESSSIZE + INTERSIZE, 5 * CHESSSIZE + INTERSIZE);
// 打印楚河汉界
char river[20] = "楚河 汉界";
settextcolor(BLACK);
settextstyle(50, 0, "楷体");
setbkmode(TRANSPARENT);// 背景色置为透明
outtextxy(INTERSIZE + 100, 4 * CHESSSIZE + INTERSIZE, river);
//上九宫格
line(3 * CHESSSIZE + INTERSIZE, INTERSIZE, 5 * CHESSSIZE + INTERSIZE, 2 * CHESSSIZE + INTERSIZE);
line(3 * CHESSSIZE + INTERSIZE, 2 * CHESSSIZE + INTERSIZE, 5 * CHESSSIZE + INTERSIZE, INTERSIZE);
//下九宫格
line(3 * CHESSSIZE + INTERSIZE, 7 * CHESSSIZE + INTERSIZE, 5 * CHESSSIZE + INTERSIZE, 9 * CHESSSIZE + INTERSIZE);
line(3 * CHESSSIZE + INTERSIZE, 9 * CHESSSIZE + INTERSIZE, 5 * CHESSSIZE + INTERSIZE, 7 * CHESSSIZE + INTERSIZE);
//下棋方显示
if (g % 2 == 1)
{
settextcolor(RED);
settextstyle(40, 0, "楷体");
outtextxy(640, 600, "红方落子");
}
if (g % 2 == 0)
{
settextcolor(BLACK);
settextstyle(40, 0, "楷体");
outtextxy(640, 110, "黑方落子");
}
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (map[i][j].id != SPACE)
{
fillcircle(map[i][j].x, map[i][j].y, 30);
fillcircle(map[i][j].x, map[i][j].y, 25);
settextstyle(30, 10, "黑体");
setbkmode(TRANSPARENT);
settextcolor(map[i][j].type);
outtextxy(map[i][j].x - 13, map[i][j].y - 13, chessname[map[i][j].id]);
}
}
}
}
void printfchess()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
enum piece chessid = SPACE;
int chesstype;
if (i <= 4)
{
chesstype = BLACK;
if (i == 0)// 设置最下面一排
{
if (j <= 4)
{
chessid = blackpiece[j];
}
else
{
chessid = blackpiece[8 - j];
}
}
if (i == 2)// 设置炮
{
if (j == 1 || j == 7)
{
chessid = blackpiece[5];
}
}
if (i == 3)// 设置兵
{
if ((j + 1) % 2 == 1)
{
chessid = blackpiece[6];
}
}
}
else
{
chesstype = RED;
if (9 - i == 0)
{
if (j <= 4)
{
chessid = redpiece[j];
}
else
{
chessid = redpiece[8 - j];
}
}
if (9 - i == 2)
{
if (j == 1 || j == 7)
{
chessid = redpiece[5];
}
}
if (9 - i == 3)
{
if ((j + 1) % 2 == 1)
{
chessid = redpiece[6];
}
}
}
map[i][j].river = 0;// 赋值各点位属性
map[i][j].id = chessid;
map[i][j].type = chesstype;
map[i][j].x = j*CHESSSIZE + INTERSIZE;
map[i][j].y = i*CHESSSIZE + INTERSIZE;
}
}
}
void Mousecontrol()
{
int n = 0;
ExMessage m;
peekmessage(&m, EX_MOUSE);// 记录鼠标命令
if (m.message == WM_LBUTTONDOWN)
{
int row = (m.y - INTERSIZE) / CHESSSIZE;
int col = (m.x - INTERSIZE) / CHESSSIZE;
if (m.x > map[row][col].x + 30 && m.y < map[row][col].y + 30)// 调整点击范围
col++;
if (m.x<map[row][col].x + 30 && m.y>map[row][col].y + 30)
row++;
if (m.x>map[row][col].x + 30 && m.y > map[row][col].y + 30)
{
row++;
col++;
}
if (start == BEGIN)// 记录开始结束坐标
{
begin.x = col;
begin.y = row;
if (map[begin.y][begin.x].id != SPACE&&map[begin.y][begin.x].type == type)
{
start = END;
}
}
else if (start == END)
{
start = BEGIN;
end.x = col;
end.y = row;
n = 1;// 设置一个循环
}
}
switch (map[begin.y][begin.x].id)// 设置各个棋子走法
{
case piece::将:
case piece::帥:
if (begin.y <= 4)
{
for (int i = 0; i <= 2; i++)
{
for (int j = 3; j <= 5; j++)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && end.y == i&&end.x == j && (begin.x == end.x || begin.y == end.y) &&
(abs(begin.x - end.x) == 1 || abs(begin.y - end.y) == 1) && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;//用于计数,判断有效次数
}
}
}
}
}
else
{
for (int i = 7; i <= 9; i++)
{
for (int j = 3; j <= 5; j++)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && end.y == i&&end.x == j && (begin.x == end.x || begin.y == end.y) &&
(abs(begin.x - end.x) == 1 || abs(begin.y - end.y) == 1) && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
}
}
break;
case piece::仕:
case piece::士:
if (begin.y <= 4)
{
for (int i = 0; i <= 2; i++)
{
for (int j = 3; j <= 5; j++)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && end.y == i&&end.x == j &&
(abs(begin.x - end.x) == 1 && abs(begin.y - end.y) == 1) && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
}
}
else
{
for (int i = 7; i <= 9; i++)
{
for (int j = 3; j <= 5; j++)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && end.y == i&&end.x == j &&
(abs(begin.x - end.x) == 1 && abs(begin.y - end.y) == 1) && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
}
}
break;
case piece::马:
case piece::馬:
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && ((map[begin.y][begin.x - 1].id == SPACE && (begin.x - end.x == 2 && abs(begin.y - end.y) == 1)) ||
(map[begin.y][begin.x + 1].id == SPACE && (begin.x - end.x == -2 && abs(begin.y - end.y) == 1)) ||
(map[begin.y - 1][begin.x].id == SPACE && (abs(begin.x - end.x) == 1 && begin.y - end.y == 2)) ||
(map[begin.y + 1][begin.x].id == SPACE && (abs(begin.x - end.x) == 1 && begin.y - end.y == -2)))
&& map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
break;
case piece::象:
case piece::相:
if (begin.y <= 4)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && ((map[begin.y - 1][begin.x - 1].id == SPACE && (begin.x - end.x == 2 && begin.y - end.y == 2)) ||
(map[begin.y + 1][begin.x + 1].id == SPACE && (begin.x - end.x == -2 && begin.y - end.y == -2)) ||
(map[begin.y - 1][begin.x + 1].id == SPACE && (begin.x - end.x == -2 && begin.y - end.y == 2)) ||
(map[begin.y + 1][begin.x].id == SPACE && (begin.x - end.x == 2 && begin.y - end.y == -2))) &&
end.y <= 4 && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
else if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && ((map[begin.y - 1][begin.x - 1].id == SPACE && (begin.x - end.x == 2 && begin.y - end.y == 2)) ||
(map[begin.y + 1][begin.x + 1].id == SPACE && (begin.x - end.x == -2 && begin.y - end.y == -2)) ||
(map[begin.y - 1][begin.x + 1].id == SPACE && (begin.x - end.x == -2 && begin.y - end.y == 2)) ||
(map[begin.y + 1][begin.x].id == SPACE && (begin.x - end.x == 2 && begin.y - end.y == -2))) &&
end.y>4 && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
break;
case piece::兵:
case piece::卒:
if (map[begin.y][begin.x].river != 2)
{
if (begin.y <= 4)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && begin.y - end.y == -1 && begin.x == end.x&& map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[begin.y][begin.x].river++;
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[end.y][end.x].river = map[begin.y][begin.x].river;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
else if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && begin.y - end.y == 1 && begin.x == end.x && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[begin.y][begin.x].river++;
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[end.y][end.x].river = map[begin.y][begin.x].river;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
else if (map[begin.y][begin.x].river == 2)
{
if (begin.y >4)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && (begin.y - end.y == -1 || abs(begin.x - end.x) == 1) &&
map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[end.y][end.x].river = map[begin.y][begin.x].river;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
else if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && (begin.y - end.y == 1 || abs(begin.x - end.x) == 1) &&
map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[end.y][end.x].river = map[begin.y][begin.x].river;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
break;
case piece::車:
case piece::俥:
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && (begin.x == end.x || begin.y == end.y) && map[begin.y][begin.x].type == type)
{
int a;
if (begin.y - end.y < 0)
{
for (a = begin.y; map[a + 1][begin.x].id == SPACE; a++)
{
;
}
if (end.y <= a + 1)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
if (begin.y - end.y > 0)
{
for (a = begin.y; map[a - 1][begin.x].id == SPACE; a--)
{
;
}
if (end.y >= a - 1)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
if (begin.x - end.x < 0)
{
for (a = begin.x; map[begin.y][a + 1].id == SPACE; a++)
{
;
}
if (end.x <= a + 1)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
if (begin.x - end.x > 0)
{
for (a = begin.x; map[begin.y][a - 1].id == SPACE; a--)
{
;
}
if (end.x >= a - 1)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
}
break;
case piece::炮:
case piece::砲:
int h = 0, a = 0;//用于记录是否中间有棋子和起始位置
if (n == 1 && end.y>begin.y&&end.x == begin.x)
{
for (a = begin.y + 1; a < end.y; a++)
{
if (map[a][begin.x].id != SPACE)
{
h++;//有棋子,自增
}
}
}
if (n == 1 && end.y<begin.y&&end.x == begin.x)
{
for (a = begin.y - 1; a > end.y; a--)
{
if (map[a][begin.x].id != SPACE)
{
h++;
}
}
}
if (n == 1 && end.y == begin.y&&end.x > begin.x)
{
for (a = begin.x + 1; a < end.x; a++)
{
if (map[begin.y][a].id != SPACE)
{
h++;
}
}
}
if (n == 1 && end.y == begin.y&&end.x < begin.x)
{
for (a = begin.x - 1; a > end.x; a--)
{
if (map[begin.y][a].id != SPACE)
{
h++;
}
}
}
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && (begin.x == end.x || begin.y == end.y) &&
map[begin.y][begin.x].type == type&&h == 1 && map[end.y][end.x].id != SPACE)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && (begin.x == end.x || begin.y == end.y) &&
map[begin.y][begin.x].type == type&&h == 0 && map[end.y][end.x].id == SPACE)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
break;
}
if (n == 2)// n==2,有效移动,使下棋方改变
{
g++;
if (g % 2 == 1)
{
type = RED;
}
else
type = BLACK;
}
else if (n == 1)// 无效移动,不改变下棋方,使下一次点击为begin,防止由于无效移动打乱点击消息
start = BEGIN;
}
int Judge()
{
int n = 0, m = 0;
for (int i = 0; i <= 2; i++)
{
for (int j = 3; j <= 5; j++)
{
if (map[i][j].id == 4)
n++;
}
}
for (int i = 7; i <= 9; i++)
{
for (int j = 3; j <= 5; j++)
{
if (map[i][j].id == 11)
m++;
}
}
if (m == 0 || n == 0)
{
type = RED;
g = 1;
initgraph(800, 500);
setbkcolor(RGB(5, 225, 250)); //设置背景
cleardevice();
setlinecolor(BLACK);
setfillcolor(RGB(255, 128, 192));
settextcolor(RGB(255, 225, 0));
fillrectangle(100, 300, 300, 400);
fillrectangle(500, 300, 700, 400);
if (n == 0)
{
settextstyle(100, 0, "楷体");
setbkmode(TRANSPARENT);// 背景色置为透明
outtextxy(100, 100, "恭喜红方获胜");
settextstyle(40, 0, "楷体");
outtextxy(120, 330, "继续游戏");
outtextxy(520, 330, "退出游戏");
}
if (m == 0)
{
settextstyle(100, 0, "楷体");
setbkmode(TRANSPARENT);// 背景色置为透明
outtextxy(100, 100, "恭喜黑方获胜");
settextstyle(40, 0, "楷体");
outtextxy(120, 330, "继续游戏");
outtextxy(520, 330, "退出游戏");
}
return 0;
}
else
{
return 1;
}
}
int choise() //返回最后选择结果
{
ExMessage m;
peekmessage(&m, EX_MOUSE);// 记录鼠标命令
if (m.message == WM_MOUSEMOVE)
{
if (m.y > 300 && m.y < 400 && m.x>100 && m.x < 300)//模拟按钮的效果
{
setfillcolor(RGB(255, 26, 140));
fillrectangle(100, 300, 300, 400);
settextcolor(RGB(255, 225, 0));
settextstyle(40, 0, "楷体");
outtextxy(120, 330, "继续游戏");
FlushBatchDraw();
}
else if ((m.y > 300 && m.y < 400 && m.x>500 && m.x < 700))
{
setfillcolor(RGB(255, 26, 140));
fillrectangle(500, 300, 700, 400);
settextcolor(RGB(255, 225, 0));
BeginBatchDraw();
settextstyle(40, 0, "楷体");
outtextxy(520, 330, "退出游戏");
FlushBatchDraw();
}
else
{
setfillcolor(RGB(255, 128, 192));
settextcolor(RGB(255, 225, 0));
fillrectangle(100, 300, 300, 400);
fillrectangle(500, 300, 700, 400);
settextstyle(40, 0, "楷体");
outtextxy(120, 330, "继续游戏");
outtextxy(520, 330, "退出游戏");
FlushBatchDraw();
}
}
if (m.message == WM_LBUTTONDOWN)
{
if (m.y > 300 && m.y < 400 && m.x>100 && m.x < 300)
{
m.x = 0;
m.y = 0;
return 1;
}
if (m.y > 300 && m.y < 400 && m.x>500 && m.x < 700)
{
return 2;
}
}
return 0;
}
//全代码:
#include<easyx.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define ROW 10
#define COL 9
#define INTERSIZE 50 //前面间隔
#define CHESSSIZE 70 //格子间隔
static int type = RED;// 设置先手后手
static int g = 1; // 记录判断先后手
struct chess
{
int x;
int y;
int id;
int type;
int river = 0;
};
struct START
{
int x;
int y;
};
enum piece
{
SPACE = -1,
車, 馬, 象, 士, 将, 砲, 卒,
俥, 马, 相, 仕, 帥, 炮, 兵,
BEGIN, END
};
struct chess map[ROW][COL];
struct START begin = { -1, -1 };
struct START end = { -1, -1 };
int start = BEGIN;
enum piece blackpiece[] = { 車, 馬, 象, 士, 将, 砲, 卒 };
enum piece redpiece[] = { 俥, 马, 相, 仕, 帥, 炮, 兵 };
const char* chessname[] = { "車", "馬", "象", "士", "将", "砲", "卒", "俥", "马", "相", "仕", "帥", "炮", "兵" };
void printfboard() //打印棋盘
{
setbkcolor(RGB(252, 215, 162)); //设置背景
cleardevice();
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 2); //设置线形
for (int i = 0; i < 10; i++) //划线
{
line(INTERSIZE, i*CHESSSIZE + INTERSIZE, 8 * CHESSSIZE + INTERSIZE, i*CHESSSIZE + INTERSIZE);
if (i < 9)
{
line(i*CHESSSIZE + INTERSIZE, INTERSIZE, i*CHESSSIZE + INTERSIZE, 9 * CHESSSIZE + INTERSIZE);
}
}
//绘制矩形框
rectangle(INTERSIZE - 5, INTERSIZE - 5, 8 * CHESSSIZE + INTERSIZE + 5, 9 * CHESSSIZE + INTERSIZE + 5);
//设置河道填充
setfillcolor(RGB(252, 215, 162));
fillrectangle(INTERSIZE, 4 * CHESSSIZE + INTERSIZE, 8 * CHESSSIZE + INTERSIZE, 5 * CHESSSIZE + INTERSIZE);
// 打印楚河汉界
char river[20] = "楚河 汉界";
settextcolor(BLACK);
settextstyle(50, 0, "楷体");
setbkmode(TRANSPARENT);// 背景色置为透明
outtextxy(INTERSIZE + 100, 4 * CHESSSIZE + INTERSIZE, river);
//上九宫格
line(3 * CHESSSIZE + INTERSIZE, INTERSIZE, 5 * CHESSSIZE + INTERSIZE, 2 * CHESSSIZE + INTERSIZE);
line(3 * CHESSSIZE + INTERSIZE, 2 * CHESSSIZE + INTERSIZE, 5 * CHESSSIZE + INTERSIZE, INTERSIZE);
//下九宫格
line(3 * CHESSSIZE + INTERSIZE, 7 * CHESSSIZE + INTERSIZE, 5 * CHESSSIZE + INTERSIZE, 9 * CHESSSIZE + INTERSIZE);
line(3 * CHESSSIZE + INTERSIZE, 9 * CHESSSIZE + INTERSIZE, 5 * CHESSSIZE + INTERSIZE, 7 * CHESSSIZE + INTERSIZE);
//下棋方显示
if (g % 2 == 1)
{
settextcolor(RED);
settextstyle(40, 0, "楷体");
outtextxy(640, 600, "红方落子");
}
if (g % 2 == 0)
{
settextcolor(BLACK);
settextstyle(40, 0, "楷体");
outtextxy(640, 110, "黑方落子");
}
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (map[i][j].id != SPACE)
{
fillcircle(map[i][j].x, map[i][j].y, 30);
fillcircle(map[i][j].x, map[i][j].y, 25);
settextstyle(30, 10, "黑体");
setbkmode(TRANSPARENT);
settextcolor(map[i][j].type);
outtextxy(map[i][j].x - 13, map[i][j].y - 13, chessname[map[i][j].id]);
}
}
}
}
void printfchess()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
enum piece chessid = SPACE;
int chesstype;
if (i <= 4)
{
chesstype = BLACK;
if (i == 0)// 设置最下面一排
{
if (j <= 4)
{
chessid = blackpiece[j];
}
else
{
chessid = blackpiece[8 - j];
}
}
if (i == 2)// 设置炮
{
if (j == 1 || j == 7)
{
chessid = blackpiece[5];
}
}
if (i == 3)// 设置兵
{
if ((j + 1) % 2 == 1)
{
chessid = blackpiece[6];
}
}
}
else
{
chesstype = RED;
if (9 - i == 0)
{
if (j <= 4)
{
chessid = redpiece[j];
}
else
{
chessid = redpiece[8 - j];
}
}
if (9 - i == 2)
{
if (j == 1 || j == 7)
{
chessid = redpiece[5];
}
}
if (9 - i == 3)
{
if ((j + 1) % 2 == 1)
{
chessid = redpiece[6];
}
}
}
map[i][j].river = 0;// 赋值各点位属性
map[i][j].id = chessid;
map[i][j].type = chesstype;
map[i][j].x = j*CHESSSIZE + INTERSIZE;
map[i][j].y = i*CHESSSIZE + INTERSIZE;
}
}
}
void Mousecontrol()
{
int n = 0;
ExMessage m;
peekmessage(&m, EX_MOUSE);// 记录鼠标命令
if (m.message == WM_LBUTTONDOWN)
{
int row = (m.y - INTERSIZE) / CHESSSIZE;
int col = (m.x - INTERSIZE) / CHESSSIZE;
if (m.x > map[row][col].x + 30 && m.y < map[row][col].y + 30)// 调整点击范围
col++;
if (m.x<map[row][col].x + 30 && m.y>map[row][col].y + 30)
row++;
if (m.x>map[row][col].x + 30 && m.y > map[row][col].y + 30)
{
row++;
col++;
}
if (start == BEGIN)// 记录开始结束坐标
{
begin.x = col;
begin.y = row;
if (map[begin.y][begin.x].id != SPACE&&map[begin.y][begin.x].type == type)
{
start = END;
}
}
else if (start == END)
{
start = BEGIN;
end.x = col;
end.y = row;
n = 1;// 设置一个循环
}
}
switch (map[begin.y][begin.x].id)// 设置各个棋子走法
{
case piece::将:
case piece::帥:
if (begin.y <= 4)
{
for (int i = 0; i <= 2; i++)
{
for (int j = 3; j <= 5; j++)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && end.y == i&&end.x == j && (begin.x == end.x || begin.y == end.y) &&
(abs(begin.x - end.x) == 1 || abs(begin.y - end.y) == 1) && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;//用于计数,判断有效次数
}
}
}
}
}
else
{
for (int i = 7; i <= 9; i++)
{
for (int j = 3; j <= 5; j++)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && end.y == i&&end.x == j && (begin.x == end.x || begin.y == end.y) &&
(abs(begin.x - end.x) == 1 || abs(begin.y - end.y) == 1) && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
}
}
break;
case piece::仕:
case piece::士:
if (begin.y <= 4)
{
for (int i = 0; i <= 2; i++)
{
for (int j = 3; j <= 5; j++)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && end.y == i&&end.x == j &&
(abs(begin.x - end.x) == 1 && abs(begin.y - end.y) == 1) && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
}
}
else
{
for (int i = 7; i <= 9; i++)
{
for (int j = 3; j <= 5; j++)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && end.y == i&&end.x == j &&
(abs(begin.x - end.x) == 1 && abs(begin.y - end.y) == 1) && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
}
}
break;
case piece::马:
case piece::馬:
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && ((map[begin.y][begin.x - 1].id == SPACE && (begin.x - end.x == 2 && abs(begin.y - end.y) == 1)) ||
(map[begin.y][begin.x + 1].id == SPACE && (begin.x - end.x == -2 && abs(begin.y - end.y) == 1)) ||
(map[begin.y - 1][begin.x].id == SPACE && (abs(begin.x - end.x) == 1 && begin.y - end.y == 2)) ||
(map[begin.y + 1][begin.x].id == SPACE && (abs(begin.x - end.x) == 1 && begin.y - end.y == -2)))
&& map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
break;
case piece::象:
case piece::相:
if (begin.y <= 4)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && ((map[begin.y - 1][begin.x - 1].id == SPACE && (begin.x - end.x == 2 && begin.y - end.y == 2)) ||
(map[begin.y + 1][begin.x + 1].id == SPACE && (begin.x - end.x == -2 && begin.y - end.y == -2)) ||
(map[begin.y - 1][begin.x + 1].id == SPACE && (begin.x - end.x == -2 && begin.y - end.y == 2)) ||
(map[begin.y + 1][begin.x].id == SPACE && (begin.x - end.x == 2 && begin.y - end.y == -2))) &&
end.y <= 4 && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
else if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && ((map[begin.y - 1][begin.x - 1].id == SPACE && (begin.x - end.x == 2 && begin.y - end.y == 2)) ||
(map[begin.y + 1][begin.x + 1].id == SPACE && (begin.x - end.x == -2 && begin.y - end.y == -2)) ||
(map[begin.y - 1][begin.x + 1].id == SPACE && (begin.x - end.x == -2 && begin.y - end.y == 2)) ||
(map[begin.y + 1][begin.x].id == SPACE && (begin.x - end.x == 2 && begin.y - end.y == -2))) &&
end.y>4 && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
break;
case piece::兵:
case piece::卒:
if (map[begin.y][begin.x].river != 2)
{
if (begin.y <= 4)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && begin.y - end.y == -1 && begin.x == end.x&& map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[begin.y][begin.x].river++;
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[end.y][end.x].river = map[begin.y][begin.x].river;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
else if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && begin.y - end.y == 1 && begin.x == end.x && map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[begin.y][begin.x].river++;
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[end.y][end.x].river = map[begin.y][begin.x].river;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
else if (map[begin.y][begin.x].river == 2)
{
if (begin.y >4)
{
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && (begin.y - end.y == -1 || abs(begin.x - end.x) == 1) &&
map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[end.y][end.x].river = map[begin.y][begin.x].river;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
else if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && (begin.y - end.y == 1 || abs(begin.x - end.x) == 1) &&
map[begin.y][begin.x].type == type)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[end.y][end.x].river = map[begin.y][begin.x].river;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
break;
case piece::車:
case piece::俥:
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && (begin.x == end.x || begin.y == end.y) && map[begin.y][begin.x].type == type)
{
int a;
if (begin.y - end.y < 0)
{
for (a = begin.y; map[a + 1][begin.x].id == SPACE; a++)
{
;
}
if (end.y <= a + 1)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
if (begin.y - end.y > 0)
{
for (a = begin.y; map[a - 1][begin.x].id == SPACE; a--)
{
;
}
if (end.y >= a - 1)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
if (begin.x - end.x < 0)
{
for (a = begin.x; map[begin.y][a + 1].id == SPACE; a++)
{
;
}
if (end.x <= a + 1)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
if (begin.x - end.x > 0)
{
for (a = begin.x; map[begin.y][a - 1].id == SPACE; a--)
{
;
}
if (end.x >= a - 1)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
}
}
break;
case piece::炮:
case piece::砲:
int h = 0, a = 0;//用于记录是否中间有棋子和起始位置
if (n == 1 && end.y>begin.y&&end.x == begin.x)
{
for (a = begin.y + 1; a < end.y; a++)
{
if (map[a][begin.x].id != SPACE)
{
h++;//有棋子,自增
}
}
}
if (n == 1 && end.y<begin.y&&end.x == begin.x)
{
for (a = begin.y - 1; a > end.y; a--)
{
if (map[a][begin.x].id != SPACE)
{
h++;
}
}
}
if (n == 1 && end.y == begin.y&&end.x > begin.x)
{
for (a = begin.x + 1; a < end.x; a++)
{
if (map[begin.y][a].id != SPACE)
{
h++;
}
}
}
if (n == 1 && end.y == begin.y&&end.x < begin.x)
{
for (a = begin.x - 1; a > end.x; a--)
{
if (map[begin.y][a].id != SPACE)
{
h++;
}
}
}
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && (begin.x == end.x || begin.y == end.y) &&
map[begin.y][begin.x].type == type&&h == 1 && map[end.y][end.x].id != SPACE)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
if (begin.x != -1 && end.x != -1 && !((begin.x == end.x) && (begin.y == end.y))
&& n == 1 && (begin.x == end.x || begin.y == end.y) &&
map[begin.y][begin.x].type == type&&h == 0 && map[end.y][end.x].id == SPACE)
{
if (map[end.y][end.x].id == SPACE || map[begin.y][begin.x].type != map[end.y][end.x].type)
{
map[end.y][end.x].id = map[begin.y][begin.x].id;
map[end.y][end.x].type = map[begin.y][begin.x].type;
map[begin.y][begin.x].id = SPACE;
n++;
}
}
break;
}
if (n == 2)// n==2,有效移动,使下棋方改变
{
g++;
if (g % 2 == 1)
{
type = RED;
}
else
type = BLACK;
}
else if (n == 1)// 无效移动,不改变下棋方,使下一次点击为begin,防止由于无效移动打乱点击消息
start = BEGIN;
}
int Judge()
{
int n = 0, m = 0;
for (int i = 0; i <= 2; i++)
{
for (int j = 3; j <= 5; j++)
{
if (map[i][j].id == 4)
n++;
}
}
for (int i = 7; i <= 9; i++)
{
for (int j = 3; j <= 5; j++)
{
if (map[i][j].id == 11)
m++;
}
}
if (m == 0 || n == 0)
{
type = RED;
g = 1;
initgraph(800, 500);
setbkcolor(RGB(5, 225, 250)); //设置背景
cleardevice();
setlinecolor(BLACK);
setfillcolor(RGB(255, 128, 192));
settextcolor(RGB(255, 225, 0));
fillrectangle(100, 300, 300, 400);
fillrectangle(500, 300, 700, 400);
if (n == 0)
{
settextstyle(100, 0, "楷体");
setbkmode(TRANSPARENT);// 背景色置为透明
outtextxy(100, 100, "恭喜红方获胜");
settextstyle(40, 0, "楷体");
outtextxy(120, 330, "继续游戏");
outtextxy(520, 330, "退出游戏");
}
if (m == 0)
{
settextstyle(100, 0, "楷体");
setbkmode(TRANSPARENT);// 背景色置为透明
outtextxy(100, 100, "恭喜黑方获胜");
settextstyle(40, 0, "楷体");
outtextxy(120, 330, "继续游戏");
outtextxy(520, 330, "退出游戏");
}
return 0;
}
else
{
return 1;
}
}
int choise() //返回最后选择结果
{
ExMessage m;
peekmessage(&m, EX_MOUSE);// 记录鼠标命令
if (m.message == WM_MOUSEMOVE)
{
if (m.y > 300 && m.y < 400 && m.x>100 && m.x < 300)//模拟按钮的效果
{
setfillcolor(RGB(255, 26, 140));
fillrectangle(100, 300, 300, 400);
settextcolor(RGB(255, 225, 0));
settextstyle(40, 0, "楷体");
outtextxy(120, 330, "继续游戏");
FlushBatchDraw();
}
else if ((m.y > 300 && m.y < 400 && m.x>500 && m.x < 700))
{
setfillcolor(RGB(255, 26, 140));
fillrectangle(500, 300, 700, 400);
settextcolor(RGB(255, 225, 0));
BeginBatchDraw();
settextstyle(40, 0, "楷体");
outtextxy(520, 330, "退出游戏");
FlushBatchDraw();
}
else
{
setfillcolor(RGB(255, 128, 192));
settextcolor(RGB(255, 225, 0));
fillrectangle(100, 300, 300, 400);
fillrectangle(500, 300, 700, 400);
settextstyle(40, 0, "楷体");
outtextxy(120, 330, "继续游戏");
outtextxy(520, 330, "退出游戏");
FlushBatchDraw();
}
}
if (m.message == WM_LBUTTONDOWN)
{
if (m.y > 300 && m.y < 400 && m.x>100 && m.x < 300)
{
m.x = 0;
m.y = 0;
return 1;
}
if (m.y > 300 && m.y < 400 && m.x>500 && m.x < 700)
{
return 2;
}
}
return 0;
}