Simple backgammon game – [C language]

Hello everyone, I am Ale. It’s been a long time since I updated, I’ve been too busy lately haha. Without further ado, let’s go directly to the topic, today we use C language to implement a simple version of the game of backgammon!

Directory

1. Logical thinking

2. Logical relationship of text.c file

2.1 Initial game interface and establish a logical framework

2.2 game() function logic part

3. Detailed analysis of game.c

3.1 Initialize the chessboard and print the chessboard

3.2 Players playing chess and computer playing chess

3.3 Judging winning or losing

4. game.h file

5. Summary


1. Logical thinking

This game uses three files, namely two source files text.c, game.c and header file game.h. The reason for doing this is nothing more than to practice the functions of each part in modules, and at the same time, it can make our code look clearer and more logical, which is convenient for us to check for errors.

The text.c file is the logical framework of the entire program, which contains the main function. In the design of this file, as long as the logical thinking is clear, it is ok to go step by step. The game.c file is mainly used to realize the game functions of the game of backgammon, and the game.h file is used to declare various required header files and functions.

2. Logical relationship of text.c file

Let’s think about it first. To realize this game of backgammon, we must first have a game start interface. Players must choose whether to start the game or quit the game. Let’s talk about this first, let’s go down step by step .

2.1 Initial game interface and establish a logical framework

int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do//Every time before entering the game, the menu will be printed first, and the player will be allowed to choose after each game.
{
menu(input);//game menu
printf("Please choose:");
scanf("%d", &input);
switch (input)
{
case 1:
{
game();//Enter the game
break;
}
case 0:
{
printf("Exit the game");
break;
}
default:
{
printf("Choose wrong, please choose again");
}
}
} while (input);//As long as the input value is not 0, the condition is always true
return 0;
}

This is the idea of establishing an overall framework. Because the menu needs to be printed first every time, the do while loop is used, and then the switch statement is used to let the player choose to enter the game or exit the game. Enter the game and enter the game() function, exit the game and jump out of the loop directly, and the program ends. The following parts are implemented.

game menu

