How to write a three-man chess game with code

In order to make the logic look clearer, we will create 3 files to type the code this time, 1 header file (game.h), 2 .c files (game.c and test.c).
The game.h file mainly writes some preprocessing information, some header files of existing functions that need to be used, and declarations of custom functions.

#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 3
#define COL 3

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<Windows.h>

//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col);
//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);
//判断游戏进展情况
char CheckWin(char board[ROW][COL], int row, int col);

The game.c file mainly writes the specific definition of the custom function in game.h.

#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;
	for (i = 0; i < row; i++)
	{
    
    
		int j = 0;
		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 < col; j++)
			{
    
    
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}

void PlayerMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	printf("玩家走:>\n");
	while (1)
	{
    
    
		printf("请输入一个坐标:>");
		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("坐标被占用,请重新输入!\n");
		}
		else
		{
    
    
			printf("坐标非法,请重新输入!\n");
		}
	}
}

void ComputerMove(char board[ROW][COL], int row, int col)
{
    
    
	printf("电脑走:>\n");
	Sleep(2000);
	while (1)
	{
    
    
		int x = rand() % row;
		int y = rand() % col;
		if (board[x][y] == ' ')
		{
    
    
			board[x][y] = '*';
			break;
		}
	}
}

int IsFull(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;
		}
	}
	return 1;
}

char CheckWin(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][0] != ' ')
			return board[i][0];
	}
	//判断列
	for (i = 0; i < col; i++)
	{
    
    
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
			return board[0][i];
	}
	//判断对角线
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
		return board[0][0];
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
		return board[0][2];
	//判断是否为和其
	if (IsFull(board, row, col) == 1)
		return 'q';
	//胜负未定,继续游戏
	return 'c';
}

The test.c file mainly writes the main body code of the three-man chess game.

#include"game.h"

void menu()
{
    
    
	printf("|----------------------|\n");
	printf("|        1.play        |\n");
	printf("|        0.exit        |\n");
	printf("|----------------------|\n");
	printf("请输入:>\n");
}

void game()
{
    
    
	char ret = 0;
	char board[ROW][COL];
	//初始化棋盘,初始化为空格
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
	while (1)
	{
    
    
		//玩家下棋
		PlayerMove(board, ROW, COL);
		system("cls");
		DisplayBoard(board, ROW, COL);
		ret = CheckWin(board, ROW, COL);
		if (ret != 'c')
			break;
		//电脑下棋
		ComputerMove(board, ROW, COL);
		system("cls");
		DisplayBoard(board, ROW, COL);
		Sleep(1500);
		ret = CheckWin(board, ROW, COL);
		if (ret != 'c')
			break;
	}
	if (ret == '#')
		printf("玩家赢\n");
	else if (ret == '*')
		printf("电脑赢\n");
	else if (ret == 'q')
		printf("和棋\n");
	Sleep(1000);
}

int main()
{
    
    
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
    
    
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			system("cls");
			game();//三子棋的游戏
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新选择!\n");
			break;
		}
		system("cls");
	} while (input);
	return 0;
}

Typing the code like this will make it easier for you to sort out your ideas.

The game menu will be printed as soon as the code is executed. The player enters 1 to start the game, enters 0 to exit the game, so that when the player finishes a game, the number entered by the player can be used as the judgment condition of the do while loop, because entering the game requires input 1, so the player will enter the do while loop and print the menu again after finishing a game (for multiple games), until the player does not want to play, enter 0 to exit the game.
To start the game, we first define a two-dimensional array to store the chess pieces played by the player and the computer. First, assign the initial value to the space. When the player or the computer moves the chess somewhere, assign it to the corresponding character.
Print an empty board before the player makes a move. After that, the player or the computer prints the chess board after each move. Finally, by judging the chess pieces on the board, it is judged whether the computer wins or the player wins or draws (on the board). If the pieces are full but the undecided win is a draw), otherwise the game continues.
The function of system("cls"); in the code is to clear the screen, which can give us better visual effects. Sleep(1000); The function of the computer is to temporarily sleep for 1000 milliseconds (you can set it yourself) to prevent the screen from being cleared before you see the result. Their header files are stdlib.h and windows.h.
There are still many details worth paying attention to in the code, I will not list them one by one. Interested bloggers can copy the code to their compiler and run it to see how it works.

Guess you like

Origin blog.csdn.net/chenlong_cxy/article/details/113094291