Simple implementation of Sanzi chess

Keywords: C Visual Studio

First release the whole game source code, followed by the corresponding description, and the output result is at the end

1, Project

1.game.h about game related function declarations, symbol declarations and header files

#Include < stdio. H > / / include header file
#Include < stdlib. H > / / include of rand function header file
#include<time.h>
#define ROW 3
#define COL 3

//Initializes the declaration of the checkerboard function
void initboard(char board[ROW][COL], int row, int col);//Receive incoming rows and columns


// Print the declaration of the checkerboard function
void displayboard(char board[ROW][COL], int row, int col);


//Declaration of player's chess function
void playermove(char board[ROW][COL], int row, int col);


//Declaration of computer chess function
void computermove(char board[ROW][COL], int row, int col);


//Declaration of win / lose function
char iswin(char board[ROW][COL], int row, int col);
//Player wins return*
//Computer victory return#
//Draw return Q
//The game goes on as usual return C

  2.test.c test the logic of the game

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

void game()
{
	char board[ROW][COL];//Define array
	initboard(board, ROW, COL);//Call the initialize array function
	displayboard(board, ROW, COL);//Print chessboard (call the function to print chessboard) - essentially print array
	//So far, the printing of the chessboard is over. Next, players and computers play chess
	char ret = 0;//Accept game status
	while (1)
	{
		playermove(board, ROW, COL);//Call the player's function to play chess
		displayboard(board, ROW, COL);//Display the chess pieces on the chessboard after the player finishes playing chess
		ret = iswin(board, ROW, COL);//The return value of the judging function is assigned to the variable ret
		if (ret != 'C')
			break;
		computermove(board, ROW, COL);//Call the computer chess function
		displayboard(board, ROW, COL);//Call the computer to finish the chessboard
		ret = iswin(board, ROW, COL);//The return value of the judging function is assigned to the variable ret
		if (ret != 'C')
			break;
	}
	if (ret == '*')
	{
		printf("Player victory\n");
	}
	else if (ret == '#')
	{
		printf("Computer victory\n");
	}
	else
	{
		printf("it ends in a draw\n");
	}
	displayboard(board, ROW, COL);//Call the chessboard after the game
}
int main()
{
	srand((unsigned int)time(NULL));//The rand function calls srand when generating coordinates. In order to put it into the main function conveniently, the return value of the time function is used as the generator of random numbers
	int input = 0;
	do
	{
		menu();
		printf("Please select->");
		scanf_s("%d", &input);
		switch (input)
		{
			case 1:
				game();
				break;
			case 0:
				printf("Exit the game\n");
				break;
			default:
				printf("Error, please re-enter\n");
				break;
		}
	} while (input);

	return 0;

}

3. Implementation of game. C game related functions

#include"game.h"


//Function definition for initializing checkerboard
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] = ' ';
		}
	}
}
//Function definition of 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("\n");
	
		if (i < row - 1)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
	
}
//Function definition of player movement
void playermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("Player turn->\n");
	while (1)
	{
		printf("Please enter the chess position\n");
		scanf_s("%d %d", &x, &y);
		//Judge whether the landing point is legal
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//After input, judge whether to play chess at the same landing point repeatedly
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("The landing point is repeated, please re-enter\n");

		}
		else
			printf("Illegal input, please re-enter\n");
	}
	
}
//Function definition of computer mobile
void computermove(char board[ROW][COL], int row, int col)
{
	printf("Computer drop->\n");
	//For a computer, its behavior can be defined as random input, so we use functions to generate random numbers
//The column value entered by the computer is between 0 and 2, and the column value is between 0 and 2. We need to limit it
//The random number must be within 3 when it is summed to 3
	while (1)
	{
		int x = rand() % row;
		int y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}

	}

}
//Function to judge whether the chessboard is full
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[row][col] == ' ');
			return 0;
		}
	}
	return 1;
}
char iswin(char board[ROW][COL], int row, int col)
{
	//Judge whether the three rows and three columns and the main and auxiliary diagonals are equal. If they are equal, return
	//At this time, we return the player's victory to * and the computer's victory to #, which corresponds to the elements placed in the chessboard by the previous player and the computer, avoiding the trouble of creating new variables
	int i = 0;//Judge three lines
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1]&& board[i][1] == board[i][2] && board[i][0] != ' ')//Finally, determine whether it is a space. Any array element can be used because the three are the same
		{
			return board[i][0];
		}
	}
	//Judge three columns
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i]&& board[1][i] == board[2][i] && board[0][i] != ' ')//Finally, determine whether it is a space. Any array element can be used because the three are the same
		{
			return board[0][i];
		}
	}
	//Judge diagonal
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')//Main diagonal
	{
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')//Sub diagonal
	{
		return board[1][1];
	}
	//Judge the draw and call a function at this time
	int t = isfull(board, row, col);
	{
		if (t == 1)
		{
			return 'Q';
		}
		//continue
		return 'C';
	}

}

2, Specific steps

First, compile in test.c, use the do while statement, first output the game menu and initial options, and then enter numbers to judge whether the game starts. Relevant logic: input in the brackets of while is the input number. Use the switch statement, if the input is 0, output "exit the game" and jump out of the loop. If the input is 1, start the game, if it is other numbers, Then enter the cycle again for input after outputting "error"

