I. Problem description

C language to achieve chess.

Two, the basic process

1. Print the menu to start or quit the game

2. Create a two-dimensional array as the checkerboard and initialize it

3. Print the checkerboard

4. Player puts “X” to represent player

5. Test results

Computer “O” stands for computer

7. Test results

8. Judge the results

9. Go to Step 2

Three, steps,

1. Menu interface

1. Start the game. 2

void Menu() { printf("+--------------------+\n"); printf("| 1.Play 2.Exit |\n"); printf("+--------------------+\n"); } int main() { int select = 0; int quit = 0; while (! quit){ Menu(); printf("Please Select#"); scanf("%d",&select); Switch (select){case 1: // select 1 to enter Game(); break; Case 2: // Quit = 1; break; Printf ("You Enter Error,Try Again! \n"); break; } } printf("ByeBye! \n"); system("pause"); return 0; }Copy the code

Results:

2. Create a board

Use a two-dimensional array representation of three rows and three columns, where macro definitions are used to improve the readability and maintainability of the code.

#define ROW 3 #define COL 3 #define WHITE 'X' // player #define BLACK 'O' // computer #define INIT ' '#define DRAW 'D' // DRAW #define NEXT 'N' // ContinueCopy the code

3. Initialize the board

INIT means space

#define INIT ' ' static void InitBoard(char board[][COL], int row, int col){ for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ board[i][j] = INIT; }}}Copy the code

4. Print the checkerboard

static void ShowBoard(char board[][COL], int row, int col){ system("cls"); for (int i = 0; i < col; i++){ printf(" %4d", i+1); } printf("\n +---+----+---+\n"); for (int i = 0; i < row; i++){ printf("%-2d", i+1); for (int j = 0; j <col; j++){ printf("| %c", board[i][j]); } printf("\n +---+----+---+\n"); }}Copy the code

Results:

5. Player drops

The player input row coordinates to indicate the drop, and “X” to indicate the player.

Note:

1. Players need to place pieces within the board;

2. Players have to choose the position above the board to play.

3. If the entered coordinate is incorrect, enter it again.

static void PlayerMove(char board[][COL], int row, int col){ int x = 0; int y = 0; while (1) { printf("Please Enter Position<x,y>#"); scanf("%d %d", &x, &y); If (x < 1 | | < 1 y | | x > 3 | | y > 3) {/ / required whether players Enter the coordinates of the printf (" Enter the Position Error! \n"); continue; } if (board[x-1][y-1] == INIT){board[x-1][y-1] = WHITE; break; } else{ printf("Position Is Not Empty! \n"); }}}Copy the code

6. The computer drops

Seed a random number, let the computer randomly generate row and row coordinates, “O” indicates the computer placed.

Note:

1. Use srand((unsigned long)time(NULL)) as the seed of random number;

2. The computer should place squares on the board. (Go here, it means there must be a space, because the result is determined after the player hits the ball, there is no need to determine whether there is a space);

//srand((unsigned long)time(NULL)); Static void ComputerMove(char board[][COL], int row, int COL){while (1){int x = rand() % row; Int y = rand() % col; If (board[x][y] == INIT){board[x][y] = BLACK; break; }}}Copy the code

7. Test results

1. Return result:

(1). “X” means the player wins;

(2). “O” means computer win;

(3). “D” means draw;

(4). “N” means to continue playing chess before a game is finished.

2. Call a draw or continue

If there are Spaces in the array elements, the board is not full, continue; If the board is full and there is no winner, a draw.

3. Decide whether the computer or the player wins

(1). Judge all rows

(2). Judge all columns

(3) judge the two diagonals

static char IsEnd(char board[][COL], int row, int col){ for (int i = 0; i < row; i++){ if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] ! = INIT){ return board[i][0]; }} for (int j = 0; j < col; j++){ if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] ! = INIT) { return board[0][j]; / / judge all columns}} the if (board [0] [0] = = board [1] [1] && board [1] [1] = = board [2] [2] && board [0] [0]! = INIT){ return board[0][0]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] ! = INIT){ return board[0][2]; } for (int I = 0; i < row; i++){ for (int j = 0; j < col; j++){ if (board[i][j] == INIT){ return NEXT; } } } return DRAW; }Copy the code

– the player:

Computer games:

A draw:

Note: judge the result every time the player takes a shot, and judge the result every time the computer takes a shot

Four, code implementation

Game logic:

void Game() { char board[ROW][COL]; // create board InitBoard(board, ROW, COL); // Initialize the checkerboard srand((unsigned long)time(NULL)); // type random number seed char result = 0; while (1){ ShowBoard(board, ROW, COL); PlayerMove(board, ROW, COL); // result = IsEnd(board, ROW, COL); If (result! = NEXT){ break; } ShowBoard(board, ROW, COL); ComputerMove(board, ROW, COL); Result = IsEnd(board, ROW, COL); If (result! = NEXT){ break; } } ShowBoard(board, ROW, COL); Switch (result){case WHITE: printf("You Win! \n"); break; case BLACK: printf("You Lose! \n"); break; case DRAW: printf("You==Computer! \n"); break; default: printf("BUG! \n"); break; }}Copy the code

That says here, a three son chess network game is over cough up!