這篇文章主要為大家詳細介紹了C語言實現——《中國象棋項目》,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下!
游戲介紹:
中國象棋是由兩人輪流走子,在戰法上遵循古代孫子兵法中的“不戰而屈人之兵,善之善者也”的作戰思想,以“將死”或“困斃”對方將(帥)為勝的一種二人對抗性游戲。對局時,由執紅棋的一方先走,雙方輪流各走一招,直至分出勝、負、和,對局即終了。在棋戰中,人們可以從攻與防、虛與實、整體與局部等復雜關系的變化中提升思維能力。
每種棋子的行棋規則大家如果有不懂的地方,可以去了解一下,都很簡單。你想要做出本項目,了解規則是必不可少的。
本項目編譯環境:VS2019/VS2013;
插件:圖形庫插件easyX,涉及圖片素材可以自行百度找也可以關注文末領??;
效果圖展示
配套講解教程:中國象棋游戲教程——嗶哩嗶哩
源代碼示例:
enum PIECES//枚舉棋子
{
車,馬,象,士,將,砲,卒,
俥,馬,相,仕,帥,炮,兵,
SPACE,BEGIN,END
};
//紅黑方棋子、
int redChess[] = { 車, 馬, 象, 士, 將, 砲, 卒};
int blackChess[] = { 俥, 馬, 相, 仕, 帥, 炮, 兵 };
const char*chessName[] = { "車", "馬", "象", "士", "將", "砲", "卒", "俥", "馬", "相", "仕", "帥", "炮", "兵" };
struct Chess//棋子屬性
{
int x;
int y;
int id;//哪個棋子
int type;//是哪一方的棋子 紅,黑?
bool river;//判斷小兵是否過了河
};
struct Chess map[ROW][COL];//結構體數組,用來保存每一個點的信息
POINT begin = { -1, -1 }, end = { -1, -1 };//保存前后兩次點擊的數組下標
int state = BEGIN;
//初始化游戲數據
void GameInit()
{
for (int i = 0; i < ROW; i++)
{
for (int k = 0; k < COL; k++)
{
int chessname = SPACE;
int mcolor = BLACK;
//黑棋
if (i <= 4)
{
mcolor = BLACK;
//初始化第一行的棋子,
if (i == 0)
{
if (k <= 4)
{
chessname = blackChess[k];
}
else
{
chessname = blackChess[8-k];
}
}
//設置炮
if (i == 2 && (k == 1 || k == 7))
{
chessname = blackChess[砲];
}
//設置小兵
if (i == 3 && k % 2 == 0)
{
chessname = blackChess[卒];
}
}
//紅棋
else
{
mcolor = RED;
//初始化第一行的棋子,
if (i == 9)
{
if (k <= 4)
{
chessname = redChess[k];
}
else
{
chessname = redChess[8 - k];
}
}
//設置炮
if (i == 7 && (k == 1 || k == 7))
{
chessname = redChess[炮];
}
//設置小兵
if (i == 6 && k % 2 == 0)
{
chessname = redChess[兵];
}
}
map[i][k].id = chessname;
map[i][k].river = false;
map[i][k].type = mcolor;
map[i][k].x = k*CHESS_GRID_SIZE + INTERVAL;
map[i][k].y = i*CHESS_GRID_SIZE + INTERVAL;
}
}
}
//游戲的繪制函數
void GameDraw()
{
//設置背景顏色 red black
setbkcolor(RGB(252, 215, 162));
cleardevice();
//繪制棋盤
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 2);
for (int i = 0; i < 10; i++)
{
//畫橫線
line(INTERVAL, i*CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, i*CHESS_GRID_SIZE + INTERVAL);
//畫豎線
if (i < 9)
{
line(i*CHESS_GRID_SIZE + INTERVAL, INTERVAL, i*CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
}
}
rectangle(INTERVAL - 5, INTERVAL - 5, 8 * CHESS_GRID_SIZE + INTERVAL+5, 5+9 * CHESS_GRID_SIZE + INTERVAL);
//楚河漢界顯示
setfillcolor(RGB(252, 215, 162));
fillrectangle(INTERVAL, 4 * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL);
//顯示文字
char river[20] = "楚河 漢界";
settextstyle(50, 0, "楷體");
settextcolor(BLACK);
setbkmode(TRANSPARENT);
outtextxy(INTERVAL+100, 4 * CHESS_GRID_SIZE + INTERVAL+10, river);
//畫九宮格
//畫上面
line(3 * CHESS_GRID_SIZE + INTERVAL, INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL);
line(3 * CHESS_GRID_SIZE + INTERVAL, 2 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, INTERVAL);
//畫下面
line(3 * CHESS_GRID_SIZE + INTERVAL, 7 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE + INTERVAL);
line(3 * CHESS_GRID_SIZE + INTERVAL, 9 * CHESS_GRID_SIZE+INTERVAL,5*CHESS_GRID_SIZE+INTERVAL,7*CHESS_GRID_SIZE+INTERVAL);
//畫棋子
settextstyle(30, 0, "楷體");
for (int i = 0; i < ROW; i++)
{
for (int k = 0; k < COL; k++)
{
if (map[i][k].id != SPACE)
{
setlinecolor(map[i][k].type);
fillcircle(map[i][k].x, map[i][k].y, 30);
fillcircle(map[i][k].x, map[i][k].y, 25);
settextcolor(map[i][k].type);
outtextxy(map[i][k].x-10, map[i][k].y-10, chessName[map[i][k].id]);
}
}
}
}
void MouseControl()
{
if (MouseHit())
{
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN)
{
//獲取鼠標點擊的數組的下標
int row = (msg.y-INTERVAL)/CHESS_GRID_SIZE;
int col = (msg.x-INTERVAL)/CHESS_GRID_SIZE;
if (state == BEGIN)
{
state = END;
begin.x = row;
begin.y = col;
}
else if (state == END)
{
state = BEGIN;
begin.x = row;
begin.y = col;
}
printf("%d,%d %d ", row, col, state);
}
}
}
void chessMove()
{
if (begin.x != -1 && end.x != -1 && !(begin.x == end.x &&begin.y == end.y))
{
map[end.x][end.y].id = map[begin.x][begin.y].id;
map[end.x][end.y].type = map[begin.x][begin.y].type;
map[end.x][end.y].river = map[begin.x][begin.y].river;
map[begin.x][begin.y].id = SPACE;
}
}
int main555()
{
//創建一個圖形窗口
initgraph(800,800,SHOWCONSOLE);
GameInit();
printf("歡迎來到頑石老師課堂,中國象棋");
BeginBatchDraw();
while (1)
{
GameDraw();
FlushBatchDraw();
MouseControl();
chessMove();
}
return 0;
}
-
C語言
+關注
關注
180文章
7614瀏覽量
137450 -
代碼
+關注
關注
30文章
4823瀏覽量
68904
原文標題:C語言項目實戰:《中國象棋》零基礎項目!210 行源代碼示例
文章出處:【微信號:cyuyanxuexi,微信公眾號:C語言編程學習基地】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論