Artificial mentally retarded three piece chess

Posted by Zyxist on Tue, 22 Feb 2022 06:01:09 +0100

After learning arrays and functions, we can do some interesting things, such as the Gobang game mentioned in this article.

First of all, before we start to write code, we need to know what the game needs and divide the modules.

  1. Game menu
  2. How do players choose game mode
  3. game board
  4. How players play chess
  5. Computer chess method
  6. How to judge winning or losing

Next, let's improve the content of the module

1. Game menu

The game menu of Sanzi chess is very simple. It only takes 1 Start the game and 2 Just end the game

void menu()
{
	printf("   1.Start the game   ");
	printf("   0.End the game   ");
}

2. How do players choose game mode

We can use the switch statement to perform different game modes according to the value selected by the player

void test()
{
	int input = 0;
	do
	{
		menu();
		printf("Please select\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("End the game\n");
			break;
		default:
			printf("Wrong choice\n");
			break;
		}
	} while (input);
}

3. Game board

Use the two-dimensional array to store the chess pieces. According to the Sanzi game, you can print a 3 by output × 3 chessboard, and its contents need to be saved. Therefore, a 3 can be used × 3 to save.

1) Initialization of chessboard

void InitBoard(char Board[3][3], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			Board[i][j] = ' ';
		}
	}
}

2) Presentation of chessboard: the chessboard of Sanzi is a tic tac toe, so it needs to reasonably arrange | symbols and - -- Symbols in order to print correctly

void DiplayBoard(char Board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf(" %c ", Board[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < row - 1)
		{
			for (int j = 0; j < col; j++)
			{
				printf("---");
				if (j != col - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

4. How players play chess

Factors to consider before settling

1) The player is not a programmer and doesn't know that the array starts from 0, so it needs to be optimized slightly

2) Before falling, you need to check whether the coordinates entered by the player are legal

3) Is there a piece in the player's position before falling

The way to play chess is to enter the elements of the array to drop the seeds

Playmove(char Board[ROW][COL], int row, int col)
{
	int x = 0, y = 0;
	printf("Please play chess\n");
	while (1)
	{
		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("The location is already occupied\n");
			}

		}
		else
		{
			printf("Input error\n");
		}

	}
}

5. Methods of computer playing chess

What we use here is a chess playing method of artificial mental retardation: that is, we can use the time stamp to deal with it to make the number within the range of the array.

void Computermove(char Board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (Board[x - 1][y - 1] == ' ')
		{
			Board[x - 1][y - 1] = '#';
			break;
		}
	}
}

5. How to judge the winner

The situation of winning Sanzi chess

1) There are three pieces in a line

2) There are three pieces in a line

3) There are three pieces in the diagonal line

char is_win(char Board[ROW][COL], int row, int col)
{
	//Judgment line
	for (int 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 (int 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];
		}
	}
	//Judge 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];
	}
	//it ends in a draw
	if (is_full(Board,ROW,COL) == 1)
	{
		return 'Q';
	}
	//continue
	return 'C';
}

In Sanzi chess, it is mainly the application of array, and then it is to clarify the logic of chessboard and the logic of winning or losing. After figuring out these, you can easily realize Sanzi chess

Topics: C C++