The most complete and detailed explanation of three chess

If you want to write the sanbang code, you must have a start-to-finish idea:

First of all we have three files

game.h - declaration of the function

game.c - function implementation

test.c - test game

Create a menu (menu())

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

int main()
{
	int i = 0;

	do
	{
		//1. First create a menu so users can see how to choose.
		menu();
		printf("please choose:>");
		scanf("%d", &i);
		switch (i)
		{
		case 1:
			game();//The core of the main body of the game, put it in game.c for execution
			break;
		case 0:
			printf("exit the game");
			break;
		default:
			printf("Incorrect selection, please re-select\n");
			break;
		}

	} while (i);

	return 0;
}

Screenshot of the menu interface running:

 

Implementation of the core of the main body of the game code

1. Checkerboard initialization

void game()
{
        //Creation of the game board
	char arr[rows][cols] = {0};
        //Initialize the checkerboard function.
	Init_board(arr,rows,cols);
}

Implementation of the function:

void Init_board(char arr[rows][cols],int row,int col)
{
		int i = 0;//i means row
		int j = 0;//j means column
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			arr[i][j] =' ';//First initialize the chessboard to spaces
		}
	}
}

2. Print the chessboard

print_board(arr, rows, cols);//print checkerboard

Implementation of the function:

void print_board(char arr[rows][cols],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 ", arr[i][j]);
			if(j < col-1)
				printf("|");
		}
			printf("\n");
             //print split line
		if (i < row - 1)
		{
			int j = 0;
			for (j = 0; j < col ; j++)
			{
					printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}

Trivia: In order to improve the readability of the code, the global variable rows cols is used. With these two global variables, it will be easier to change the code in the future (it will be very convenient to change the chess to backgammon).

3. The player plays chess:

In order for players to play chess, they need to pay attention to the following points:

1. Receive the address position entered by the player (because the subscript of the array is 0, the coordinates entered by the player should be -1)

2. Determine whether the address entered by the player has a move

3. Put the player's pieces into the corresponding place

void Player_action(char arr[rows][cols],int row,int col)
{

	int i = 0;
	int j = 0;
	printf("player playing chess=<\n");

	while (1)
	{
		printf("Please enter coordinates=>:");
		scanf("%d %d", &i,&j);
		
		if(i >= 1 && i <= row && j >= 1 && j <= col)
		{
			if(arr[i - 1][j - 1] == ' ')
			{
				arr[i - 1][j - 1] = '*';
				break;
			}
			else
			{
				printf("The coordinates are already occupied, re-enter\n");
			}
		}
		else
		{
			printf("Illegal coordinates, re-enter\n");
		}

	}

}

4. Computer playing chess

To implement computer chess, you must call the header file:

  1. <stdlib>
  2. <time.h>

If you want to limit the coordinates under the computer to a certain value, you also need:

  1. srand((unsigned int)time(NULL)); coordinate initialization
  2. x = rand() % row;
    y = rand() % col; create random number

At the same time, it is judged whether the address placed by the computer is occupied, and the chess piece is dropped.

code show as below:

void Computer_operation(char arr[rows][cols], int row, int col)
{
		printf("computer chess\n");
	int i = 0;
	int y = 0;

	while(1)
	{ 
		i = rand() % row;
		y = rand() % col;
		if (arr[i][y] == ' ')
		{
			arr[i][y] = '#';//The computer plays chess
			break;
		}

	}

}

The srand((unsigned int)time(NULL)) function should be placed in the main() function.

5. Judgment of the outcome of the game

Due to the particularity of the rules, the computer and the player need to judge the result of each move to ensure that the logic of the program can be run.

Here we use the Winning() function to achieve this.

Also need to implement the following logic:

1. Player wins '*'
2. Computer wins '#'
3. Tie ‘C’
4. Continue with 'F'

The function is located throughout the main body of the game:

void game()
{
	char ret = 0;
    
	char arr[rows][cols] = {0};//Checkerboard Array Creation
    //1. Initialize the chessboard function.
	Init_board(arr,rows,cols);

	//2. Print the chessboard.
	print_board(arr, rows, cols);

	//3. Play chess.
	while(1)
	{ 
		Player_action(arr, rows, cols);//Players play chess.

		//Judging win or lose
		ret = Winning(arr, rows, cols);

		if (ret != 'F')
		{
			break;
		}
		print_board(arr, rows, cols);

		Computer_operation(arr, rows, cols);//Computer playing chess.
		//Judging win or lose
		ret = Winning(arr, rows, cols);
		if (ret != 'F')
		{
			break;
		}
		print_board(arr, rows, cols);
	}
	if (ret == '*')
	{
		print_board(arr, rows, cols);
		printf("player wins\n");	
	}

	else if (ret == '#')
	{
		print_board(arr, rows, cols);
		printf("computer wins\n");
	}
	else
	{
		printf("draw\n");
	}
}

The Winning() function is as follows:

char Winning(char arr[rows][cols], int row, int col)
{
	//Judgment lines linked together
	int i = 0;
	for (i = 0; i < row; i++)
	{
		if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2] && arr[i][1] != ' ')
		{
			return arr[i][i];
		}
	}

	//Judgment columns are linked together
	int j = 0;
	for (j = 0; j < col; j++)
	{
		if (arr[0][j] == arr[1][j] && arr[1][j] == arr[2][j] && arr[1][j] != ' ')
		{
			return arr[1][j];
		}
	}

	//Judging that the diagonals are connected together
	if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[1][1] != ' ')
	{
		return arr[1][1];
	}

	if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[1][1] != ' ')
	{
		return arr[1][1];
	}

	//Judging a draw
	if (draw(arr, row, col))
	{
		return 'C';
	}

	return 'F';//Continue the game
}

