Simple implementation of Sanzi chess with C language

I How to design

First of all, we can use functions to divide the implementation of Sanzi chess into different modules. We can think about it. For a game, there must first be a directory for players to choose whether to play or not, that is, the menu interface; Secondly, we have to have a chessboard. The next step is for the player to fall, and then the computer to fall. After each move of chess, we have to judge whether to win or lose. The final result is the player's victory, the computer's victory or a draw.

II code implementation

1. Menu creation

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


void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));

	do
	{
		menu();
		printf("Please select:>");
		scanf_s("%d", &input);
		switch (input)
		{
		case 1:
			game();//game
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Selection error\n");
			break;
		}
	} while (input);
}

2. Create chessboard and initialize chessboard

The chessboard mainly adopts a two-dimensional array of characters, in which we can define macros to make the size of the chessboard easy to control. The implementation process is as follows:

2.1. Ding Yihong

#define ROW 3
#define COL 3
char board[3][3]={0}

2.2. Chessboard initialization

Initially, each space in the array is used to place spaces and initialize the chessboard

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] = ' ';
		}
	}
}

2.3. Split chessboard

Since there are only spaces in the array during initialization, the chessboard looks blank at this time, so we need to split the chessboard to make each of them more obvious:

oid DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		//print data
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col - 1)
				printf("|");
		}
		printf("\n");
		//Print split rows
		if (i < row - 1)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}

3. Players play chess

We remember that the place where the player has written down is *, and the place where the computer has written down is #. For the convenience of the player, the coordinates of the normal array start from 0, but for the convenience of the player, the coordinates of the player have to start from 1

void player_move(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("Players play chess\n");
	while (1)
	{
		printf("Please enter coordinates:>");
		scanf_s("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//play chess
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("This coordinate is occupied, please re-enter\n");
			}
		}
		else
		{
			printf("Illegal coordinates, please re-enter\n");
		}
	}

4. Computer chess

The computer uses rand to randomly generate row and column coordinates, # representing the position of the computer

void computer_move(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("Computer chess:>\n");
	while (1)
	{
		x = rand() % row;//0~2
		y = rand() % col;//0~2
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

5. Judge the winner or loser

We stipulate that if the player wins, the * sign will be returned; if the computer wins, the # number will be returned; if the game is tied, the Q will be returned; if the game continues, the C will be returned

The winning condition is that all rows, columns and diagonals have three identical

static int if_full(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;//Not full
			}
		}
	}
	return 1;//Full
}

char is_win(char board[ROW][COL], int row, int col)
{
	int i = 0;
	//Judgment line
	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];
		}
	}
	//Judgment column
	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];
		}
	}
	//diagonal
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
	{
		return board[1][1];
	}

	//Judge the draw
	if (if_full(board, row, col) == 1)
	{
		return 'Q';
	}

	//continue
	return 'C';
}

Keywords: C Back-end

Added by smixcer on Tue, 25 Jan 2022 03:11:28 +0200