Mini-games ———————-3-bang (nanny-level teaching)

Today I will bring you a small game, a simple implementation of backgammon . Following this article, you can also make your own backgammon.

                                                        The overall idea of ​​the game

1.text.c———used to test the game

2.game.c———The realization of game functions

3.game.h———declaration of game functions

'*' represents the chess played by the player (also means win), '#' represents the chess played by the computer (also means win), 'Q' means draw, 'C' means continue,

text.c——we are used to test the game, we will play it first when we play the game, so we will choose to use the do while loop to solve it, when we enter for the first time, there should be a menu to ask us whether to play the game, which uses our menu function, and then we can choose whether to play the game according to our choice.

 

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;
}

We define a temporary variable input of type int, and then we choose whether to play the game according to the difference of the input. When we choose 1, we play the game, and when we choose 0, we choose to exit the game. If it is not these two numbers, we choose again.

  • This is for us to implement the menu function, we only need to print the menu, so we don’t need to pass parameters and return values, just printf directly
void menu()
{
	printf("*************************\n");
	printf("********  1.play  *******\n");
	printf("********  0.exit  *******\n");
	printf("*************************\n");
}

  • In this way, we have completed the printing of the menu. When we choose 1, we enter the switch selection and enter the game function.
  • void game()
    {
    	char board[ROW][COL] = { 0 };
    	InitBoard(board, ROW, COL);
    	DisplayBoard(board, ROW, COL);
    	char ret = 0;
    	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);
    		ret = IsWin(board, ROW, COL);
    		if (ret != 'C')
    			break;
    
    	}
    	if (ret == '*')
    		printf("玩家赢\n");
    	else if (ret == '#')
    		printf("电脑赢\n");
    	else
    		printf("和局\n");
    }

    We first define a two-dimensional array with 3 rows and 3 columns. For the readability of our code, we can change the row to ROW, and the column corresponds to COL. This is what we can #define in game.h

  •  game. h

#pragma once

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

#define ROW 3
#define COL 3

In game.h, we put all the library functions in game.h, and then we use #define to define the number of ROW and COL, so that when we want to change the chessboard to 5x5, we only need to change the value of game.h to realize all the changes.

Back in text.c, after we define the two-dimensional array of char type, we need to initialize it, and initialize all the two-dimensional arrays to ' '. At this time, we need to declare the InitBoard function in game.h, and pass in all our two-dimensional arrays, rows, and columns.

 In game.h we can declare like this

void InitBoard(char board[ROW][COL], int row,int col);

Then the implementation of our function is in game.c,

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

We think, after initializing the chessboard, if we want to display the chessboard, we need to use our DisPlayBoard function, the goal is to realize this picture.

 

 We found that the first line is composed of spaces and | , our row is 3, but our last column does not have |, we can implement it separately, and the second line is composed of ___ and |, and the last column is also without | . At this time, the DisPlayBoard function can be realized, and the code is as follows.

 

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

Of course, we also need to declare this function in game.h. When we finish the function of displaying the chessboard, we can implement the function of the user to move. We can define a function of PlayerMove and a function of computer movement (ComputerMove). We declare it in game.h, and then we can implement it in game.c


void PlayerMove(char board[ROW][COL], int row, int col)
{
	int pos_x = 0;
	int pos_y = 0;
	printf("玩家输入:>");
	while (1)
	{
		printf("请输入棋盘坐标\n");
		scanf("%d %d", &pos_x, &pos_y);
		if (pos_x >= 1 && pos_x <= row && pos_y >= 1 && pos_y <= col)
		{
			if (board[pos_x-1][pos_y-1] == ' ')
			{
				board[pos_x-1][pos_y-1] = '*';
				break;
			}
			else
			{
				printf("pos is occupied\n");
			}
		}
		else
		{
			printf("pos is error\n");

		}
	}
}

 We define pos_x and pos_y to indicate the position to go, and the chess piece we placed must be on the chessboard. This is the first if judgment. In the second if judgment, if the position is " ", it means that no one plays at that position. We can put "*" in that position, and then pick out the loop. But please note that the position I placed is [pos_x-1][pos_y], this is because our array subscript starts from 0, and when the user plays, they think that row 1 and column 1 are our row 0 and column 0, so we all need -1.

