Realization of Duoziqi in C language

There are a few steps in the realization of Duo Ziqi

(1) General design of the game

#include<stdio.h>
int main()
{
	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;
}

First, use the do while() statement to realize the repeated use of the game, and then use the Switch function to build the game start interface and use it with the menu.

(2) Design of game menu

void menu()
{
	printf("********************************\n");
	printf("***** 1、进入游戏 **************\n");
	printf("***** 0、退出游戏 **************\n");
	printf("********************************\n");
}

The game menu still uses the printf() function to simply realize the menu function.

(3) Game design

Since the chessboard is a small one in line with the meaning of two-dimensional space, it is realized by using a two-dimensional array. First, define the array as a two-dimensional array.

int arr[ROW][COL] = {0} ;

ROW and COL use the way of macro definition, just to be able to achieve multi-ziqi, not just limited to three-moon or four-moon chess and so on.

#define ROW 3
#define COL 3

Here we first define it as three to explain, and later you can change the number at will.

The chessboard is first of all a whiteboard. We have to initialize the array first, and initialize it to a space just to leave room for playing chess.

First build an initialization function initialize(arr, ROW, COL). Then continue to build the function of this function.

void initialize(char arr[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			arr[i][j] = ' ';
		}
	}
}

After the initialization is complete, there are currently only 9 more spaces, and it is necessary to print out the initial appearance of the chessboard, and build a function to print the chessboard.

void copychec(char arr[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 ", arr[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");
		}
	}
}

After the printing of the chessboard is completed, the chess game will officially start.

First the player plays chess, build the function.

Because we have learned some computer knowledge, we know that the array starts from subscript 0, and non-computer people probably don’t know. Therefore, the range of our players’ chess, the horizontal and vertical coordinates should be greater than or equal to 1, and less than or equal to ROW or COL (It is the number of chess pieces that you set at the beginning, but we wrote it with three chess pieces at the beginning, so our range is greater than or equal to 1 and less than or equal to 3).

void playmove(char arr[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 (arr[x - 1][y - 1] == ' ')
			{
				arr[x - 1][y - 1] = '*' ;
				break;
			}
			else
			{
				printf("位置被占,请重新输入\n");
			}
		}
		else
		{
			printf("输入不合法,请重新输入\n");
		}
	}
}

After playing chess, in order to facilitate the next step, we have to print out the chessboard at this time

Call the copycheck() function again.

Then it is the turn of the computer to play chess, which is also the construction function. Among them, we also use the rand() function to make the coordinates under the computer be random values. Before using the rand function, you need to add srand ((unsigned int )time(NULL)) to the main function; the header file names are: #include<stdlib.h> and #include<time.h>

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

It is also necessary to print the chessboard and call the copycheck() function.

--------------------------------------------------------------------------------

It is impossible for us to play chess with only one move, so we put the above in the while() function

At this point the game framework is simple to build

void game()
{
	char arr[ROW][COL] = { 0 };
	initialize(arr, ROW, COL);//数组初始化
	copychec(arr, ROW, COL);//打印棋盘

	while (1)
	{
		playmove(arr, ROW, COL);//玩家下棋
		copychec(arr, ROW, COL);//打印棋盘

		computemove(arr, ROW, COL);//电脑下棋
		copychec(arr, ROW, COL);//打印棋盘
	}
}

Of course there are winners and losers in the game.

It has to be judged after the player has finished playing and the computer has finished playing chess. So design a function to satisfy this function.

In the judgment, there are a total of four situations, "player wins, computer wins, draw, continue", which are represented by the return value "*, #, m, n" respectively. In the judgment process, we use counting to judge who wins , which also meets the changing requirements of Duo Ziqi, not limited to a certain number of chess pieces, so writing is more flexible. When judging the columns, we exchanged the loop positions of i and j, so that it can be judged column by column.

In the end, when judging the tie, we use the Inspect() function to check whether there are empty grids on the entire board, and return 0 if there is, and return 1 otherwise.

//返回的是‘*’玩家赢
//返回的是‘#’电脑赢
//返回的是‘m'平局
//返回的是‘n'继续
//

int Inspect(char arr[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 (arr[i][j] == ' ')
				return 0;
		
		}
	}
	return 1;
}