void menu(int input)
{
printf("**************************\\
");
printf("*******1. Start the game********\\
");
printf("*******0. Quit the game********\\
");
printf("**************************\\
");
}

Players choose 1/0 to start the game or exit the game

switch (input)
{
case 1:
{
game();//Enter the game
break;
}
case 0:
{
printf("Exit the game");
break;
}
default:
{
printf("Choose wrong, please choose again");
}
}

2.2 game() function logic part

After entering the game, there must be a chess board, and then the player plays chess and the computer plays chess, and finally if one side wins, it is necessary to judge the winner or loser. On the whole, it is such a process. So the basic logic is:

1. Initialize the chessboard 2. Print the chessboard 3. The player plays chess 4. The computer plays chess 5. Judge win or lose

After each player or the computer plays chess, it is necessary to judge whether to win or lose, so that the return value can be used to judge. What is certain is that the player and the computer play chess back and forth, so a loop is used here.

The overall code first, and the partial explanation.

void game()
{
char board[ROW][COL] = { 0 };//board is a 3*3 two-dimensional array of character type, ROW and COL are declared as constants in game.h, this is for convenience.
//Initialize the chessboard
InitBoard(board, ROW, COL);
// print the chessboard
DisplayBoard(board, ROW, COL);
char ret = 0;//Using ret to receive the return value can judge winning or losing
while (1)
{
//The player plays chess
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
printf("\\
");
// judge win or lose
ret = IsWin(board, ROW, COL);
if (ret != 'C')
break;
//Computer playing chess
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
printf("\\
");
if (ret != 'C')
break;

}
if (ret == '*')
{
printf("Congratulations to the player for winning!\\
");
}
else if(ret == '#')
{
printf("Computer victory!\\
");
}
else
{
printf("Tie!\\
");
}
}

Detailed analysis of 3.game.c

The code in game.c is shown below to further analyze and explain game()

3.1 Initialize the chessboard and print the chessboard

//Initialize the board as a space, each element is initialized as ' '(space)
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] = ' ';
}
}
}
// print the chessboard
void DisplayBoard(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 ++ )
{
printf(" %c ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\\
");
if (i < row - 1)
{
//printf("---|---|---\\
");
int j = 0;
for (j = 0; j < col; j ++ )
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\\
");
}
}
}

The effect is as follows:

3.2 Players playing chess and computer playing chess

void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("Please play chess\\
");
while (1)//The judgment condition is 1 because if the coordinate input is wrong, it must be re-entered
{
printf("Please enter the coordinates of the chess pieces to be played, separated by spaces:");
scanf("%d %d", &x, &y);
if (x >= 1 & amp; & amp; x <= row & amp; & amp; y >= 1 & amp; & amp; y <= col)//x, y must be within 1-3
{
if (board[x - 1][y - 1] == ' ')//For the array, the range is within 0-2
{
board[x - 1][y - 1] = '*';//The chess piece filled by the player is set to '*'
break;
}
else
{
printf("The entered coordinates are occupied, please re-enter\\
");
}
}
else
{
printf("The coordinate is illegal, please re-enter\\
");
}
}
}

void ComputerMove(char board[ROW][COL],int row,int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % ROW;//Give x and y a random number of 0-2
y = rand() % COL;
if (board[x][y] == ' ')
{
board[x][y] = '#';//The pieces filled by the computer are set to'#'
break;
}
}
}

3.3 Judging Winning

char IsWin(char board[ROW][COL], int row, int col)
{
//If the three characters in each line are the same, it will win, and the three characters cannot be spaces ' '
int i = 0;
for (i = 0; i < row; i ++ )
{
if (board[i][0] == board[i][1] & amp; & amp; board[i][1] == board[i][2] & amp; & amp; board[i] [0] != ' ')
{
return board[i][0];
}
}
//If the three characters in each column are the same, win
for (i = 0; i < col; i ++ )
{
if (board[0][i] == board[1][i] & amp; & amp; board[1][i] == board[2][i] & amp; & amp; board[0] [i] != ' ')
{
return board[0][i];
}
}
// If the three characters on the diagonal line are the same, it wins
if (board[0][0] == board[1][1] & amp; & amp; board[1][1] == board[2][2] & amp; & amp; board[1] [1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] & amp; & amp; board[1][1] == board[2][0] & amp; & amp; board[1] [1] != ' ')
return board[1][1];

// tie
if (IsFull(board, row, col) == 1)
{
return 'Q';
}
\t//continue
return 'C';
}

There is also a function IsFull() mentioned here, which is a function used to judge whether the board is full. If it is full, this function returns 1, so it can be used to judge whether there is a tie, because at this time the board is full and no one wins. The following is the implementation of the IsFull function

//Traverse every character on the chessboard to judge whether there is ' '.
//If there is, it means the board is not full and returns 0.
//No means the board is full and returns 1.
int IsFull(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 ++ )
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}

4. game.h file

This document should have been placed at the beginning, but for the convenience of readers, I put it at the end

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 3//declare ROW is a constant
#define COL 3//declare COL is a constant

//Initialize the chessboard
void InitBoard(char board[ROW][COL], int row, int col);
// print the chessboard
void DisplayBoard(char board[ROW][COL], int row, int col);
//The player plays chess
void PlayerMove(char board[ROW][COL], int row, int col);
//Computer playing chess
void ComputerMove(char board[ROW][COL], int row, int col);


// judge win or lose
//The return value is '*', the player wins
//The return value is '#', the computer wins
//The return value is 'Q', a draw
//The return value is 'C', continue the game
char IsWin(char board[ROW][COL], int row, int col);

5. Summary

The above is the entire process and code of the implementation of the game of backgammon. The code is implemented in the VS2019 environment. I hope that all readers can gain something from reading it, and at the same time, I also hope that you can give some comments and suggestions after reading it. I will definitely work hard to correct and continue to work hard!