int main()
{
	int input = 0;
	do
	{
		menu();
		printf("Please select->");
		scanf_s("%d", &input);
		switch (input)
		{
			case 1:
				//Start the game
				game()
				break;
			case 0 :
				printf("Exit the game\n");
				break;
			default:
				printf("Error, please re-enter");
				break;
		}
	} while (input);

	return 0;

Game menu: the return type of the function menu implementation is null

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

Game logic: the return type of the game function is null. It should be clear that the pieces under the player and the pieces under the computer are equivalent to one character in the three-dimensional chess. At this time, we can define a two-dimensional array of characters to achieve this purpose

void game()
{
	char board[ROW][COL];
}

Note that at this time, the ROW and column cannot be just a number. We write the ROW as ROW and the column as COL, define it in the game.h header file, and finally include the header file

[game.h]

#define ROW 3
#define COL 3

At this time, initialize the array. At first, we don't want nothing in the array, and we don't want some strange things to appear. Therefore, we write all the spaces in the array first, and then overwrite them when entering characters. We also use this function to represent it, and the function is defined in the header file, Implement the corresponding function in the source file. The function declaration means to tell the compiler that I want to use this function. It doesn't matter if you don't find its definition now. Please don't report an error. I'll fill in the definition later.

[test.c]

void game()
{
	char board[ROW][COL];
	initboard(board, ROW, COL);
}

[game.c]

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("\n");
	
		if (i < row - 1)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
	
}

So far, the definition of chessboard is over. Next, let's talk about the logic of players and computers playing chess (at this time, computers only play chess randomly, and deep-level algorithms are not involved at this time)

First declare the function playermove

void playermove(char board[ROW][COL], int row, int col);

Then compile the function in game.c

void playermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("Player turn->\n");
	while (1)
	{
		printf("Please enter the chess position\n");
		scanf_s("%d %d", &x, &y);
		//Judge whether the landing point is legal
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//After input, judge whether to play chess at the same landing point repeatedly
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("The landing point is repeated, please re-enter\n");

		}
		else
			printf("Illegal input, please re-enter\n");
	}
	
}

At this time, the player's turn ends. In test.c, call the player's movement function, and then print the chessboard after the call

playermove(board, ROW, COL);//Call the player's function to play chess
		displayboard(board, ROW, COL);//Display the chess pieces on the chessboard after the player finishes playing chess

After that, the computer makes a drop and declares the function computermove

void computermove (char board[ROW][COL], int row, int col);

Then, the function of compiling the function in game.c is relatively easy for the computer to play chess. It can be placed randomly only by giving a random value and limiting it. The deep-seated algorithm logic will not be discussed at this time (for the method of obtaining the random number, you can look at my other blog how to generate the random number, which has the corresponding instructions)

void computermove(char board[ROW][COL], int row, int col)
{
	printf("Computer drop->");
	//For a computer, its behavior can be defined as random input, so we use functions to generate random numbers
//The column value entered by the computer is between 0 and 2, and the column value is between 0 and 2. We need to limit it
//The random number must be within 3 when it is summed to 3
	while (1)
	{
		int x = rand() % row;// The rand function calls srand when generating coordinates. In order to put it into the main function conveniently, the return value of the time function is used as the generator of random numbers
		int y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}

	}

}

The logic of players and computers playing chess has been completed. At this time, we should judge the victory or defeat of players or computers after each player and computer playing chess. This function should be called after players and computers playing chess respectively

		playermove(board, ROW, COL);//Call the player's function to play chess
		displayboard(board, ROW, COL);//Display the chess pieces on the chessboard after the player finishes playing chess
		iswin(board, ROW, COL);//Call the winning function
		computermove(board, ROW, COL);//Call the computer chess function
		displayboard(board, ROW, COL);//Call the computer to finish the chessboard
		iswin(board, ROW, COL);//Call the winning function

Function declaration

char iswin(char board[ROW][COL], int row, int col);
//Because you want to return characters, it is defined as char type 

//Player wins return* 

//Computer victory return#

//Draw return Q 

//The game goes on as usual return C

char iswin(char board[ROW][COL], int row, int col)
{
	//Judge whether the three rows and three columns and the main and auxiliary diagonals are equal. If they are equal, return
	//At this time, we return the player's victory to * and the computer's victory to #, which corresponds to the elements placed in the chessboard by the previous player and the computer, avoiding the trouble of creating new variables
	int i = 0;//Judge three lines
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1]&& board[i][1] == board[i][2] && board[i][0] != ' ')//Finally, determine whether it is a space. Any array element can be used because the three are the same
		{
			return board[i][0];
		}
	}
	//Judge three columns
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i]&& board[1][i] == board[2][i] && board[0][i] != ' ')//Finally, determine whether it is a space. Any array element can be used because the three are the same
		{
			return board[0][i];
		}
	}
	//Judge diagonal
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')//Main diagonal
	{
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')//Sub diagonal
	{
		return board[1][1];
	}
	//Judge the draw and call a function at this time
	int t = isfull(board, row, col);
	{
		if (t == 1)
		{
			return 'Q';
		}
		//continue
		return 'C';
	}

}

You can see that the isfull function is called, but this function has not been defined, so it has

//Function to judge whether the chessboard is full
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[row][col] == ' ');
			return 0;
		}
	}
	return 1;
}

After this function is defined, the logic of the whole simple Gobang is completed. Next, let's look at the output results

 

 

 

I use vs2019, readers can copy the code on their own     

Posted by zoidberg on Sat, 06 Nov 2021 22:12:11 -0700