"draw" in the draw() function:

int draw(char arr[rows][cols], int row, int col)
{
	int i = 0;
	int j = 0;
	
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (arr[i][j] == ' ')
			{
				return 0;//There are still spaces, continue.
			}
		}

	}
	return 1;//no spaces, tie
}

The overall implementation of the game:

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

//Game body
void game()
{
	char ret = 0;

	char arr[rows][cols] = {0};
	//1. Initialize the chessboard function.
	Init_board(arr,rows,cols);
	//2. Print the chessboard.
	print_board(arr, rows, cols);
	//3. Play chess.
	while(1)
	{ 
		Player_action(arr, rows, cols);//Players play chess.
		//Judging win or lose
		ret = Winning(arr, rows, cols);
		if (ret != 'F')
		{
			break;
		}
		print_board(arr, rows, cols);

		Computer_operation(arr, rows, cols);//Computer playing chess.
		//Judging win or lose
		ret = Winning(arr, rows, cols);
		if (ret != 'F')
		{
			break;
		}
		print_board(arr, rows, cols);
	}
	if (ret == '*')
	{
		print_board(arr, rows, cols);
		printf("player wins\n");
		
	}

	else if (ret == '#')
	{
		print_board(arr, rows, cols);
		printf("computer wins\n");
	}
	else
	{
		printf("draw\n");
	}
}

//main program
int main()
{
	srand((unsigned int)time(NULL));

	int i = 0;
	do
	{
		//1. First create a menu so users can see how to choose.
		menu( );

		printf("please choose:>");
		scanf("%d", &i);

		switch (i)
		{
		case 1:
			game();//game
			break;
		case 0:
			printf("exit the game");
			break;
		default:
			printf("Incorrect selection, please try again\n");
			break;
		}

	} while (i);

	return 0;
}

game running live

 

 

 

The above is the detailed explanation of three pieces of chess. If you have learned it, don't hurry to operate it once to deepen your memory.

Tags: C programming language

Posted by commandonz on Sun, 15 May 2022 01:31:55 +0300