Tic_Tac_Toe (three piece chess) game -- C language implementation

1 demand analysis

Sanzi chess is a traditional folk game, also known as Jiugong chess, circle fork, one dragon, Jingzi chess, etc. Rules of the game: the two sides take turns to settle the pieces. The one who takes the lead in forming a straight line between the horizontal, vertical and opposite corners wins.

The project aims to realize simple man-machine combat with C language, mainly realizing the following functions:

Function 1

Menu selection
Function 2Initialize chessboard
Function 3Print chessboard
Function 4Man machine drop
Function 5Judge whether the chess board is full

2 business process

        

3 code display

3.1 header file (storing function declarations, including library functions)

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define ROW 3
#define COL 3
//Initialize chessboard
void Initialize_checkerboard(char board[ROW][COL],int, int);
//Print chessboard
void Display_checkerboard(char board[ROW][COL], int, int);
//Players play chess
void Player_move(char board[ROW][COL], int, int);
//Machine chess
void Computer_move(char board[ROW][COL], int, int);
//Judge whether to win or lose
char CheckWin(char board[ROW][COL], int row, int col);

3.2 function file (realize the main functions of the game)

3.2. 1 initialize the chessboard

Loop through the two-dimensional array and assign the array elements to spaces.

void Initialize_checkerboard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (size_t i = 0; i < row; i++)
	{
		for (size_t j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}

3.2. 2 print chessboard

Printing chessboard is divided into two steps: 1 Print "num|num|num" 2 Print "--- |---". The implementation code is as follows:

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

		if (i < row - 1)
		{
			for (size_t j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
		}
		printf("\n");
	}

}

3.2. 3. Player drop

The player's position is operated by inputting coordinates, initializing coordinates x and y, and judging the legitimacy of coordinates after the player inputs coordinates. If the coordinates are within the chessboard range and the entered coordinates are not occupied, replace the initialized space with "*" to indicate the player's position. If the coordinates have been occupied, the player will be prompted to re-enter. If the input coordinates do not conform to the law, the coordinate error will be prompted, which will be re entered by the player.

void Player_move(char board[ROW][COL], int row, int col)
{
	int x_coordinate = 0;
	int y_coordinate = 0;
	printf("Player turn:>\n");//Prompt players to enter
	while (1)
	{
		int x_coordinate = 0;
		int y_coordinate = 0;
		printf("Input coordinates:>");
		scanf("%d %d", &x_coordinate, &y_coordinate);
		if (x_coordinate >= 1 && x_coordinate <= row && y_coordinate >= 1 && y_coordinate <= col)//Judge the legitimacy of coordinates
		{
			if (board[x_coordinate - 1][y_coordinate - 1] == ' ')
			{
				board[x_coordinate - 1][y_coordinate - 1] = '*';
				break;
			}
			else
			{
				printf("The coordinates are already occupied...Enter again\n");
			}
		}
		else
		{
			printf("Coordinate error!\n");
		}
	}
}

3.2. 4. Machine drop

The implementation of the machine drop is roughly the same as that of the player, except that the machine drop is a random coordinate. The random coordinate implementation first uses the srand function: srand((unsigned int)time(NULL)). This function provides variable values for rand() function with time variation, so as to achieve the effect of different random values. Then take the remainder of the random value according to the number of rows and columns, and compress any value within the range of the number of rows and columns, so as to obtain the random horizontal and vertical coordinates in the chessboard coordinates.

void Computer_move(char board[ROW][COL], int row, int col)
{
	printf("Playing....\n");//Game status display
	int x_coordinate = 0;
	int y_coordinate = 0;
	printf("Computer turn:>\n");
	while (1)
	{
		x_coordinate = rand() % row;//Random abscissa
		y_coordinate = rand() % col;//Random ordinate
		if (board[x_coordinate][y_coordinate] == ' ')
		{
			board[x_coordinate][y_coordinate] = '#';
			break;
		}
	}
}

 3.2. 4. Judge whether the winning or losing and whether the chessboard is full

The logic of judging whether to win or lose is: the first consistent Party of row, column and diagonal is the winner. Then, three situations need to be discussed to judge whether to win or lose. When the row, column and diagonal are equal, Returns the content of a piece on the line ("*" or "#" )Don't forget that there is a draw at this time. When deciding whether to draw a game, it is necessary to consider whether the checkerboard is full. If the checkerboard is full, it will return 1 to the winning and failing judgment function, otherwise it will return to 0. The winning and winning judgment function will be judged after receiving the return value. If it is not full, it will return to C, and it will return to Q when it 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[i][j] == ' ')
			{
				return 0;//The chessboard is not full
			}
		}
	}
	return 1;//The chessboard is full
}

char CheckWin(char board[ROW][COL], int row, int col)
{
	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][1] != ' ')
		{
			return  board[i][1];//
		}
	}

	//Judge three columns
	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];
		}
	}

	//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];
	}

	//Judge the draw
	//If the chessboard is full, return 1, and if not, return 0
	int ret = IsFull(board, row, col);
	if (ret == 1)
	{
		return 'Q';
	}

	//continue
	return 'C';

}

3.3 operation documents (game execution)

Print the menu, select whether to play the game, and judge the legitimacy of the input value. After starting the game, the player will settle first. After the player settles, he will judge whether the chess board is full. If the return value of the win / loss judgment function is C and the chess board is not full, the program will continue to execute. On the contrary, after printing the draw, exit the cycle and re select the menu. If the chessboard is not full, clear the screen and place the machine. After the machine is placed, judge the winner or loser, judge whether the chessboard is full, and finally print the screen to realize the effect of playing chess on a chessboard.

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

void game()
{
	char board[ROW][COL];
	Initialize_checkerboard(board, ROW, COL);
	Display_checkerboard(board, ROW, COL);
	char ret = 0;
	while (1)
	{
		Player_move(board, ROW, COL);
		ret = CheckWin(board, ROW, COL);
		if (ret != 'C')
		{
            printf("DRAW!!\n");
			break;
		}
		system("cls"); //Realize the man-machine game on a chessboard
		Computer_move(board, ROW, COL);
		ret = CheckWin(board, ROW, COL);
		if (ret != 'C')
		{
			printf("DRAW!!\n");
			break;
		}
		Display_checkerboard(board, ROW, COL);
	}
	if (ret == '*')
	{
		printf("PLAYER WIN!!\n");
	}
	else if (ret == '#')
	{
		printf("COMPUTER WIN!!\n");
	}
	else
	{
		printf("CONTINUE!!\n");
	}
	Display_checkerboard(board, ROW, COL);
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("Make your choice:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Game Over...\n");
			break;
		default:
			printf("You make wrong choice!");
			break;
		}
	} while (input);
	return 0;
}

4 effect display

4.1 initialize and print the chessboard after starting the game

4.2 realize playing chess on a chessboard and display the status in the game

4.3 display the chessboard after a game

 

EOF

Keywords: C

Added by sandsquid on Thu, 30 Dec 2021 17:55:43 +0200