C/C + + game project: a Chinese chess tutorial that Chinese programmers must know

        Chinese chess is a very popular game in China. According to their understanding of the form ...
Rules of the game
Chess rules:
Eating rules:
Winning and losing judgment:
        Chinese chess is a very popular game in China. According to their understanding of the form of the chess game and their mastery of the rules of chess, the two sides mobilize vehicles and horses, organize troops, coordinate operations, and carry out symbolic military battles on the specific battlefield of the chessboard. Chess, also known as "Xiangqi", is also known as "Chinese chess" in order to distinguish "chess". Chinese chess has a long history in China and belongs to a kind of two person confrontation game. Due to its simple tools and strong interest, it has become a very popular chess activity.

Rules of the game

Chess rules:

Move range of chess pieces: it can only move in the nine palaces. Move rules: each step can only move a little horizontally or vertically. Special rules: shuaihe is not allowed to directly face each other on the same straight line (there is no chess piece in the middle). If one party has occupied the position first, the other party must avoid it, Otherwise, even if you lose the official / scholar's movement range: you can only move in the nine palaces. Movement rules: you can only move one point along the diagonal direction at each step. Phase / image movement range: one side of the river boundary movement rules: you can only move two points along the diagonal direction at each step. You can use the Tian character in Chinese characters to vividly express: the diagonal of Tian character lattice, commonly known as phase (image) walking Tian character. When there are chessmen in the walking route of Xiang (Xiang), that is, there are chessmen in the center of Tian Zi (whether your own or the other party's chessmen), you are not allowed to walk past. It is commonly known as blocking Xiang (Xiang) eyes. Horse movement range: any position movement rule: each step can only move a little horizontally or vertically, and then move left or right diagonally. The Japanese character in Chinese characters can be used to describe the way a horse walks, commonly known as the horse walks the Japanese character (diagonal). When the horse walks, if there are other chess pieces (either your own or the other's) blocking the first step straight or horizontal, you are not allowed to walk over, commonly known as broken horse legs. Vehicle movement range: any position movement rule: you can move any unimpeded point shot / shot horizontally or vertically. Movement range: any position movement rule: it moves very similar to the vehicle, but it must skip a piece to eat the opponent's piece. Soldier / pawn movement range: movement rule at any position: before crossing the river boundary, you can only move forward a little at each step. After crossing the river boundary, the ability to move left and right is increased. Soldiers (pawns) are not allowed to move backward.

Eating rules:

No matter what chess pieces are, they can usually eat each other's chess pieces as long as there are each other's chess pieces in the parts that can be reached according to the chess rules.

The only exception is the gun's chess eating method, which is relatively special. It needs to be separated by chess pieces (either your own or the other's) to eat the other's chess pieces.

Winning and losing judgment:

The handsome (will) party who is "dying" or "trapped" by the other party will lose.

The party who declares defeat loses.

Today, I will take you step by step in C language to complete a fun and interesting Chinese chess game that you can learn to play with your friends.

PS: to install easyx graphics library #include < easyx. H >     

The development tool is VS2013

Before that, I would like to explain to you that because this is a relatively large project, it will be difficult to show all the codes, so I cut most of the main codes to let you understand the logical idea of realizing this project. I hope you can understand it. Get the complete code at the bottom of the article

Step 1: create a project and put the prepared material resources under the same level directory (the materials can be collected at the bottom of the article), as shown in the figure:

Step 2: next is our main function main.Cpp. Create a window, paste a checkerboard diagram, and add double buffer drawing to prevent flickering:

