Xiaobai's simple three-brows

In order to improve the readability of the code, the game is divided into three parts, test.c (the steps to express yourself), game.h (the declaration of the function before the game code block) game.c (the game code block The implementation of the function in the .c file needs to refer to the function declaration in the .h file. For example, the use of the library function printf needs to use the header file #include <stdio.h>, which must be used in the summary of the .c file Library functions, you can put the header file in the game.h header file, and use #include "game.h" when using the .c file. In the end, what we need is such a backgammon template and execute it on it. Play chess, then analyze
insert image description here

test.c

Simple menu menu

Prompt for the number to choose

void menu()
{
    
    
	printf("********************\n");
	printf("****1.play 0.exit***\n");
	printf("********************\n");
}

Do you want to start the game

Enter 1 to start the game, 0 to exit the game, select other prompts to enter the wrong input, then re-enter, in order to be able to play more than one game, you can use do... while to ensure that the game can be played

int main()
{
    
    
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
    
    
		menu();//游戏菜单
		printf("请输入:>");
		scanf("%d", &input);
		switch (input)//判断是否开始游戏
		{
    
    
		case 1:
			printf("游戏开始\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

enter the game

Create a two-dimensional array, use the array to initialize the chessboard, pass the array, the rows and columns of the array to the formal parameters, print the chessboard in the same way, and use the chessboard created by the array in the game, so the array, The row and column are passed to the formal parameters. After the chessboard is initialized and printed, the chess game is played. The chess game is not only a chess piece, but the player will play and the computer will play. Therefore, a loop is needed to control the repetitive order of chess, and the player wins and the computer. Win and draw are replaced by characters respectively, and the corresponding character is returned if the condition of who wins is met, otherwise the game continues until the draw.

void game()
{
    
    
	char ret;
	char board[3][3] = {
    
     0 };
	//初始化棋盘
	Initboard(board, ROW, COL);
	//打印棋盘
	Displayboard(board, ROW, COL);

	//1.玩家用*表示
	// 2.电脑用#表示
	//判断输赢
	//1.玩家赢返回*
	//2.电脑赢返回#
	//3.继续游戏返回C
	//4.平局返回P
	while (1)
	{
    
    
		//玩家下棋
		PlayerMove(board, ROW, COL);
		Displayboard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
		//电脑下棋
		ComputMove(board, ROW, COL);
		Displayboard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '*')
		printf("玩家赢\n");
	else if (ret == '#')
		printf("电脑赢\n");
	else
		printf("平局\n");
}

test.c complete

#include "game.h"
void menu()
{
    
    
	printf("********************\n");
	printf("****1.play 0.exit***\n");
	printf("********************\n");
}
void game()
{
    
    
	char ret;
	char board[3][3] = {
    
     0 };
	//初始化棋盘
	Initboard(board, ROW, COL);
	//打印棋盘
	Displayboard(board, ROW, COL);

	//1.玩家用*表示
	// 2.电脑用#表示
	//判断输赢
	//1.玩家赢返回N
	//2.电脑赢返回Q
	//3.平局返回P
	while (1)
	{
    
    
		//玩家下棋
		PlayerMove(board, ROW, COL);
		Displayboard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
		//电脑下棋
		ComputMove(board, ROW, COL);
		Displayboard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '*')
		printf("玩家赢\n");
	else if (ret == '#')
		printf("电脑赢\n");
	else
		printf("平局\n");
}
	
int main()
{
    
    
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
    
    
		menu();//游戏菜单
		printf("请输入:>");
		scanf("%d", &input);
		switch (input)//判断是否开始游戏
		{
    
    
		case 1:
			printf("游戏开始\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

game.h

Header file declaration, declare the function to be implemented

#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include <stdio.h>
#include <stdlib.h>
#include <time.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 PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputMove(char board[ROW][COL], int row, int col);
//判断输赢
char IsWin(char board[ROW][COL], int row, int col);

Next is the specific implementation of the function

game.c

concrete implementation of the function

Initialize the board

Initialize the chessboard as a space, print the appearance of the chessboard, and play chess. Whoever wins returns the string entered by whoever wins, and returns a custom string if it is a draw

#include "game.h"

//初始化棋盘
void Initboard(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++)
		{
    
    
			board[i][j] = ' ';//初始化为空格
		}
	}
}

print checkerboard

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

player playing chess

//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	//输入坐标
	
	while (1)
	{
    
    
		printf("请输入坐标:");
		scanf("%d%d", &i, &j);
		//判断输入坐标合法性
		if (i - 1 >= 0 && i - 1 < row && j - 1 >= 0 && j - 1 < col)
		{
    
    
			if (board[i - 1][j - 1] = ' ')
			{
    
    
				board[i - 1][j - 1] = '*';
				break;
			}
			else
				printf("此坐标已被占用,请重新输入\n");
		}
		else
			printf("坐标非法,请重新输入\n");
	}
}

computer chess

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

judge win or lose

//判断输赢
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 IsWin(char board[ROW][COL], int row, int col)
{
    
    
	int i = 0;
	int j = 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 (j = 0; j < col; j++)
	{
    
    
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j]!=' ')
			return board[0][j];
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1]!=' ' || 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 'P';
	return 'C';
}

void menu()
{
    
    
	printf("********************\n");
	printf("****1.play 0.exit***\n");
	printf("********************\n");
}
void game()
{
    
    
	char ret;
	char board[3][3] = {
    
     0 };
	//初始化棋盘
	Initboard(board, ROW, COL);
	//打印棋盘
	Displayboard(board, ROW, COL);

	//1.玩家用*表示
	// 2.电脑用#表示
	//判断输赢
	//1.玩家赢返回N
	//2.电脑赢返回Q
	//3.平局返回P
	while (1)
	{
    
    
		//玩家下棋
		PlayerMove(board, ROW, COL);
		Displayboard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
		//电脑下棋
		ComputMove(board, ROW, COL);
		Displayboard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '*')
		printf("玩家赢\n");
	else if (ret == '#')
		printf("电脑赢\n");
	else
		printf("平局\n");
}
	
int main()
{
    
    
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
    
    
		menu();//游戏菜单
		printf("请输入:>");
		scanf("%d", &input);
		switch (input)//判断是否开始游戏
		{
    
    
		case 1:
			printf("游戏开始\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

game.c complete

#include "game.h"
//初始化棋盘
void Initboard(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++)
		{
    
    
			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)
		{
    
    
			int j = 0;
			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 i = 0;
	int j = 0;
	while (1)
	{
    
    
	    //输入坐标
		printf("请输入坐标:");
		scanf("%d%d", &i, &j);
		//判断输入坐标合法性
		if (i - 1 >= 0 && i - 1 < row && j - 1 >= 0 && j - 1 < col)
		{
    
    
			if (board[i - 1][j - 1] = ' ')
			{
    
    
				board[i - 1][j - 1] = '*';
				break;
			}
			else
				printf("此坐标已被占用,请重新输入\n");
		}
		else
			printf("坐标非法,请重新输入\n");
	}
}
//电脑下棋
void ComputMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	
	printf("电脑下:\n");
	while (1)
	{
    
    
		x = rand() % row;
		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 IsWin(char board[ROW][COL], int row, int col)
{
    
    
	int i = 0;
	int j = 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 (j = 0; j < col; j++)
	{
    
    
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j]!=' ')
			return board[0][j];
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1]!=' ' || 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 'P';
	return 'C';
	
}

full code

#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include <stdio.h>
#include <stdlib.h>
#include <time.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 PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputMove(char board[ROW][COL], int row, int col);
//判断输赢
char IsWin(char board[ROW][COL], int row, int col);

//初始化棋盘
void Initboard(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++)
		{
    
    
			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)
		{
    
    
			int j = 0;
			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 i = 0;
	int j = 0;
	while (1)
	{
    
    
	    //输入坐标
		printf("请输入坐标:");
		scanf("%d%d", &i, &j);
		//判断输入坐标合法性
		if (i - 1 >= 0 && i - 1 < row && j - 1 >= 0 && j - 1 < col)
		{
    
    
			if (board[i - 1][j - 1] = ' ')
			{
    
    
				board[i - 1][j - 1] = '*';
				break;
			}
			else
				printf("此坐标已被占用,请重新输入\n");
		}
		else
			printf("坐标非法,请重新输入\n");
	}
}
//电脑下棋
void ComputMove(char board[ROW][COL], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	
	printf("电脑下:\n");
	while (1)
	{
    
    
		x = rand() % row;
		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 IsWin(char board[ROW][COL], int row, int col)
{
    
    
	int i = 0;
	int j = 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 (j = 0; j < col; j++)
	{
    
    
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j]!=' ')
			return board[0][j];
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1]!=' ' || 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 'P';
	return 'C';
}
void menu()
{
    
    
	printf("********************\n");
	printf("****1.play 0.exit***\n");
	printf("********************\n");
}
void game()
{
    
    
	char ret;
	char board[3][3] = {
    
     0 };
	//初始化棋盘
	Initboard(board, ROW, COL);
	//打印棋盘
	Displayboard(board, ROW, COL);

	//1.玩家用*表示
	// 2.电脑用#表示
	//判断输赢
	//1.玩家赢返回*
	//2.电脑赢返回#
	//3.继续游戏返回C
	//4.平局返回P
	while (1)
	{
    
    
		//玩家下棋
		PlayerMove(board, ROW, COL);
		Displayboard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
		//电脑下棋
		ComputMove(board, ROW, COL);
		Displayboard(board, ROW, COL);
		//判断输赢
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '*')
		printf("玩家赢\n");
	else if (ret == '#')
		printf("电脑赢\n");
	else
		printf("平局\n");
}
	
int main()
{
    
    
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
    
    
		menu();//游戏菜单
		printf("请输入:>");
		scanf("%d", &input);
		switch (input)//判断是否开始游戏
		{
    
    
		case 1:
			printf("游戏开始\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新输入\n");
			break;
		}
	} while (input);
	return 0;
}


game show

player wins

insert Yangtze River International image description here
insert image description here

computer wins

insert image description here
insert image description here
insert image description here

draw

insert image description here
insert image description here
insert image description here

evaluate

There are many deficiencies in this simple version of the game, the main reason is that Xiaobai's level is not enough, so he can only copy and write such a code.
Computer players can't have a specific way to play, but play randomly, which reduces the fun, and can't control the order of who plays first. I hope you can give me more advice

Guess you like

Origin blog.csdn.net/weixin_68201503/article/details/130586165