这篇文章主要为大家详细介绍了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 %d\n", 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++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
编程学习书籍分享:
编程学习视频分享:
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
对于C/C++感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C++的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/89226.html