As I was preparing for the postgraduate examination, I did not have time to record the code learning one by one. I still tried to write down some key points. Although the article was written, it was just a record similar to the outline.

One can either live through one’s discomfort and insecurities or one can live through one’s dissatisfaction with excuses to avoid challenges. I choose the former. Although college wasted four years, still come back early, with a regretful mood to start over!

Simulation design of a chess game (the opponent is a random man – machine)

steps

1. Create three files

Test.c Tests the implementation of game features

Game.c is used to write related functions

Game.h is used to declare related functions

2. Print the directory (initial selection screen)

Use the do while loop so that there is at least one selection

Select 1 to call the game function to start the game, select 0 to quit (switch implementation, input is the selection value)

The while condition is the selected input value, such a 0/1 selection is consistent with the machine’s internal execution of the false/true selection

3. Implement game function

3.1 Game platform construction

Two-dimensional array initialization (initboard)

Print a checkerboard into a grid, ie divide it into nine squares with vertical and horizontal lines

3.2 Realize the function of playing chess

3.3 Computer chess function

3.4 Judge the winner

game.h #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 3 #define Void InitBoard(char board[ROW][COL], int ROW, int COL); Void DisplayBoard(char board[ROW][COL], int ROW, int COL) Void PlayerMove(char board[ROW][COL], int ROW, int COL); Void ComputerMove(char board[ROW][COL], int ROW, int COL); Char IsWin(char board[ROW][COL], int ROW, int COL); Char IsFull(char board[ROW][COL], int ROW, int COL) char IsFull(char board[ROW][COL], int ROW, int COL);Copy the code
game.c #include "game.h" void InitBoard(char board[ROW][COL],int row,int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] = ' '; } } } void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col-1) { printf("|"); } } printf("\n"); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); }}} void PlayerMove(char board[ROW][COL], int ROW, int COL) {printf(" player's turn :>\n"); int x = 0; int y = 0; While (1) {printf(" Please enter the coordinates to be placed; > "); scanf("%d %d", &x, &y); / / judgment legitimacy if coordinates (x > = 1 && x < = row && y > = 1 && y < = {/ / col) whether coordinates occupied the if (board [1] x [1] y = = ' ') {board [1] x [y - 1] = '*'; break; } else {printf(" coordinates occupied, please retype \n"); }} else {printf(" invalid coordinates, please retype \n"); }} void ComputerMove(char board[ROW][COL], int ROW, int COL) {printf(" computerturn \n"); while (1) { int x = rand() % row + 1; int y = rand() % col + 1; / / determine whether coordinates occupied the if (board [x] [y] = = ' ') {board [x] [y] = '#'; break; } } } char IsFull(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0; } } } return 1; } char IsWin(char board[ROW][COL], int row, int col) { int i = 0; For (I = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] ! = ' ') { return board[i][1]; For (I = 0; i < row; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] ! = ' ') { return board[1][i]; For (I = 0; i < row; i++) { if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] ! = ' ') { return board[1][1]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] ! = ' ') { return board[1][1]; }} // if the board is full and there is no winner or loser, it is a draw. -0 int ret = IsFull(board, row, col); if (ret == 1) { return 'Q'; } // return 'C' if none of the above is true; }Copy the code
test.c #include "game.h" void menu() { printf("*******************\n"); printf("*****1. play*******\n"); printf("*****0. exit*******\n"); printf("*******************\n"); Char board[ROW][COL]; char board[COL]; // Initialize the checkerboard with space InitBoard(board, ROW, COL); DisplayBoard(board, ROW, COL); Char ret = 0; While (1) {// PlayerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); Ret = IsWin(board, ROW, COL); if (ret ! = 'C') { break; } // ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); Ret = IsWin(board, ROW, COL); if (ret ! = 'C') { break; }} if (ret == '*') {printf(" player wins \n"); } else if (ret == '#') {printf(" computer wins \n"); } else {printf(" tie \n"); } // Display the game DisplayBoard(board, ROW, COL); } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); Printf (" Please select :>\n"); scanf("%d", &input); Switch (input) {case 0: printf(" exiting the game \n"); break; case 1: game(); break; Default: printf(" Invalid input value, please retype \n"); break; } } while (input); return 0; }Copy the code

Welcome correction!

Photo source: www.lyouxi.com/ mobile games