[C language] Realization of three chess (analysis)

Preface
As a small game that everyone is familiar with, backgammon will explain to you the detailed process of this small game today.
Backgammon is a two-player game
. We all know that the highest configuration of the game is friends, so we set it up here a little simpler, only needing the player to confront the computer.

1. Use multiple files to complete a program

For starters, you've probably never used multiple files to complete a program.
Today we use three files , two source files game.c and test.c and one header file game.h to complete this program.

game.h is a header file, you can write header files in library functions, your own function declarations and your own defined numbers in game.h , and when other files call game.h , you can use the contents. test.c is the main part of our program, put the main function in it to realize the main part of the program. Finally game.c is used to define the function.

2. General framework.

1. Select the interface.

insert image description here
print menu

void menu()
{
    
    
	printf("********************************\n");
	printf("**********   1.play   **********\n");
	printf("**********   0.exit   **********\n");
	printf("********************************\n");
}
做出选择
int input = 0;
	srand((unsigned int) time(NULL));
	do
	{
    
    
		printf("请选择:>");
		scanf("%d",&input);
		switch (input)
		{
    
    
			case 1:
				game();
			break;
			case 0:
				printf("退出游戏\n");
			break;
			default:
				printf("选择错误,请重新选择\n");
			break;
		}
	} while (input);

Each option will represent a different action. Here will briefly explain the following options.
**(1).**Select to enter the game**(2).**Exit the game**(3).**Select wrong, re-select
every time we finish After the game, you will choose whether to play again

2. Define the chessboard

2.1 The essence of three-bang is essentially a two-dimensional array.
2.2 Define the chessboard size and initialize it

定义棋盘大小
#define ROW 3
#define COL 3
初始化棋盘
void InitBoard(char Board[ROW][COL], int row, int col)
{
    
    
	int i = 0, j = 0;
	for (i = 0; i < row; i++)
	{
    
    
		for (j = 0; j < col; j++)
		{
    
    
			Board[i][j] = ' ';
		}
	}
}

2.2.1 Here we make such a definition for convenience when we need to change the size of the chessboard in the future.

insert image description here
2.2.2 How to print out such a chessboard
When we see such a chessboard, we will definitely think of using a loop to complete the chessboard, but how to select a small unit has become a problem. if in this wayinsert image description here

Can it be done? Of course, but when the chessboard needs to be expanded, each line will be limited to three spaces forever. Therefore, we should change the method and refine this small unit on the basis of the previous method. The small unit in the first line is divided into a space and a vertical bar , while the second line is divided into three small horizontal bars and a vertical bar . bar . In this way, no matter how the chessboard is expanded, it only needs to control the conditions of the loop to complete the corresponding chessboard.insert image description here

打印棋盘
void DisplayBoard(char Board[ROW][COL], int row, int col)
{
    
    
	int i = 0 , 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 < col - 1)
		{
    
    
			for (j = 0; j < col; j++)
			{
    
    
				printf("---");
				if (j < col - 1)
					printf("|");
			}
		printf("\n");

		}
	}
}

3. Players play chess

3.1 Players need to input coordinates every time they play chess, and the perspective of the chessboard is different from that of the programmer and the player. The programmer sees a board from 0 to 2, and the player sees a board from 1 to 3, so when writing code, you need to Pay attention to this.
3.2 After entering the coordinates, it is necessary to judge whether the coordinates entered by the player are in the range of 1 to 3, and it is also necessary to judge whether the entered coordinates have been downloaded.

玩家下棋
void Player(char Board[ROW][COL], int row, int col, char set)
{
    
    
	printf("玩家下棋\n");
	int x = 0, y = 0;
	while (1)
	{
    
    
		printf("请输入坐标:>");
		scanf("%d %d", &x, &y);
		x -= 1; y -= 1;
		//减一是因为人们认为的首坐标是1,而实际上是0
		if (x < row && x >= 0 && y >= 0 && y < col)  //判断是否在棋盘内部
		{
    
    
			if (Board[x][y] == ' ') //判断是否被占用
			{
    
    
				Board[x][y] = set;
				break;
			}
			else
			{
    
    
				printf("此坐标已被占用,请重新输入\n");
			}
		}
		else
			printf("输入坐标非法,请重新输入\n");
	}
}


