How to use c language to implement three-brows

foreword

This article mainly uses the knowledge of two-dimensional arrays in C language to realize the game of backgammon step by step, so as to consolidate the understanding of knowledge and improve the ability of coding.

One: the rules of the game of backgammon

After starting the game, the player will see a 3✖️3 grid. After the player enters the coordinates, the computer will automatically play chess. If three identical chess pieces form rows, columns, or opposite corners, one side wins. If the board is full and there is no winner, The game is over and the player now chooses whether to replay.

 

Two: Create source files and header files

Header file: game.h
Source file: text.c game.c
text.c contains the main function, which is used to judge whether the game logic is correct.
In order to make the code more concise and easier to maintain, we encapsulate the implementation of each function into a function, and game.h is used to define these functions.
The header file game.h is used to include the header files that need to be used, and the macro defines some symbolic constants and declares the functions in game.c.

Header files included in game.h:

043e3ecdb8304fc48b5ff27a8324198c.png

 

 Three: Code basic logic and menu printing.

Basic logic: In order to allow players to choose whether to restart after playing a game, we need to design a loop in text.c and design a game() function. We write the logic of the game into the game function() and run A game() function represents a game. Menu: Before playing the game, we need to know whether the player wants to play. We declare a function DisMenu()
in game.h and define the function in game.c ( the same is true for subsequent function declarations and definitions ), through the scanf() function Get the information input by the player, if it is 1, the game will be played, if it is 0, it will jump out of the loop, and when the program ends, other error messages will be printed out, and a new loop will be performed.

main function:

 

4f7213bb4a8f4666a08e0bc43902b81f.png

 DisMenu function ():

67effee313994aceb90ef9092bb22219.png

 

Four: Initialize the chessboard.

We first macro define the specifications of the chessboard in game.h, ROW represents the function, and COL represents the number of columns.

303afa6587794b239e4e4e3a21bd36c5.png

Then define a char type array Board[ROW][COL] in the game() function to save the information of each grid of the chessboard.

16497a9fa1f143d5be4c486323b9ee19.png

Declare and define the function initBoard(). The parameters passed in by the function are the array and the number of rows and columns of the board. At this time, the Board array is not initialized. In order to facilitate the subsequent judgment of the grid state, we uniformly initialize it to ' ' (that is, a space).

2886353a41c345b3a9d659ec9b9d3c80.png

 

Five: Print the chessboard.

Declare and define a function DisPlayBoard() whose parameters are the Board array and the number of rows and columns of the board. Call the DisPlayBoard() function in the main function. Note that when the checkerboard is printed to the end of each line, a line break must be performed, and the printing of each line of elements must be separated by underscores.

8c9a8d43d94143daa79d1762c7a57ab2.png

2975c6fc01274767aa17b94aa1a2c81b.png

 

 

Six: Players play chess

Declare and define a function playerMove() whose parameters are the Board array and the number of rows and columns of the board.
Playing chess is actually changing the elements in the corresponding array through the coordinates input by the player. We stipulate that the player's chess is *.

Note: The coordinates entered by the player should start from 1 for the row and column labels. When using an array to modify, the subscript should be changed to x-1 and y-1 to avoid out-of-bounds access to the array.

67ab21f1e4e74771884b08210c3b03b0.png

 

Seven: Computer chess. 

Declare and define a function computerMove (), the function parameters are the Board array and the number of rows and columns of the chessboard.
We choose to let the computer play chess randomly (I don’t know how to design algorithms, you can try it yourself), call the srand() function to get the current time as the seed generated by the random number, the rand() function randomly generates non-negative integers, use rand () Take the remainder of any integer x to get an integer in the range of [0,x).

b088f56400c84a9585b5f4b00789206d.png

 

 Eight: Design in the game() function

We want the execution of the game() function to represent the end of a game. If the game is not over after the two parties have finished playing the game, we will continue to play chess. We design the game between the player and the computer as a loop. If the game Jump out of the loop once at the end.

439da4cffdc0452ebf1b28fc9cbca0e6.png

 

Nine: Determine whether the game is over.

We declare and define a function IsWin(), the function parameters are the Board array and the number of rows and columns of the board, and return the characters that can judge the game state.
Draw: If none of the above conditions is true, we consider whether it is a draw. If the board is full, we return Q. We declare and define the function IsFull(). The function parameters are the Board array and the number of rows and columns of the board. The return value of the function is int type. Returns 1, otherwise returns 0.
Every time the player's computer finishes playing chess, we will make a judgment. If the return value is not C, we will print the result separately and jump out of the loop in the game() function, otherwise the player will continue to play chess.

b3721cece57e4dac9e61493eb63a8b45.png

bec64989c72d465a93c98bad3cb284fe.png

We put the function IsWin() that judges the game state into the loop of the game function, and judge whether the game continues based on the return value

53a88e5bca4143a69e531eafc14097ae.png

 

Ten: complete code

 game.h

de7b0631d8cf4bc58ecb3edb0a31dfb4.png

game.c

d6f4f7d6f53d4b1d8b9b8b10bf5583cd.png

text.c

94b39a56a98645be9bcd567e92abceb4.png

Actual renderings 

f16bafd475e649adbc42545362345f48.png3c5a9d017c0b425e8ebc9d56ffcc3062.png

 

 

 

 Welcome to point out the shortcomings, end!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/2301_76269963/article/details/129322381