程序代码:
#include<stdio.h>
#include<easyx.h>
#define ROW 10
#define COL 9
#define INTERVAL 35 //边距
#define GRID_SIZE 70 //格子宽度
enum Pieces //棋子
{
NONE = -1,
俥,马,相,仕,帥,炮,兵, //红子
車,馬,象,士,将,砲,卒, //绿子
BEGIN,END,
};
//给id赋值
enum Pieces greenChess[] = {車,馬,象,士,将,砲,卒};
enum Pieces redChess[] = {俥,马,相,仕,帥,炮,兵};
//绘制时转化成字符串
const char* ChessName[]= {"俥","马","相","仕","帥","炮","兵","車","馬","象","士","将","砲","卒"};
//每一个棋子的属性
struct Chess
{
enum Pieces id; //棋子名称
DWORD type; //棋子类型,红?绿?
short x;
short y;
bool isRiver; //是否过河
};
//游戏地图
struct Chess map[ROW][COL];
//初始化数据
void init()
{
//遍历地图
for(size_t i=0; i<ROW; i++)
{
size_t temp = 0;
for(size_t k=0; k<COL; k++)
{
map[i][k].id = NONE;
if(i<=4) //绿棋子
{
map[i][k].type = GREEN;
if(i == 0) //放置第一行的棋子
{
if(k<=4)
{
temp=k;
}
else
{
temp = 4-(k-4);
}
map[i][k].id=greenChess[temp];
}
if(i==2 && (k==1 || k==7))
{
map[i][k].id=greenChess[5];
}
if(i==3 && (k%2) ==0)
{
map[i][k].id=greenChess[6];
}
}
else //红棋子
{
map[i][k].type = RED;
if(i == 9) //放置第一行的棋子
{
if(k<=4)
{
temp=k;
}
else
{
temp = 4-(k-4);
}
map[i][k].id=redChess[temp];
}
if(i==7 && (k==1 || k==7))
{
map[i][k].id=redChess[5];
}
if(i==6 && (k%2) ==0)
{
map[i][k].id=redChess[6];
}
}
map[i][k].isRiver = false;
map[i][k].x = k*GRID_SIZE+INTERVAL;
map[i][k].y = i*GRID_SIZE+INTERVAL;
}
}
}
//绘制
void draw()
{
setlinestyle(PS_SOLID,3);
//设置文字样式
settextstyle(36,0,"楷体");
for(size_t i=0; i<ROW; i++)
{
for(size_t k=0; k<COL; k++)
{
if(map[i][k].id == NONE)
continue;
settextcolor(map[i][k].type);
setlinecolor(map[i][k].type);
//绘制棋子
fillcircle(map[i][k].x, map[i][k].y, 30);
outtextxy(map[i][k].x-18, map[i][k].y-18, ChessName[map[i][k].id]);
}
}
}
void main()
{
//创建图形窗口
initgraph(632,700);
//设置背景模式
setbkmode(TRANSPARENT);
//贴棋盘
IMAGE img_board;
loadimage(&img_board,"./res/ChessBoard.png");
putimage(0,0,&img_board);
init();
draw();
getchar();
}