4. Computer chess

4.1 When the computer is playing chess, use the rand() function to generate a random value so that the remainder of the row and column can be used to obtain two coordinates.
4.2 You need to use the srand () function before using the rand() function . The parameter of the srand() function needs a random number , and we all know that time changes. There is a time() function in the library function to complete the task. Here we set the parameter of the time() function to NULL , the return value of the time() function is time_t and as the parameter of the srand() function, the type must be converted to unsigned int type.

srand((unsigned int) time(NULL))
电脑下棋
//电脑下棋
void Computer(char Board[ROW][COL], int row, int col, char set)
{
    
    
	printf("电脑下棋\n");
	while (1)
	{
    
    
		//rand()%row就是一个0到row的数
		//这里不要忘记调用rand函数时,要先使用srand函数
		int x = rand() % row;
		int y = rand() % col;
		if (x < row && x >= 0 && y >= 0 && y < col)
		{
    
    
			if (Board[x][y] == ' ')
			{
    
    
				Board[x][y] = set;
				break;
			}
		}
	}
}

5. Display the chessboard

When the player or the computer finishes placing a position, it is necessary to print the chessboard once, so that the player can continue to play chess.

6. Determine whether to win or lose or continue

Here we use Q to represent the player and P to represent the computer.
Each game has the following situations, (1) win (2) lose (3) draw (4) no result yet, continue the game.
Whenever a player or computer finishes playing a game, it needs to judge whether the horizontal, column, and diagonal lines can win.
(1) Player wins and returns 'Q'
(2) Computer wins and returns 'P'
(3) Tie returns 'T'
(4) Continues and returns 'C'

判断输赢
char Win(char Board[ROW][COL], int row, int col)
{
    
    
	int i = 0, j = 0;
	//Board[i][j] != ' ',是防止误判影响结果
	//判断对角线是否能赢
	if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[1][1] != ' ')
	{
    
    
		return Board[1][1];
	}
	else if (Board[2][0] == Board[1][1] && Board[1][1] == Board[0][2] && Board[1][1] != ' ')
	{
    
    
		return Board[1][1];
	}

	//判断行是否能赢
	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 (j = 0; j < col; j++)
	{
    
    
		if (Board[0][j] == Board[1][j] && Board[1][j] == Board[2][j] && Board[1][j] != ' ')
		{
    
    
			return Board[1][j];
		}
	}

	//判断是否还有空格
	for (i = 0; i < row; i++)
	{
    
    
		for (j = 0; j < col; j++)
		{
    
    
			if (Board[i][j] == ' ')
				return 'C';
		}
	}

3. Completion of the overall program

test.c文件
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#pragma warning(disable : 6031)

#include "game.h"

void game()
{
    
    
	char tmp = 0;
	//定义一个二位数组来储存下的内容
	char Board[3][3] = {
    
     0 };
	//初始化棋盘
	InitBoard(Board, ROW , COL);
	DisplayBoard(Board, ROW, COL);
	while (1)
	{
    
    
		//玩家下棋
		Player(Board, ROW, COL, 'Q');
		//打印棋盘
		DisplayBoard(Board, ROW, COL);
		//判断胜负
		tmp = Win(Board, ROW, COL);
		if (tmp != 'C')
		{
    
    
			break;
		}
		//电脑下棋
		Computer(Board, ROW, COL, 'P');
		//打印棋盘
		DisplayBoard(Board, ROW, COL);
		//判断胜负
		int tmp = Win(Board, ROW, COL);
		if (tmp != 'C')
		{
    
    
			break;
		}
		//判断胜负
	}
	if (tmp == 'Q')
	{
    
    
		printf("玩家胜利\n");
	}
	else if (tmp == 'P')
	{
    
    
		printf("电脑胜利\n");
	}
	else 
	{
    
    
		printf("平局\n");
	}
}

void menu()
{
    
    
	printf("********************************\n");
	printf("**********   1.play   **********\n");
	printf("**********   0.exit   **********\n");
	printf("********************************\n");
}

