Using C Language to Simple Realize Three Connect

1. How to design

First of all, we can use functions to divide the implementation of backgammon into different modules. We can think about it, a game must first have a directory for players to choose to play or not, that is, the menu interface; secondly, we must have On a chessboard, the next step is for the player to make a move, and then the computer makes a move. After each move, a winner or loser must be judged. The final result is the player wins, the computer wins or a draw.

2. Code implementation

1. Menu creation

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


void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));

	do
	{
		menu();
		printf("请选择:>");
		scanf_s("%d", &input);
		switch (input)
		{
		case 1:
			game();//游戏
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
}

2. Create a chessboard and initialize the chessboard

We mainly use a two-dimensional array of characters for the chessboard. We can define macros to make the size of the chessboard easy to control. The implementation process is as follows:

2.1. Define macros

#define ROW 3
#define COL 3
char board[3][3]={0}

2.2. Board initialization

Initially, each grid in the array is used to place spaces to initialize the board

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

2.3. Split the board

Since there are only spaces in the array during initialization, the chessboard looks blank at this time, so we need to divide the chessboard to make each of them more obvious:

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

3. Players play chess

We record the place where the player has downloaded as *, and the place where the computer has downloaded as #. For the convenience of the player, the coordinates of the normal array start from 0, but for the convenience of the player, the coordinates of the player have to start from 1

void player_move(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家下棋\n");
	while (1)
	{
		printf("请输入坐标:>");
		scanf_s("%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. Computer chess

The computer uses rand to randomly generate row and column coordinates, # represents the computer's position

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

5. Judge win or lose

We stipulate that if the player wins, it will return *, if the computer wins, it will return #, if it is a tie, it will return Q, if it continues, it will return C

Judging the victory condition is that all rows, columns, and diagonals have three identical

static int if_full(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 is_win(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][1];
		}
	}
	//判断列
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][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 (if_full(board, row, col) == 1)
	{
		return 'Q';
	}

	//继续
	return 'C';
}

Guess you like

Origin blog.csdn.net/yss233333/article/details/122648092