void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("电脑输入:>\n");
	while (1)
	{
		int pos_x = rand() % row;//0-row-1
		int pos_y = rand() % col;//0-col-1
		if (board[pos_x][pos_y] == ' ')
			board[pos_x][pos_y] = '#';
		break;
	}
}

 Of course, the chess pieces played by the computer must be placed on the chessboard, so we use %row, which means the position of 0-row-1. If we want to place chess pieces randomly on the chessboard, we need to use the rand function. The rand function needs to use the srand function . We can put srand in the main function .

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

This represents the timestamp. We show that the time in our life is always going, so that we can achieve random placement. We use the type of time in the srand function, we set it to empty, and the type is forced to (unsigned int). Note that our srand should be placed before the while function, not in the while function. We can understand it this way. of the definition .

Then there is our IsWin function. We pass the board, row, and col as parameters. We want to implement the IsWin function. The old method requires us to declare it in game.h and implement the function in game.c

char IsWin(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][1] != ' ')
			return board[i][0];//返回谁赢  '*' '#' 'Q' 'C'
	}
	for (i = 0; i < row; 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[i][i];
	if (board[3][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
		return board[1][1];
	if ((IsFull(board, row, col)) == 1)
		return 'Q';

		return 'C';
}

 We define an int type variable i, and then we use a for loop to enter the first if judgment. If the three consecutive ones are equal, and they are not equal to ' ', we can return to board【i】【0】, which means that whoever has three in a row wins. The second if judgment is to judge whether the columns are equal, and the third and fourth are to judge whether the diagonal diagonals are connected.

  • When the board is full, we return 'Q', which means draw, and if the above three are not satisfied, it means continue to play.
  • The IsFull function requires us to traverse it, using a for loop, the code is as follows,
    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;
    		}
    	}
    	//返回为1  if才会进去
    	return 1;
    }

    If none of our 3 rows and 3 columns has a ' ', it means it is full and returns 1, if one is equal to ' ', we return 0, and our IsFull function judges whether the board is empty based on our return value.

  •  Finally, attach the code of game.c
#define _CRT_SECURE_NO_WARNINGS 1

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

void PlayerMove(char board[ROW][COL], int row, int col)
{
	int pos_x = 0;
	int pos_y = 0;
	printf("玩家输入:>");
	while (1)
	{
		printf("请输入棋盘坐标\n");
		scanf("%d %d", &pos_x, &pos_y);
		if (pos_x >= 1 && pos_x <= row && pos_y >= 1 && pos_y <= col)
		{
			if (board[pos_x-1][pos_y-1] == ' ')
			{
				board[pos_x-1][pos_y-1] = '*';
				break;
			}
			else
			{
				printf("pos is occupied\n");
			}
		}
		else
		{
			printf("pos is error\n");

		}
	}
}

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;
		}
	}
	//返回为1  if才会进去
	return 1;
}

void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("电脑输入:>\n");
	while (1)
	{
		int pos_x = rand() % row;//0-row-1
		int pos_y = rand() % col;//0-col-1
		if (board[pos_x][pos_y] == ' ')
			board[pos_x][pos_y] = '#';
		break;
	}
}

char IsWin(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][1] != ' ')
			return board[i][0];//返回谁赢  '*' '#' 'Q' 'C'
	}
	for (i = 0; i < row; 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[i][i];
	if (board[3][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
		return board[1][1];
	if ((IsFull(board, row, col)) == 1)
		return 'Q';

		return 'C';
}
  • code of game.h
#pragma once

#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 ComputerMove(char board[ROW][COL], int row, int col);
char IsWin(char board[ROW][COL], int row, int col);
  • The code of text.c  
#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"


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

void game()
{
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
	char ret = 0;
	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);
		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:
				game();
				break;
			case 0:
				printf("退出成功\n");
				break;
			default:
				printf("选择错误,重新选择:>\n");
				break;

		}

	} while (input);

	return 0;
}

Finally: In this way, we have achieved a simple backgammon. Of course, we also have problems. When our board becomes 10x10, our IsWin judgment has problems, and we can only judge 3 backgammon. If you are interested, you can change the code appropriately to make it into multi-momentum. 

Guess you like

Origin blog.csdn.net/m0_72165281/article/details/130648167