C language project - realizing three-piece chess (multi-piece chess)

  Hello everyone, my name is Doki sy. Today I will bring you a simple and fun C language game - Sanbianqi (Multi-bangqi). Sanbangqi is a small game that can systematically review the basic knowledge of C language. Project, I suggest that those who are new to C language must try it on their own. Today I will share with you the three-piece chess project I implemented. If you have any questions, please feel free to correct me and support me~~ Hehe.


  First of all, three-piece or multi-piece chess is an n*n chessboard, so before we write, we will think of the construction of a two-dimensional array. Through the two-dimensional array, we determine the rows and columns of the chessboard, and then determine a series of functions for the two-dimensional array. The dimensional array performs functions such as initialization and assignment, judging winning or losing, playing chess, displaying output, etc., thereby realizing the construction of three-piece or multi-piece chess.

  To implement this project, we can divide it into three files, namely test.c, game.c, and game.h, which can make our project clearer and better control the operation of the entire program. The test.c file stores the main framework part of the code; the game.c file mainly implements the running process of the game, including all function definitions related to the implementation of the game process; game.h contains macro definitions, the library function header files that need to be used, and Declaration of each function. The following code is implemented according to files and modules:

 main function body part

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

Explanation of srand((unsigned int)time(NULL))

  First, in the main function, srand((unsigned int)time(NULL)) initializes the random function seed. We take time as the random seed here. Since time is constantly changing, different random values ​​can be generated. After that, we will play chess on the computer . The deployment of random values ​​is to generate a time seed through the random value generator srand() and then use rand() to generate a random number. The unsigned int here is a forced conversion type, which means forcing the parameter seed to an unsigned integer type. Usually we use the return value of time(NULL) as the seed to initialize the starting value of rand().

 Explanation of the main function of the main structure

  First of all, we need to execute a series of choices before recirculating whether to play the game, etc., so we use do....while() loop, first print a menu menu(), and select whether to play the game. Here we use scanf Enter an integer to select, and use the switch branch statement to make a specific selection. If the input value is 1, the game will start and enter the game() function. If it is 0, the game will exit. If other values ​​are input, the loop selection will be re-entered.

Let’s start to implement the main body of the game:

  First define a character two-dimensional array board, in which the rows and columns are defined by macro replacement in the header file. Here I initialize the rows and columns to 3. We can also change them to 5, 10, etc.

#define ROW 3
#define COL 3

Implement the game in function blocks:

1. Use the InitBoard() function to initialize the chessboard (write the function definition in the header file, and all subsequent functions will be)

  Start initializing each element of the two-dimensional array as a space

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

2. Use the DisplayBoard() function to print the chessboard   (then the player and the computer can call this function to print the result every time they play chess)

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]);//注意为使数据居于中间,需在%c两边加空格
			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");
	}
}

  The table for initial printing is as follows: (This is a 3*3 table. If you need to modify it to 10*10, you only need to modify the columns and rows in the macro definition in the header file)

 

3. Use the PlayerMove() function to enable players to play chess

  The board[x - 1][y - 1] here is because the coordinates entered by the player will not follow the rules of array subscripts starting from 0 like programmers, but are rows and columns visible to the naked eye, so the computer will You need to subtract one from the x and y coordinates.

void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家下棋:\n");
	while (1)
	{
		printf("请输入下棋坐标(中间用空格分开)如(1 1)\n");
		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");
		}
	}
}

 4. Use ComputerMove() function to realize computer chess playing

  Here x = rand() % row means to randomly pick a random integer between 0~row-1 for x. For example, x=rand()%3 takes the remainder of 3, and the resulting x value is 0~2. Random integers between, the same applies to y.

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

 5. Use the IsWin() function to determine winning or losing

  This part of the code is very important for game implementation. We need to determine whether the rows and columns, as well as the main diagonal and sub-diagonal elements are equal, so as to determine who wins and who loses. Here we use a for loop and nested if statements to make the determination. , can judge any set number of rows and columns. We stipulate:

Player wins --- Return to '*'
Computer wins --- Return to '#'
Draw --- Return to 'Q'
Game continues - Return to 'C'

