C language project practice: Zero basic project of Chinese chess, 210 lines of source code example

This article mainly introduces in detail the implementation of C language - "Chinese chess project". The example code in this article is very detailed and has certain reference value. Interested partners can refer to it!

Introduction to the game:

Chinese chess is a two person confrontational game in which two people take turns to win by "dying" or "trapped" the other party's general (handsome) in accordance with the combat thought of "subduing the soldiers without war and being good" in the art of war of ancient Sun Tzu. In the game, the one who holds the red chess will go first, and the two sides will take turns to take a move until they win, lose and make peace, and the game will end. In chess, people can improve their thinking ability from the changes of complex relations such as attack and defense, virtual and real, whole and part.

The chess rules of each kind of chess piece. If you don't understand anything, you can understand it. It's very simple. If you want to make this project, it is essential to understand the rules.

Compilation environment of the project: VS2019/VS2013;

Plug in: the graphics library plug-in easyX, which involves picture materials, can be found by Baidu or collected at the end of the text;

Rendering display

Supporting tutorial: Chinese chess game tutorial - BiliBilihttps://www.bilibili.com/video/BV1DC4y1H7xo?from=search&seid=7193207657102750553&spm_id_from=333.337.0.0

Source code example:

#include<stdio.h>
#include<graphics.h>
#define INTERVAL 50 / / previous interval
#define CHESS_GRID_SIZE 70 / / grid width
#define ROW  10
#define COL  9
enum PIECES//Enumerate pieces
{
	Car,Horses,as,Scholar,take,Cannon,Pawn,
	Bamboo,horse,mutually,Official,Handsome,cannon,soldier,
	SPACE,BEGIN,END
};
//Red and black square pieces
int redChess[] = { Car, Horses, as, Scholar, take, Cannon, Pawn};
int blackChess[] = { Bamboo, horse, mutually, Official, Handsome, cannon, soldier };
const char*chessName[] = { "Car", "Horses", "as", "Scholar", "take", "Cannon", "Pawn", "Bamboo", "horse", "mutually", "Official", "Handsome", "cannon", "soldier" };
struct Chess//Chess piece attribute
{
	int x;
	int y;
	int id;//Which piece
	int type;//Which chess piece is red or black?
	bool river;//Judge whether the soldier has crossed the river
};
struct Chess  map[ROW][COL];//Structure array, which is used to save the information of each point
POINT begin = { -1, -1 }, end = { -1, -1 };//Save the array subscript of two clicks before and after
int state = BEGIN;
//Initialize game data
void GameInit()
{
	for (int i = 0; i < ROW; i++)
	{
		for (int k = 0; k < COL; k++)
		{
			int chessname = SPACE;
			int mcolor = BLACK;
			//black 
			if (i <= 4)
			{
				mcolor = BLACK;
				//Initialize the pieces in the first line,
				if (i == 0)
				{
					if (k <= 4)
					{
						chessname = blackChess[k];
					}
					else
					{
						chessname = blackChess[8-k];
					}
				}
				//Set gun
				if (i == 2 && (k == 1 || k == 7))
				{
					chessname = blackChess[Cannon];
				}
				//Set up small soldiers
				if (i == 3 && k % 2 == 0)
				{
					chessname = blackChess[Pawn];
				}

			}
			//Red chess
			else 
			{
				mcolor = RED;
				//Initialize the pieces in the first line,
				if (i == 9)
				{
					if (k <= 4)
					{
						chessname = redChess[k];
					}
					else
					{
						chessname = redChess[8 - k];
					}
				}
				//Set gun
				if (i == 7 && (k == 1 || k == 7))
				{
					chessname = redChess[cannon];
				}
				//Set up small soldiers
				if (i == 6 && k % 2 == 0)
				{
					chessname = redChess[soldier];
				}
			}
			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;
		}
	}
}
//Drawing function of game
void GameDraw()
{
	//Set background color red black 
	setbkcolor(RGB(252, 215, 162));
	cleardevice();

	//Draw chessboard
	setlinecolor(BLACK);
	setlinestyle(PS_SOLID, 2);
	for (int i = 0; i < 10; i++)
	{
		//Draw a horizontal line
		line(INTERVAL, i*CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, i*CHESS_GRID_SIZE + INTERVAL);
		//Draw a vertical line
		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);
	//Chu River Han boundary display
	setfillcolor(RGB(252, 215, 162));
	fillrectangle(INTERVAL, 4 * CHESS_GRID_SIZE + INTERVAL, 8 * CHESS_GRID_SIZE + INTERVAL, 5 * CHESS_GRID_SIZE + INTERVAL);
	//display text
	char river[20] = "Chu River        Han Dynasty";
	settextstyle(50, 0, "Regular script");
	settextcolor(BLACK);
	setbkmode(TRANSPARENT);
	outtextxy(INTERVAL+100, 4 * CHESS_GRID_SIZE + INTERVAL+10, river);
	//Draw Jiugong grid
		//Draw above
	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);
	   //Draw below
	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);
	//Draw chess pieces
	settextstyle(30, 0, "Regular script");
	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)
		{
			//Gets the subscript of the array clicked by the mouse
			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()
{	
	//Create a graphics window
	initgraph(800,800,SHOWCONSOLE);
	GameInit();
	printf("Welcome to Mr. stubborn stone's class, Chinese chess");
	BeginBatchDraw();
	while (1)
	{
		GameDraw();
		FlushBatchDraw();
		MouseControl();
		chessMove();
	}
	return 0;
}

Write at the end: for those who are ready to learn C/C + + programming, if you want to better improve your core programming ability (internal skill), you might as well start now!

C language c + + programming learning and communication circle, QQ group: 829164294[ Click to enter WeChat official account: C language programming learning base

Sorting and sharing (source code, project practice video, project notes, basic introductory tutorial)

Welcome to change careers and learn programming partners. Use more materials to learn and grow faster than you think!

Programming learning video sharing:

 

 

Keywords: C Programming

Added by niesom on Fri, 12 Nov 2021 13:27:58 +0200