How to use C language to implement a simple three-man chess game

First put the source code for reference, the detailed explanation is below

test.c

#include "game.h"

void menu()
{
	printf("**************************\n");
	printf("******   1. play     *****\n");
	printf("******   0. exit     *****\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);
		ret = CheckWin(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		DisplayBoard(board, ROW, COL);
		//电脑下棋
		ComputerMove(board, ROW, COL);//随机下棋的
		ret = CheckWin(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		DisplayBoard(board, ROW, COL);
	}
	if (ret == '*')
		printf("玩家赢了\n");
	else if (ret == '#')
		printf("电脑赢了\n");
	else if (ret == 'Q')
		printf("平局\n");
	DisplayBoard(board, ROW, COL);
}

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

game.c

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

void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		//打印数据
		//printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		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)
		{
			//printf("---|---|---\n");
			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] != ' ')
			{
				printf("坐标被占用,请重新输入!\n");
			}
			else
			{
				board[x-1][y-1] = '*';
				break;
			}
		}
		else
		{
			printf("坐标非法,请重新输入!\n");
		}

	}

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

static 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;
		}
	}
	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[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 (IsFull(board,row,col) == 1)
	{
		return 'Q';
	}
	//继续
	//前面没有赢,没有平局,就是继续
	return 'C';
}

//电脑赢了 - 返回'#'说明电脑赢
//玩家赢了 - 返回'*'说明玩家赢
//平局     - 返回'Q'说明平局
//继续     - 返回'C'说明继续

game.h

//game.h放函数声明
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ROW 3
#define COL 3

//初始化棋盘
void InitBoard(char board,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,int row,int col);

//判断输赢的
char CheckWin(char board[ROW][COL],int row,int col);

-------------------split line-------------------

The printing of the main function and the menu is in the source code, the implementation method is relatively simple, I will not elaborate on it

 

When 1 is selected, we enter the game function. Since we want to play chess, we must have a chess board. The chess rule I set is that the player plays the character*, the computer plays the character#

So we should define a two-dimensional character array

	//数据的存储是二位数组
	char board[ROW][COL];

Then to initialize the chessboard, here we write an initialization operation function,

	//初始化棋盘-初始化为空格
	InitBoard(board, ROW, COL);

To make it easier to modify the size of the chessboard later, we can make a macro definition in the header file. Here we take a 3×3 chessboard as an example

#define ROW 3
#define COL 3

 

Initialize the chessboard to spaces

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] = ' ';
		}
	}
}

 

After the initialization is complete, we need to print the chessboard and display it on the program. At this time, we need to write a function DisplayBoard to print the chessboard.

DisplayBoard(board, ROW, COL);

If we simply use the for loop to take a picture and print, we are still very inflexible in controlling the size of the board.

		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		printf("---|---|---\n");

So we changed our mindset to print

void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		//打印数据
		//printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		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)
		{
			//printf("---|---|---\n");
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
		
		
	}
}

This printing method is convenient for us to control the size of the board more flexibly. In this way, we can change the size of the board as long as we change the values ​​of ROW and COL.

Then we compile and run, the board prints as follows

A simple chessboard is completed

 

Next is the chess game. Chess is divided into player chess and computer chess. Let’s first realize the player’s chess.

		//玩家下棋
		PlayerMove(board, ROW, COL);

 

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] != ' ')
			{
				printf("坐标被占用,请重新输入!\n");
			}
			else
			{
				board[x-1][y-1] = '*';
				break;
			}
		}
		else
		{
			printf("坐标非法,请重新输入!\n");
		}

}

Each time the player successfully enters a coordinate, it will jump out of the loop, print the board once, and enter the coordinate on the computer

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

When using the rand function, remember to add such a sentence at the beginning of the main function to achieve true randomness

	srand((unsigned int)time(NULL));

When the player lands once, the computer lands once, the round is over. After each round, the result of winning or losing must be judged once. Here is a function CheckWin function that judges winning or losing. The return value is an integer, which is used ret. receive

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[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 (IsFull(board,row,col) == 1)
	{
		return 'Q';
	}
	//继续
	//前面没有赢,没有平局,就是继续
	return 'C';
}

 

When judging a tie, define an IsFull function. The return value of the IsFull function is modified with static, indicating that this is a static value.

static 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;
		}
	}
	return 1;
}

Then it should be noted that every time the player plays a chess and every time the computer plays a chess, the result of winning or losing must be judged once. If the return value is the character C, it means that the game can continue.

 

 

The results of the program are as follows

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_52454367/article/details/113175560
Recommended