int main() { //Create graphics window initgraph(740, 820,EW_SHOWCONSOLE); //Set background mode setbkmode(TRANSPARENT); //Paste chessboard IMAGE img_board; loadimage(&img_board, "./res/ChessBoard.png"); init(); //Double buffer drawing to prevent flashing BeginBatchDraw(); while (true) { cleardevice(); putimage(0, 0, &img_board); draw(); mouseEvent(); FlushBatchDraw(); } EndBatchDraw(); getchar(); return 0; }

Step 3: use the drawing to find the coordinates of each point and draw the chess pieces, as well as the black and red chess pieces and the chess pieces crossing the river:

enum Pieces //piece { NONE = -1, Car, Horses, as, Scholar, take, Cannon, Pawn, Bamboo, horse, mutually, Official, Handsome, cannon, soldier, BEGIN, END, }; //Assign value to id enum Pieces redChess[] = { Car, Horses, as, Scholar, take, Cannon, Pawn }; enum Pieces blackChess[] = { Bamboo, horse, mutually, Official, Handsome, cannon, soldier }; //Convert to string when drawing const char* ChessName[] = { "Car","Horses","as","Scholar","take","Cannon","Pawn","Bamboo", "horse", "mutually", "Official", "Handsome", "cannon", "soldier" }; //Attributes of each piece struct Chess { enum Pieces id; //Chess piece name DWORD type; //Chess piece type, red? Black? short x; short y; bool isRiver; //Did you cross the river };

Step 4: macro definition #define ROW 10   # define COL 9 draws a map of ten columns and nine rows, initializes the data, and sets the special movement rules of chess pieces:

//Game map struct Chess map[ROW][COL]; struct State { int begr; int begc; int endr; int endc; int state; }state = {-1,-1,-1,-1,BEGIN}; void chessMove(); //Print array void show() { for (size_t i = 0; i < ROW; i++) { for (size_t k = 0; k < COL; k++) { printf("%2d ", map[i][k].id); } printf("\n"); } } //Initialization data void init() { //Traverse map 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; //Set the chess piece to no first if (i <= 4) //Black chess piece { map[i][k].type = BLACK; if (i == 0) //Place the first row of pieces { //0 1 2 3 4 if (k <= 4) { temp = k; } // 3 2 1 0 else { // k == 5 temp = 4 - (k - 4); /* 4 - (5-4) //3 4 - (6-4) //2 4 - (7-4) //1 4 - (8-4) //0 */ } map[i][k].id = blackChess[temp]; } //Set gun if (i == 2 && (k == 1 || k == 7)) { map[i][k].id = blackChess[5]; } //Set soldier if (i == 3 && k % 2 == 0) { map[i][k].id = blackChess[6]; } } else //Red chess { map[i][k].type = RED; if (i == 9) //Place the first row of pieces { //0 1 2 3 4 if (k <= 4) { temp = k; } // 3 2 1 0 else { // k == 5 temp = 4 - (k - 4); /* 4 - (5-4) //3 4 - (6-4) //2 4 - (7-4) //1 4 - (8-4) //0 */ } map[i][k].id = redChess[temp]; } //Set gun if (i == 7 && (k == 1 || k == 7)) { map[i][k].id = redChess[5]; } //Set soldier 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; } } } //draw void draw() { setfillcolor(RGB(252, 215, 162)); setlinestyle(PS_SOLID, 2); //Style text settextstyle(30, 0, "Regular script"); 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); //Draw chess pieces fillcircle(map[i][k].x, map[i][k].y, 30); fillcircle(map[i][k].x, map[i][k].y, 25); outtextxy(map[i][k].x - 15, map[i][k].y - 15, ChessName[map[i][k].id]); } } }

Step 5: set and obtain mouse operation:

//Mouse operation void mouseEvent() { ExMessage msg; //Define message structure variables if(peekmessage(&msg, EM_MOUSE)) { if (msg.message == WM_LBUTTONDOWN) //Press the left mouse button { //The subscript of the clicked array is obtained through the mouse coordinates //k * GRID_SIZE + INTERVAL = x; int col = (msg.x - INTERVAL) / GRID_SIZE; int row = (msg.y - INTERVAL) / GRID_SIZE; //Subscript calibration if (msg.x > map[row][col].x + 30 && msg.y < map[row][col].y + 30) { col++; } if (msg.x < map[row][col].x + 30 && msg.y > map[row][col].y + 30) { row++; } if (msg.x > map[row][col].x + 30 && msg.y > map[row][col].y + 30) { row++; col++; } //printf("(%d %d)\n", row, col); if (state.state == BEGIN) { state.begr = row; state.begc = col; state.state = END; } else if (state.state == END) { state.endr = row; state.endc = col; state.state = BEGIN; } chessMove(); } } } int hasBlock(struct State* state) { int cnt = 0; state->begr; state->begc; state->endr; state->endc; */ return cnt; }

Step 6: set the movement of chess pieces:

//Move chess pieces void chessMove() { printf("beg(%d %d) end(%d %d)\n", state.begr, state.begc, state.endr, state.endc); bool canMove = false; //Under what circumstances can a chess piece be moved if (!(state.begr == state.endr && state.begc == state.endc) && //Click is not the same piece state.endr!=-1 && state.begr!=-1&& //Subscript must be legal map[state.begr][state.begc].id != NONE//You can't move without a piece /*&&map[state.begr][state.begc].type != map[state.endr][state.endc].type*/) //You can't eat yourself { switch (map[state.begr][state.begc].id) { case Car: case Bamboo: if (state.begr == state.endr || state.begc == state.endc) { //Is there an obstacle between the start point and the end point if (hasBlock(&state)) { canMove = true; } } break; case Horses: case horse: break; case as: case mutually: break; case Scholar: case Official: break; case take: case Handsome: break; case Cannon: case cannon: break; case Pawn: case soldier: break; default: break; } if (canMove) { printf("canMove\n"); map[state.endr][state.endc].id = map[state.begr][state.begc].id; map[state.begr][state.begc].id = NONE; map[state.endr][state.endc].isRiver = map[state.begr][state.begc].isRiver; map[state.endr][state.endc].type = map[state.begr][state.begc].type; } } }

This is the end of the Chinese chess tutorial. Interested students can try to write it. In the future, I will release more project source codes and learning materials. I hope you can continue to pay attention. If you want C/C + + learning materials and complete source material graphics library development tools, you can learn from the Group [639681529], or you can ask questions in the comment area or in the group, I hope you can get the knowledge you want here, and if it helps you, you can pay more attention to praise comments, and put forward suggestions in the comment area. Thank you for your support!

Click the link below to join the group:

Enter the group to get the project source material and graphics library Oh ~ there are all kinds of learning materials waiting for you!http: / / click the link to join the group chat [C language / C + + Learning Q & a communication]: https://jq.qq.com/?_ wv=1027&k=9UocdboW

17 November 2021, 03:07 | Views: 5126

Add new comment

For adding a comment, please log in
or create account

0 comments