[C language] implementation of Sanzi chess

catalogue

1, Clear up logic

2, Create file

3, Specific steps

1. Create menu and implementation method

2. Create and initialize a two-dimensional array

3. Print chessboard

4. Both sides play chess

1) Players play chess

2) Computer chess

5. Judge the outcome

4, Operation results

5, Complete code display

1, Clear up logic

First of all, let's deal with the logic of Sanzi chess

1. Print game menu

2. Create and initialize a two-dimensional array

3. Print chessboard

4. Both sides play chess

5. Judge the outcome

2, Create file

I created three files, test.c, game.h, and game.c

test.c file is used to realize the logic of entering the game, exiting the game, judging win or lose, printing menu, etc

game.c is used to write the main implementation method of the game

game.h stores header files and function declarations

3, Specific steps

1. Create menu and implementation method

Write it in the test.c file

void menu()
{
	printf("********************************\n");
	printf("*********   1.play   ***********\n");
	printf("*********   0.exit   ***********\n");
	printf("********************************\n");
}
void test()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf(":>");
		
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("The Gobang game begins\n");
			game();
			break;
		case 0:
			printf("End the game\n");
			break;
		default:
			printf("Input error\n");
			break;

		}
	} while (input);
}

2. Create and initialize a two-dimensional array

Create a two-dimensional array in test.c and reference the method of initializing the array

void game()
{
	//Create a 2D array
	char board[ROW][COL] = { 0 };
	//Initialize 2D array
	InitBoard(board, ROW, COL);
}

In game.h, use macros to define rows and columns, declare functions, and fill in the required header files

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#define ROW 3
#define COL 3

Define the method of initializing the array in game.c

#include "game.h"
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] = ' ';
		}
	}
}

3. Print chessboard

Define the method of printing chessboard in game.c. It should be noted that line wrapping is required after each line is printed

void DisplayBoard(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++)
		{
			printf(" %c ",board[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j < row; j++)
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
		}
		printf("\n");
	}
}

This function is referenced in the game method in test.c

4. Both sides play chess

The man-machine and the man-machine play chess in turn, so the man-machine chess steps should be put into the cycle in test.c

//play chess
	while (1)
	{
		//Players play chess
		player_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//Computer chess
		computer_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
	}

1) Players play chess

Define the method of playing chess in game.c, taking into account whether the chess is within the range and whether the position is repeated,

Because we implement it through a two-dimensional array, the array subscripts corresponding to the coordinates x and y we enter should be x-1 and y-1

void player_move(char board[ROW][COL], int row, int col)
{
	printf("Players play chess:>");
	int x = 0;
	int y = 0;
	
	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("This coordinate is occupied, please re-enter!\n");
		}
		else
			printf("Illegal coordinates,Please re-enter!\n");
	}
	

}

2) Computer chess

In game.c, I define the method of computer chess. I use a relatively simple method to generate random numbers with rand function to randomly establish coordinates. Of course, this ai algorithm can be optimized.

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;
		y = rand() % COL;
		if (board[x][y]== ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

5. Judge the outcome

The result of Sanzi chess is nothing more than three. Players win, computers win and draw

We first define a method to realize the judgment result in game.c

If any row is the same or any column is the same or the diagonal is the same, it is regarded as victory

//Player wins: '*'
//Computer wins: '#'
//Draw: Q
//Continue: C
int is_full(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;
			}
		}
	}
	return 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
	int i = 0;
	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];
		}
	}
	for (i = 0; i < row; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
	}
	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];
	}
	if (is_full(board, row, col)==1)
	{
		return 'Q';
	}
	//continue
	return 'C';
}

Implement the logic of judging the outcome in test.c

char ret = 0;
	while (1)
	{
		//Players play chess
		player_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		//Computer chess
		computer_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
	}
	//Judge the outcome
	if (ret == '*')
	{
		printf("The player won!\n");
	}
	else if (ret == '#')
	{
		printf("The computer won!\n");
	}
	else
	{
		printf("it ends in a draw\n");
	}
}

4, Operation results

Now let's show me a wonderful game with the AI

 

Well, it seems that the ai I created is strong enough to beat me. I'm really powerful!

5, Complete code display

game.h

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#define ROW 3
#define COL 3

void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void player_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
	printf("********************************\n");
	printf("*********   1.play   ***********\n");
	printf("*********   0.exit   ***********\n");
	printf("********************************\n");
}
void game()
{
	//Create a 2D array
	char board[ROW][COL] = { 0 };
	//Initialize 2D array
	InitBoard(board, ROW, COL);
	//Print chessboard
	DisplayBoard(board, ROW, COL);
	//play chess
	char ret = 0;
	while (1)
	{
		//Players play chess
		player_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		//Computer chess
		computer_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
	}
	//Judge the outcome
	if (ret == '*')
	{
		printf("The player won!\n");
	}
	else if (ret == '#')
	{
		printf("The computer won!\n");
	}
	else
	{
		printf("it ends in a draw\n");
	}
}
void test()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf(":>");
		
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("The Gobang game begins\n");
			game();
			break;
		case 0:
			printf("End the game\n");
			break;
		default:
			printf("Input error\n");
			break;

		}
	} while (input);
}

int main()
{
	test();
	return 0;
}

game.c

 

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
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] = ' ';
		}
	}
}
void DisplayBoard(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++)
		{
			printf(" %c ",board[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j < row; j++)
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
		}
		printf("\n");
	}
}

void player_move(char board[ROW][COL], int row, int col)
{
	printf("Players play chess:>");
	int x = 0;
	int y = 0;
	
	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("This coordinate is occupied, please re-enter!\n");
		}
		else
			printf("Illegal coordinates,Please re-enter!\n");
	}
	

}

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;
		y = rand() % COL;
		if (board[x][y]== ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}


int is_full(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;
			}
		}
	}
	return 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
	int i = 0;
	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];
		}
	}
	for (i = 0; i < row; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
	}
	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];
	}
	if (is_full(board, row, col)==1)
	{
		return 'Q';
	}
	//continue
	return 'C';
}

Of course, with the increasing knowledge, the game can be continuously optimized.

Tags: C

Posted on Thu, 18 Nov 2021 14:00:35 -0500 by Beavis2084