char IsWin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int n = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;//计数器
		int r = 0;
		for (n = 0; n < col; n++)
		{
			if (board[i][n] == '*')
			{
				j++;
			}
			if (board[i][n] == '#')
			{
				r++;
			}
		}
		if (j == row)
		{
			return '*';
		}
		if (r == row)
		{
			return '#';
		}
	}
	for (i = 0; i < row; i++)
	{
		//清零
		int r = 0;
		int j = 0;
		for (n = 0; n < col; n++)
		{
			if (board[n][i] == '*')
			{
				r++;
			}
			if (board[n][i] == '#')
			{
				j++;
			}
		}
		if (r == col)
		{
			return '*';
		}
		if (j == col)
		{
			return '#';
		}
	}
	//判断正对角线
	int r = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (n = 0; n < col; n++)
		{
			if (i == n)
			{
				if (board[i][n] == '*')
				{
					r++;
				}
				if (board[i][n] == '#')
				{
					j++;
				}
			}
		}
	}
	if (r == row)
	{
		return '*';
	}
	if (j == row)
	{
		return '#';
	}
	//判断反对角线
	r = 0;
	j = 0;
	for (i = 0; i < row; i++)
	{
		if (board[i][row - 1 - i] == '*')
		{
			r++;
		}
		if (board[i][row - 1 - i] == '#')
		{
			j++;
		}
	}
	if (r == row)
	{
		return '*';
	}
	if (j == row)
	{
		return '#';
	}

	//判断平局
	if (IsFull(board, row, col))
	{
		return 'Q';
	}

	return 'C';
}

6. Use the IsFull() function to determine the return value to determine whether there is a tie.

This function is nested in the judgment condition of the last if statement in the IsWin() function and is used to judge whether it is a tie. If it returns 1, it enters the if statement and returns "tie".

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;//如果遍历完整个二维数组没有空格,返回1
}

The file implementation code is as follows: 

test.c file

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
	printf("**************************************\n");
	printf("**********请选择是否玩游戏:**********\n");
	printf("*************1.开始游戏***************\n");
	printf("*************0.退出游戏***************\n");
	printf("**************************************\n");

}
void game()
{
	char board[ROW][COL];
	int ret = 0;
	InitBoard(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
	while (1)
	{
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//判断输赢
		ret= IsWin(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("玩家赢\n");
	}
	else if (ret == '#')
	{
		printf("电脑赢\n");
	}
	else
	{
		printf("平局\n");
	}
}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请选择:>\n");
		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 file

#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 < 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("请输入下棋坐标(中间用空格分开)如(1 1)\n");
		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");
	int x = 0;
	int y = 0;
	while (1)
	{
		x = rand() % row;
		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;
}
//玩家赢 - '*'
//电脑赢 - '#'
//平局 --- 'Q'
//游戏继续-'C'

char IsWin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int n = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;//计数器
		int r = 0;
		for (n = 0; n < col; n++)
		{
			if (board[i][n] == '*')
			{
				j++;
			}
			if (board[i][n] == '#')
			{
				r++;
			}
		}
		if (j == row)
		{
			return '*';
		}
		if (r == row)
		{
			return '#';
		}
	}
	for (i = 0; i < row; i++)
	{
		//清零
		int r = 0;
		int j = 0;
		for (n = 0; n < col; n++)
		{
			if (board[n][i] == '*')
			{
				r++;
			}
			if (board[n][i] == '#')
			{
				j++;
			}
		}
		if (r == col)
		{
			return '*';
		}
		if (j == col)
		{
			return '#';
		}
	}
	//判断正对角线
	int r = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (n = 0; n < col; n++)
		{
			if (i == n)
			{
				if (board[i][n] == '*')
				{
					r++;
				}
				if (board[i][n] == '#')
				{
					j++;
				}
			}
		}
	}
	if (r == row)
	{
		return '*';
	}
	if (j == row)
	{
		return '#';
	}
	//判断反对角线
	r = 0;
	j = 0;
	for (i = 0; i < row; i++)
	{
		if (board[i][row - 1 - i] == '*')
		{
			r++;
		}
		if (board[i][row - 1 - i] == '#')
		{
			j++;
		}
	}
	if (r == row)
	{
		return '*';
	}
	if (j == row)
	{
		return '#';
	}

	//判断平局
	if (IsFull(board, row, col))
	{
		return 'Q';
	}

	return 'C';
}

game.h file

#pragma once
#include<stdio.h>
#include<time.h>
#define ROW 10
#define COL 10
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 IsWin(char board[ROW][COL], int row, int col);

thank you all! Thank you for reading, gentlemen! I am a novice in programming. If there are any errors, you are welcome to leave a message in the comment area. You are welcome to correct me! ! !

 

 

Guess you like

Origin blog.csdn.net/m0_74475605/article/details/131926904