Use C language to play a small game (1)-three chess

Although the C language is the most basic language, it has extremely powerful functions. You can write a lot of interesting things in the C language. Today I will introduce you a three-middle chess game. This game has not added complex algorithms to improve the computer. Only the most basic things are realized. The whole is done with arrays, but I will continue to improve later.

1. Setting menu and option interface

Because it is the early stage of the game, the menu settings are relatively simple, just 1 and 0, choose 1 to play the game, choose 0 to exit the game

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

	//用户输入并执行
	

}

void test()
{
    
    
	int x = 0;

	printf("请输入想要进行的操作:");
	do{
    
    
		menu();
		scanf("%d", &x);
		printf("\n");
		switch (x)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("请重新输入\n");
			break;
		}
	} while (x);
}

The execution of the option is completed in the switch statement. Case1 will execute the game playing function, case0 will exit, and other input will be re-entered. The reason for using the do-while loop is that the game must be played at least once, which is suitable for the do-while loop In the use scene, the judgment condition will not jump out of the loop except for 0, so input as the judgment loop condition. As long as you do not enter 0, the game will continue.

2. Game interface

(1) Set the chessboard

Insert picture description here
Set the chessboard to the interface as shown in the figure, and it can be completed with a for loop.

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

In order to keep the checkerboard interface as an empty interface at the beginning, the location where the array is stored is initialized to a space first, so that the objectivity of the interface can be guaranteed without affecting subsequent changes.

(2) Players play chess

At this basic stage, the player chooses chess by entering the coordinates of the chessboard. Therefore, pay attention to the following issues when writing code:
1. If the player's input coordinates are beyond the position of the board, you should be prompted and reselect.
2. If the coordinates entered by the player have already played chess pieces, it is impossible to overwrite them with new values, so the player should be reminded and selected again.
3. Because players are not all programmers, not everyone knows that the start of the array starts from 0, so the player must enter a normal value, and we need to reduce the input coordinates by one when writing the program. ,

According to the above three issues that need to be paid attention to, the entire process of player inputting coordinates needs to be completed in a loop. If the input is not the correct value, re-enter it, and if the input is correct, use break to jump out. The code is implemented as follows:

void PlayerMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	while (1)
	{
    
    
		printf("玩家下棋->\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
		{
    
    
			if (board[x - 1][y - 1] == ' ')
			{
    
    
				board[x - 1][y - 1] = '*';
				break;

			}
			else
			{
    
    
				printf("坐标被占用,请重新输入\n");

			}
		}
		else
		{
    
    
			printf("输入超出棋盘,请重新输入\n");
		}
	}
}

(3) Computer playing chess

The way the computer plays chess can be changeable, and the different algorithms can make the way the computer plays chess continuously change, which can make the computer's intelligence continue to improve. But at the initial stage, I don't know enough about some algorithms, so I use the most basic random numbers to let the computer play chess, and later I will look up more information to make the whole structure more perfect.

Since the computer moves are prescribed by random numbers, there is no need to consider the situation where the coordinates are out of bounds. You only need to consider whether the coordinates are occupied or not. The same cycle method is used to achieve every computer input, and the input is correct. break out.

The code is implemented as follows:

void ComputerMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	printf("电脑下棋->\n");
	while (1)
	{
    
    
		
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
    
    
			board[x][y] = '#';
			break;
		}
		
	}
}

(3) Judging the result of the game

Since it is a chess game, there must be a result, whether it is a loss, a win, or a tie. Therefore, after each input from the player and the computer, we have to make a judgment on the entire board. If one party wins or draws , It ends the game and starts the next selection. The judgment conditions I use are to return "*", return "#", return Q and return C to represent different meanings. Returning "*" means the player wins. Return "#" means the computer wins, return Q means a tie, return C means the game continues.

Code:

char IsWin(char board[ROW][COL], int row, int col)
{
    
    
	int q = 0;
	int i = 0;
	int j = 0;
	//首先判断行是否成一行
	for (i = 0; i < row; i++)
	{
    
    
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
		{
    
    
			return board[i][0];
		}
	}

	for (i = 0; i < col; i++)
	{
    
    
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
    
    
			return board[1][i];
		}
	}

	if (board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[1][1] != ' ')
		return board[1][1];
	if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
		return board[1][1];

	q = IsFull(board, ROW, COL);
	if (q == 1)
		return 'Q';

	return 'C';

	
	
}

Judging who wins is based on whether there is a row or a column or two diagonals connected. Because it is an elementary algorithm, the judgment of winning or losing is only judged by if, so it can only be limited to three children. chess. There is a clever place here, that is to return "*" and "#" which is directly used to connect to a line and return any coordinate element on that line, so that you do not need to judge who won , And then return to the corresponding logo.

The judgment of a tie is to traverse the entire board. If no one wins and the board is full, it is a tie.


int IsFull(char board[ROW][COL], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
    
    
		for (j = 0; j < col; j++)
		{
    
    
			if (board[i][j] == ' ')
				return 0;
			
		}
		
	}
	return 1;
}

Code effect

The player wins the
Insert picture description here
computer wins. The overall code implementation of the
Insert picture description here
tie
Insert picture description here
is still very successful, but there are still many areas that can be improved. For example, some algorithms can be added to the computer so that the computer can choose the best position according to the situation of the board. For example, the input is now The input based on the coordinates can be improved to use the mouse to click to play chess, etc. These all require more thinking.

I will look up information later to optimize the overall situation. I hope everyone can give me more encouragement and support.

Guess you like

Origin blog.csdn.net/qq_41490958/article/details/113096333