Backgammon mini game (board size can be changed)

Hello everyone! After learning C language for so long, we all hope that we can make some achievements, and implementing some small games is undoubtedly the first choice that comes to mind.

Today I will introduce to you a small game of tic-tac-toe full of fun and challenge - backgammon. It's a simple yet thoughtful game to outwit your opponent by placing symbols on the board such that the first line of three of the same symbol is connected. Let's take a look at the rules, strategy and implementation of the game of Three Connect! 

Post the corresponding positions of each function here, so that you can find them easily

Table of contents

1. The rules of the game of backgammon

2. Realization of important functions of the backgammon game

1. Menu printing

2. Board initialization

3. Checkerboard printing

4. Players play chess

5. Computer chess

6. Judgment on whether to win

3. Entire code and game demo:


1. The rules of the game of backgammon

Backgammon, also known as tic-tac-toe, is a board game played by two players. The game board is a 3x3 matrix of squares, and each player takes turns placing their own symbols (usually "X" and "O") in the empty squares. The first to form a line of three of the same symbol (horizontal, vertical or diagonal) wins.

The following is a schematic diagram to help us better understand the rules of the game:


2. Realization of important functions of the backgammon game

1. We first print the game menu

void menu()//打印菜单的函数
{
	printf("*********************\n");
	printf("******* 1. play *****\n");
	printf("******* 0. exit *****\n");
	printf("*********************\n");
}

The effect is as follows:

2. Initialize the chessboard We can use a 3x3 two-dimensional array to represent the chessboard, and the initial values ​​are all set to spaces. You can define a function to create and initialize the chessboard

void initBoard(char arr[ROW][COL], int a, int b)//初始化棋盘的函数
{
	for (int i = 0; i < a; i++)
	{
		for (int j = 0; j < b; j++)
		{
			arr[i][j] = ' ';
		}
	}
}

3. Print the chessboard Define a function to print the current chessboard state (the code can change the size of the chessboard at will)

void printBoard(char arr[ROW][COL], int row,int col)//打印棋盘的函数
{
	
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf(" %c ", arr[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		for (int k = 0; k < col; k++)
		{
			printf("---");
			if (k < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
	}
}

4. Prompt the player to enter the position Define a function to prompt the player to enter the position to play chess, and at the same time check whether the input position is legal Define a function to check whether the position entered by the player is legal, that is, the values ​​of the row and column are both between 0 and 2 , and the position is not already occupied by another player

void Player(char arr[ROW][COL], int row, int col)
{
	while (1)
	{
           printf("玩家进行下棋,请输入棋子所处坐标 \n");
	    int x, y;
	    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] = 'o';
			break;
		}
		printf("该位置已被占用,请输入其他坐标\n");
		
	}
	else
	{
		printf("输入错误,请重新输入");
	}
}
}

5. The computer plays chess (because I am a beginner, I have not learned much about algorithms, I will use random numbers for the time being, and I will learn to use the miniMax algorithm to improve in the future), and it is also necessary to judge whether it has been occupied. 

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

6. Determine whether a player wins. Define a function to determine whether there are players in a line. We need to check whether the pieces on each row, column, and diagonal are the same, if there are identical pieces, the player wins, o means the player wins, x means the computer wins, c means a draw, and k means continue. It is more convenient to return the corresponding character

char Iswin(char arr[ROW][COL], int row, int col)//判断输赢的函数
{
	for (int i = 0; i < row; i++)//判断行
	{
		if (arr[i][0] == arr[i][1]&&arr[i][1]==arr[i][2] && arr[i][2] != ' ')
		{
			return arr[i][0];
		}
		
	}
	for (int j = 0; j < col; j++)//判断列
	{
		if (arr[0][j] == arr[1][j] && arr[1][j] == arr[2][j] && arr[2][j] != ' ')
		{
			return arr[0][j];
		}
	}
	if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[1][1] != ' ')
	{
		return arr[0][0];
	}
	if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[1][1] != ' ')
	{
		return arr[0][2];
	}
	if (isFull(arr,row,col))
	{
		return 'c';
	}
	return 'k';
}

3. Entire code and game demo:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 3
#define COL 3//定义行和列均为3

void menu()//打印菜单的函数
{
	printf("*********************\n");
	printf("******* 1. play *****\n");
	printf("******* 0. exit *****\n");
	printf("*********************\n");
}


void initBoard(char arr[ROW][COL], int a, int b)//初始化棋盘的函数
{
	for (int i = 0; i < a; i++)
	{
		for (int j = 0; j < b; j++)
		{
			arr[i][j] = ' ';
		}
	}
}


void printBoard(char arr[ROW][COL], int row,int col)//打印棋盘的函数
{
	
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			printf(" %c ", arr[i][j]);
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		for (int k = 0; k < col; k++)
		{
			printf("---");
			if (k < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
	}
}


void Player(char arr[ROW][COL], int row, int col)
{
	while (1)
	{
           printf("玩家进行下棋,请输入棋子所处坐标 \n");
	    int x, y;
	    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] = 'o';
			break;
		}
		printf("该位置已被占用,请输入其他坐标\n");
		
	}
	else
	{
		printf("输入错误,请重新输入");
	}
}
}


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


int isFull(char arr[ROW][COL], int row, int col)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (arr[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}


char Iswin(char arr[ROW][COL], int row, int col)//判断输赢的函数
{
	for (int i = 0; i < row; i++)//判断行
	{
		if (arr[i][0] == arr[i][1]&&arr[i][1]==arr[i][2] && arr[i][2] != ' ')
		{
			return arr[i][0];
		}
		
	}
	for (int j = 0; j < col; j++)//判断列
	{
		if (arr[0][j] == arr[1][j] && arr[1][j] == arr[2][j] && arr[2][j] != ' ')
		{
			return arr[0][j];
		}
	}
	if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[1][1] != ' ')
	{
		return arr[0][0];
	}
	if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[1][1] != ' ')
	{
		return arr[0][2];
	}
	if (isFull(arr,row,col))
	{
		return 'c';
	}
	return 'k';
}


void game()
{
	char win ;
	char board[ROW][COL]; //我们使用一个二维数组board来表示棋盘,每个格子可以存放一个字符,初始值为空格
	initBoard(board, ROW, COL);//initBoard函数用来初始化棋盘
	printBoard(board, ROW, COL);//printBoard函数用来打印棋盘
	//开始下棋
	while (1)
	{
		Player(board, ROW, COL);//玩家下棋
		printBoard(board, ROW, COL);
		win=Iswin(board, ROW, COL);//o表示玩家赢,x是电脑赢,c是平局,k是继续
		if (win != 'k')
		{
			break;
		}
		Computer(board, ROW, COL); //电脑下棋
		printBoard(board, ROW, COL);
		if (win != 'k')
		{
			break;
		}
	}
	if (win == 'o')
	{
		printf("玩家获胜\n");
	}
	else if (win == 'x')
	{
		printf("电脑获胜\n");
	}
	else
	{
		printf("平局\n");
	}
	
}






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


 The game of three chess is a simple but challenging game, it can not only help us relax, but also can exercise our thinking ability. Through the introduction of this article, we have learned about the rules and implementation methods of the three-bang game. I hope you can try to play this game and enjoy the fun and challenges!

Today's sharing is here first, thank you all! ! !

Guess you like

Origin blog.csdn.net/qq_74415153/article/details/131941389