int main()
{
    
    
	menu();
	int input = 0;
	srand((unsigned int) time(NULL));
	do
	{
    
    
		printf("请选择:>");
		scanf("%d",&input);
		switch (input)
		{
    
    
			case 1:
				game();
			break;
			case 0:
				printf("退出游戏\n");
			break;
			default:
				printf("选择错误,请重新选择\n");
			break;
		}
	} while (input);
	return 0;
}
game.h文件
#define ROW 3
#define COL 3

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

//初始化棋盘
void InitBoard(char Board[ROW][COL],int row ,int col);
//打印棋盘
void DisplayBoard(char Board[ROW][COL], int row, int col);
//玩家下棋
void Player(char Board[ROW][COL], int row, int col ,char set);
//电脑下棋
void Computer(char Board[ROW][COL], int row, int col, char set);
//判断胜负
char Win(char Board[ROW][COL], int row, int col);

 game.c文件
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#pragma warning(disable : 6031)

#include "game.h"

//初始化棋盘
void InitBoard(char Board[ROW][COL], int row, int col)
{
    
    
	int i = 0, j = 0;
	for (i = 0; i < row; i++)
	{
    
    
		for (j = 0; j < col; j++)
		{
    
    
			Board[i][j] = ' ';
		}
	}
}

//打印棋盘
void DisplayBoard(char Board[ROW][COL], int row, int col)
{
    
    
	int i = 0 , 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 < col - 1)
		{
    
    
			for (j = 0; j < col; j++)
			{
    
    
				printf("---");
				if (j < col - 1)
					printf("|");
			}
		printf("\n");

		}
	}
}

//玩家下棋
void Player(char Board[ROW][COL], int row, int col, char set)
{
    
    
	printf("玩家下棋\n");
	int x = 0, y = 0;
	while (1)
	{
    
    
		printf("请输入坐标:>");
		scanf("%d %d", &x, &y);
		x -= 1; y -= 1;
		//减一是因为人们认为的首坐标是1,而实际上是0
		if (x < row && x >= 0 && y >= 0 && y < col)  //判断是否在棋盘内部
		{
    
    
			if (Board[x][y] == ' ') //判断是否被占用
			{
    
    
				Board[x][y] = set;
				break;
			}
			else
			{
    
    
				printf("此坐标已被占用,请重新输入\n");
			}
		}
		else
			printf("输入坐标非法,请重新输入\n");
	}
}

//电脑下棋
void Computer(char Board[ROW][COL], int row, int col, char set)
{
    
    
	printf("电脑下棋\n");
	while (1)
	{
    
    
		//rand()%row就是一个0到row的数
		//这里不要忘记调用rand函数时,要先使用srand函数
		int x = rand() % row;
		int y = rand() % col;
		if (x < row && x >= 0 && y >= 0 && y < col)
		{
    
    
			if (Board[x][y] == ' ')
			{
    
    
				Board[x][y] = set;
				break;
			}
		}
	}
}

//判断胜负
char Win(char Board[ROW][COL], int row, int col)
{
    
    
	int i = 0, j = 0;
	//Board[i][j] != ' ',是防止误判影响结果
	//判断斜着是否能赢
	if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[1][1] != ' ')
	{
    
    
		return Board[1][1];
	}
	else if (Board[2][0] == Board[1][1] && Board[1][1] == Board[0][2] && Board[1][1] != ' ')
	{
    
    
		return Board[1][1];
	}

	//判断行是否能赢
	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 (j = 0; j < col; j++)
	{
    
    
		if (Board[0][j] == Board[1][j] && Board[1][j] == Board[2][j] && Board[1][j] != ' ')
		{
    
    
			return Board[1][j];
		}
	}

	//判断是否还有空格
	for (i = 0; i < row; i++)
	{
    
    
		for (j = 0; j < col; j++)
		{
    
    
			if (Board[i][j] == ' ')
				return 'C';
		}
	}
	//如果没有空格,并且没有一方赢,那么就是平局
	return 'T';
	// 1.玩家赢  返回‘Q’
	// 2.电脑赢  返回‘P'
	// 3.平局    返回‘T’  
	// 4.继续    返回‘C’  
}

Summarize

As a beginner in C language, it is a great improvement to be able to complete such a program alone.
If you have any suggestions and questions, or if there are any mistakes, I hope everyone can mention them.
I hope everyone can make progress with me in the future! !

Guess you like

Origin blog.csdn.net/qq_55401402/article/details/129347243