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:
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:
DisMenu function ():
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.
Then define a char type array Board[ROW][COL] in the game() function to save the information of each grid of the chessboard.
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).
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.
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.
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).
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.
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.
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
Ten: complete code
game.h
game.c
text.c
Actual renderings
Welcome to point out the shortcomings, end!