孙嘉优秀作者
原创内容 来源:小居数码网 时间:2024-07-28 17:56:01 阅读() 收藏:27 分享:71 爆
导读:您正在阅读的是关于【数码知识】的问题,本文由科普作家协会,生活小能手,著名生活达人等整理监督编写。本文有1676个文字,大小约为6KB,预计阅读时间5分钟。
这篇文章主要为大家详细介绍了C语言实现——《中国象棋项目》,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下!
游戏介绍:
中国象棋是由两人轮流走子,在战法上遵循古代孙子兵法中的“不战而屈人之兵,善之善者也”的作战思想,以“将死”或“困毙”对方将(帅)为胜的一种二人对抗性游戏。对局时,由执红棋的一方先走,双方轮流各走一招,直至分出胜、负、和,对局即终了。在棋战中,人们可以从攻与防、虚与实、整体与局部等复杂关系的变化中提升思维能力。
每种棋子的行棋规则大家如果有不懂的地方,可以去了解一下,都很简单。你想要做出本项目,了解规则是必不可少的。
本项目编译环境:VS2019/VS2013;
插件:图形库插件easyX,涉及图片素材可以自行百度找也可以关注文末领取;
效果图展示
配套讲解教程:「链接」
源代码示例:
#include<stdio.h>#include<graphics.h>#define INTERVAL 50 //前面的间隔#define CHESS_GRID_SIZE 70 //格子宽度#define ROW 10#define COL 9enum 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[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 %dn", 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/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
上面就是小居数码小编今天给大家介绍的关于(象棋的编程代码公开)的全部内容,希望可以帮助到你,想了解更多关于数码知识的问题,欢迎关注我们,并收藏,转发,分享。
94%的朋友还想知道的:
(562)个朋友认为回复得到帮助。
部分文章信息来源于以及网友投稿,转载请说明出处。
本文标题:c++中国象棋源代码(象棋的编程代码公开):http://sjzlt.cn/shuma/152373.html