char Deter(char arr[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	//判断行
	for (i = 0; i < row ; i++)
	{
		int count = 0;
		for (j = 0; j < col - 1; j++)
		{
			if (arr[i][j] == arr[i][j + 1] && arr[i][j] != ' ')
			{
				count++;
			}
			if(count == row - 1)
				return arr[i][j];
		}
	}
	//判断列
	for (j = 0; j < col ; j++)
	{
		int count = 0;
		for (i = 0; i < row - 1; i++)
		{
			if (arr[i][j] == arr[i + 1][j] && arr[i][j] != ' ')
			{
				count++;
			}
			if(count == row -1)
				return arr[i][j];
		}
	}
	//判断对角线
		int countn = 0;
	for (i = 0; i < row - 1; i++)
	{
		//判断右对角线

		for (j = 0; j < col; j++)
		{
			if (i + j == row - 1 && arr[i][j] == arr[i + 1][j - 1] && arr[i][j] != ' ')
			{
				countn++;
			}
			if(countn == row - 1)
				return arr[i][j];
		}
	}
	//判断左对角线
	int count = 0;
	for (i = 0; i < row - 1; i++)
	{
		for (j = 0; j < col - 1; j++)
		{
			if (i == j && arr[i][j] == arr[i + 1][j + 1] && arr[i][j] != ' ')
			{
				count++;
			}
			if(count == row - 1)
				return arr[i][j];
		}
	}
	//平局
	if (Inspect(arr, ROW, COL) == 1)
	{
		return 'm';
	}
    //继续
	return 'n';
}

Finally, the function of the game was successfully designed.

void game()
{
	char arr[ROW][COL] = { 0 };
	initialize(arr, ROW, COL);//数组初始化
	copychec(arr, ROW, COL);//打印棋盘
	char net = 0;
	while (1)
	{
		playmove(arr, ROW, COL);//玩家下棋
		copychec(arr, ROW, COL);//打印棋盘
		net = Deter(arr, ROW, COL);//判断输赢
		if (net != 'n')
			break;

		computemove(arr, ROW, COL);//电脑下棋
		copychec(arr, ROW, COL);//打印棋盘
		net = Deter(arr, ROW, COL);//判断输赢
		if (net != 'n')
			break;
	}
		if (net == '*')
		{
			printf("玩家赢了\n");
		}
		else if (net == '#')
		{
			printf("电脑赢了\n");
		}
		else if (net == 'm')
		{
			printf("平局\n");
		
		}
	
}

The overall code of the game is:

#define ROW 3
#define COL 3
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
	printf("********************************\n");
	printf("***** 1、进入游戏 **************\n");
	printf("***** 0、退出游戏 **************\n");
	printf("********************************\n");
}

void initialize(char arr[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			arr[i][j] = ' ';
		}
	}
}

void copychec(char arr[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 ", arr[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 playmove(char arr[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 (arr[x - 1][y - 1] == ' ')
			{
				arr[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("位置被占,请重新输入\n");
			}
		}
		else
		{
			printf("输入不合法,请重新输入\n");
		}
	}
}

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


//返回的是‘*’玩家赢
//返回的是‘#’电脑赢
//返回的是‘m'平局
//返回的是‘n'继续
//

int Inspect(char arr[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 (arr[i][j] == ' ')
				return 0;

		}
	}
	return 1;
}

char Deter(char arr[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	//判断行
	for (i = 0; i < row; i++)
	{
		int count = 0;
		for (j = 0; j < col - 1; j++)
		{
			if (arr[i][j] == arr[i][j + 1] && arr[i][j] != ' ')
			{
				count++;
			}
			if (count == row - 1)
				return arr[i][j];
		}
	}
	//判断列
	for (j = 0; j < col; j++)
	{
		int count = 0;
		for (i = 0; i < row - 1; i++)
		{
			if (arr[i][j] == arr[i + 1][j] && arr[i][j] != ' ')
			{
				count++;
			}
			if (count == row - 1)
				return arr[i][j];
		}
	}
	//判断对角线
	int countn = 0;
	for (i = 0; i < row - 1; i++)
	{
		//判断右对角线

		for (j = 0; j < col; j++)
		{
			if (i + j == row - 1 && arr[i][j] == arr[i + 1][j - 1] && arr[i][j] != ' ')
			{
				countn++;
			}
			if (countn == row - 1)
				return arr[i][j];
		}
	}
	//判断左对角线
	int count = 0;
	for (i = 0; i < row - 1; i++)
	{
		for (j = 0; j < col - 1; j++)
		{
			if (i == j && arr[i][j] == arr[i + 1][j + 1] && arr[i][j] != ' ')
			{
				count++;
			}
			if (count == row - 1)
				return arr[i][j];
		}
	}
	//平局
	if (Inspect(arr, ROW, COL) == 1)
	{
		return 'm';
	}
	return 'n';
}

void game()
{
	char arr[ROW][COL] = { 0 };
	initialize(arr, ROW, COL);//数组初始化
	copychec(arr, ROW, COL);//打印棋盘
	char net = 0;
	while (1)
	{
		playmove(arr, ROW, COL);//玩家下棋
		copychec(arr, ROW, COL);//打印棋盘
		net = Deter(arr, ROW, COL);//判断输赢
		if (net != 'n')
			break;

		computemove(arr, ROW, COL);//电脑下棋
		copychec(arr, ROW, COL);//打印棋盘
		net = Deter(arr, ROW, COL);//判断输赢
		if (net != 'n')
			break;
	}
		if (net == '*')
		{
			printf("玩家赢了\n");
		}
		else if (net == '#')
		{
			printf("电脑赢了\n");
		}
		else if (net == 'm')
		{
			printf("平局\n");
		
		}
	
}



int main()
{
	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;
}

This is the end of the explanation of Duoziqi game this time, because my level is limited, the explanation is not very clear in some places, everyone is welcome to correct, thank you!

Guess you like

Origin blog.csdn.net/weixin_74967884/article/details/131342078