Take you to play the game of backgammon - [C language]

Table of contents

Foreword:

1. Printing of the menu

2. Implementation of the game function

2.1 Initialize the chessboard

2.2 Display the chessboard

2.3 Players play chess

2.4 Computer chess

2.5 Judging Win or Lose

2.6 Determine whether the board is full

3. All code

3.1 game.h

3.2  game.c

3.3 test.c


Foreword:

In order to realize the three chess, first we should write the code in modules, we are divided into 3 parts

1. test.c — test the game (main function) 2. game.c realize part of the code of the game 3. game.h function declaration, macro definition, etc.


1. Printing of the menu

Menu printing:

For the convenience of playing the game, we can set up a menu and print it every time we play the game, so as to facilitate the player to play the game experience. The implementation is as follows:

Player chooses "1" - play game       

Player selects "0" - exits the game       

The player chooses another number—prompt that the choice is wrong, and choose again



Implement the pick number to play game:

1. How to play one game after another, and quit the game if you don’t want to play?

We can use the do-while loop to set the loop to determine the value of the input, so that we can exit the game only when the input is 0


2. How to choose 1 to play the game, choose 0 to exit the game, and choose other numbers to prompt the wrong choice ?

We can use switch-case statement inside do-while loop


code show as below:


2. Implementation of the game function

2.1 Initialize the chessboard

What we write here is the code of three-dimensional chess, three-row three-column, we can use two-dimensional array to print

We can traverse this two-dimensional array and initialize the 9 elements in it to spaces

code:

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.2 Display the chessboard

Since we initialize each element of the above two-dimensional array to a space, if we just print it out for the player to play the game, the experience will be very bad, so we will use the following methods to optimize our board display for the convenience of the player Play

In the blue rectangle above: 

We can group three spaces (data in the middle, spaces on both sides) and the vertical bars behind it into one group, and there are three groups in this line

Note: Our last group does not have a vertical bar (|), so we need to control it separately

In the yellow rectangle box above:

We group three horizontal lines and one vertical line into 1 group, and one line is also three groups, and the last line is the same 

 Code:

Summary: Through the above analysis, we can see that for each line of chessboard printed, we actually have two lines of checkerboard format control, so we can regard the blue and yellow boxes as a group, there are three groups in total, and the last group Do not print horizontal lines

void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		//1. 打印数据
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if(j<col-1)
				printf("|");
		}
		printf("\n");
		//2. 打印分割线
		if (i < row - 1)
		{
			//printf("---|---|---\n");
			int j = 0;
			for (j = 0; j < col; j++)
			{
				printf("---");
				if(j<col-1)
					printf("|");
			}
			printf("\n");
		}
	}
}

2.3 Players play chess

A two-dimensional array with three rows and three columns. The subscript range of the row and column is 0~2. The player may not know the nature of the array, and the subscript of the array may exceed the boundary. At this time, we need to input the horizontal and vertical coordinates of the player. decrement by 1

void PlayerMove(char board[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 (board[x-1][y-1] == ' ')//可以落子
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else//不能落子
			{
				printf("坐标被占有,不能落子,重新输入坐标\n");
			}
		}
		else//非法
		{
			printf("坐标非法,重新输入\n");
		}
	}
}

2.4 Computer chess

When playing chess with a computer, we can use the function of generating random numbers, so that the generated random numbers % row and % column are a random two-dimensional array subscript

The function usage of generating random numbers was introduced in my previous blog. If you are interested, you can learn it!

void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;//0~row-1
	int y = 0;//0~col-1
	
	printf("电脑下棋:>\n");

	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

2.5 Judging Win or Lose

This function needs to realize the following 4 situations

1. The player wins

2. The computer wins

3. Draw

4. If none of the above conditions are met, then continue

Three Cases of Winning

1. Three identical symbols appear in each row 2. Three identical symbols appear in each column 3. Three identical symbols appear in a diagonal


draw:

Implement a function to judge whether the game is a draw, and call it in the function of judging whether the game is won or lost

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][0] != ' ')
		{
			return board[i][0];
		}
	}
	//列
	for (i = 0; i < col; 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[1][1];
	if (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 'Q';
	}
	//继续
	return 'C';
}

2.6 Determine whether the board is full

Traverse the array to see if there is an element or a space, if there is, return 0, if not, it means the board is full, return 1

Note: The number returned here is returned to the conditional judgment statement of the judgment win or lose function

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


3. All code

3.1 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);


//判断输赢
//玩家赢 - '*'
//电脑赢 - '#'
//平局  - 'Q'
//继续  - 'C'

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

3.2  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++)
	{
		//1. 打印数据
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if(j<col-1)
				printf("|");
		}
		printf("\n");
		//2. 打印分割线
		if (i < row - 1)
		{
			//printf("---|---|---\n");
			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 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 (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)
{
	int x = 0;//0~row-1
	int y = 0;//0~col-1
	
	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;
	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 (i = 0; i < col; 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[1][1];
	if (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 'Q';
	}
	//继续
	return 'C';
}

3.3 test.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);//1 0 4
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择!\n");
			break;
		}
	} while (input);

	return 0;
}


If you think the article is good, I look forward to your one-click triple link. Your encouragement is the source of motivation for my creation. Let us work together and see you at the top! ! ! 

Guess you like

Origin blog.csdn.net/qq_58286439/article/details/130612158