[C Language] Project Combat - Fast 0-Basic Getting Started with Backgammon Game (with source code included)

insert image description here

Junxi_'s personal homepage

Be diligent and encourage the years to wait for no one

C/C++ game development



If you are an old fan who has been following since now, you may be a little confused "how old are you?" Without further ado, let's get started!

foreword

The content brought today is the specific implementation of the backgammon project. In fact, we have implemented the backgammon last time, and the
link is as follows:
[C Language] Detailed explanation of the backgammon (the kind of Baojiabaohui)
But, last time our backgammon actually still has a lot of places that need to be optimized. Let’s optimize it today and change it to backgammon.

1. The source code of the last game

  • I won’t repeat the things I talked about last time. For details, please refer to the link above. Here, I only paste the source code of the last time so that everyone can better understand the content later.
  • The following is the source code in each specific file of Sanziqi, you need to get it yourself

1.game.h (header file)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define Col 3
#define Row 3
void InitBoard(char board[Row][Col], int row, int col);//初始化棋盘
void PrintBoard(char board[Row][Col], int row, int col);//打印棋盘
void PlayBoard(char board[Row][Col], int row, int col);//玩游戏
void ComputerBoard(char board[Row][Col], int row, int col);//电脑下
int IsFull(char board[Row][Col], int row, int col);//棋盘是否已满
int Is_Win(char board[Row][Col], int row, int col);//判断谁赢

2.game.c (the file that defines the function)

#include"game.h"
void InitBoard(char board[Row][Col], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
    
    
		for (j =0; j < col; j++)
		{
    
    
			board[i][j] = ' ';

		}
	}
}


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

void PlayBoard(char board[Row][Col], int row, int col)
{
    
    
	int x, y = 0;
	while (1)
	{
    
    
		printf("玩家下棋>:\n");
		printf("请输入要下的坐标,中间用空格隔开\n");
		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 ComputerBoard(char board[Row][Col], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	printf("电脑下棋:>\n");
	while(1)
	{
    
    
		x = rand() % row;//生成0-row-1的数
		y = rand() % col;//生成0-col-1的数
		
		if (board[x][y] == ' ')
		{
    
    
			board[x][y] = '#';
			break;
		}

	}



}
int IsFull(char board[Row][Col], int row, int col)
{
    
    
	int i ,j;
	for (i = 0; i < row; i++)
	{
    
    
		for (j = 0; j < col; j++)
		{
    
    
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}


int Is_Win(char board[Row][Col], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	//竖着
	if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][0] !=' ')
	{
    
    
		return board[x][0];
	}
	//横着
	if (board[0][y] == board[1][y] && board[1][y] == board[2][y] && board[0][y] != ' ')
	{
    
    
		return board[0][y];
	}
	//对角线
	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.test.c (main file)

#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);
	PrintBoard(board,Row, Col);
	char ret = 0;
	while (1)
	{
    
    
		PlayBoard(board, Row, Col);
		PrintBoard(board, Row, Col);
		ret = Is_Win(board, Row, Col);
		if (ret != 'C')
		break; 
		
		ComputerBoard(board, Row, Col);
		PrintBoard(board, Row, Col);
		ret = Is_Win(board, Row, Col);
		if (ret != 'C')
		break;
	}
	if (ret == '*')
		printf("玩家赢\n");
	if (ret == '#')
		printf("电脑赢\n");
	if (ret == 'Q')
		printf("平局\n");
}
int main()
{
    
    
	srand((unsigned int)time(NULL));
	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;
}
  • Look at the game effect

insert image description here

Two. Backgammon

1. Board size

  • The size of the three-bang board is 3*3, which is obviously not enough for backgammon. Now the Row and Col we defined earlier will play a role. When we want to change the size of the board, we only need to change their values!
#define Col 10
#define Row 10

insert image description here

  • The effect is as shown in the figure, if you want to change it to be bigger, of course you can
  • 15*15 chessboard
    insert image description here
  • This step is complete

2. Determine the winning and losing conditions

  • Since we are backgammon now, the conditions for judging the outcome will naturally have to change. Let's first review the function of judging the outcome in three-moon chess
int IsFull(char board[Row][Col], int row, int col)
{
    
    
	int i ,j;
	for (i = 0; i < row; i++)
	{
    
    
		for (j = 0; j < col; j++)
		{
    
    
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}


int Is_Win(char board[Row][Col], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	//竖着
	if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][0] !=' ')
	{
    
    
		return board[x][0];
	}
	//横着
	if (board[0][y] == board[1][y] && board[1][y] == board[2][y] && board[0][y] != ' ')
	{
    
    
		return board[0][y];
	}
	//对角线
	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';

}
  • First of all, our judgment has to be changed from three sons to five sons
  • Secondly, we found that the above code is not flexible, pay attention here:
//竖着
	if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][0] !=' ')
	{
    
    
		return board[x][0];
	}
	//横着
	if (board[0][y] == board[1][y] && board[1][y] == board[2][y] && board[0][y] != ' ')
	{
    
    
		return board[0][y];
	}
	//对角线
	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];
  • In the middle, we found that its judgment method is directly linked to the chessboard and can only be used on a 3*3 chessboard. The most obvious place is the diagonal. The judgment method of the above code is to directly judge whether the elements stored in (1,1)(2,2)(3,3) or (1,3)(2,2)(3,1) are equal. Once we change the size of the chessboard, we cannot correctly determine whether we win or lose. For example:
    insert image description here
  • At this point we have won, but the program did not judge us to win
  • The improved code is as follows:
int Is_Win(char board[Row][Col], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	//竖着
	if (j < col - 4)//防止越界
	if (board[i][j] == board[i][j + 1] && board[i][j] ==board[i][j + 2]&& board[i][j] == board[i][j + 3] && board[i][j]== board[i][j + 4])
			return board[i][j];

	
	//横着
	if (i < row - 4)
	    if(board[i][j] == board[i][j + 1] && board[i][j] ==board[i][j + 2]&& board[i][j] == board[i][j + 3] && board[i][j]== board[i][j + 4])
	     return board[i][j];
	//左对角线连成五子
	if (i < row - 4 && j < col - 4)
		if (board[i][j] ==board[i + 1][j + 1] && board[i][j] ==board[i + 2][j + 2]&& board[i][j] == board[i + 3][j + 3] && board[i][j] == board[i + 4][j + 4])
			return board[i][j];
	// 右对角线连成五子 
	if (i < row - 4 && j > 4)
		if (board[i][j] == board[i + 1][j - 1] &&board[i][j] ==board[i + 2][j - 2]&& board[i][j] == board[i + 3][j - 3] && board[i][j] == board[i + 4][j - 4])
			return board[i][j];
	
	if (IsFull(board, row, col) == 1)
	{
    
    
		return 'Q';
	}
	return 'C';

}
  • Turn three into five, and at the same time increase the flexibility of the code, so that we can play backgammon and so on in the future.

3. Try it out

  • The effect of the game is shown in the figure
    insert image description here
  • Although the grid is a bit crude, the functions we need are perfectly realized!

4. Backgammon source code

  • Let me share the source code with you, so that you can try it out and correct any errors that may occur when you write it.

game.h (backgammon header file)

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define Col 10
#define Row 10
void InitBoard(char board[Row][Col], int row, int col);//初始化棋盘
void PrintBoard(char board[Row][Col], int row, int col);//打印棋盘
void PlayBoard(char board[Row][Col], int row, int col);//玩游戏
void ComputerBoard(char board[Row][Col], int row, int col);//电脑下
int IsFull(char board[Row][Col], int row, int col);//棋盘是否已满
int Is_Win(char board[Row][Col], int row, int col);//判断谁赢


test.c (main function file for testing backgammon)

#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);
	PrintBoard(board, Row, Col);
	char ret = 0;
	while (1)
	{
    
    
		PlayBoard(board, Row, Col);
		PrintBoard(board, Row, Col);
		ret = Is_Win(board, Row, Col);
		if (ret != 'C')
			break;

		ComputerBoard(board, Row, Col);
		PrintBoard(board, Row, Col);
		ret = Is_Win(board, Row, Col);
		if (ret != 'C')
			break;
	}
	if (ret == '*')
		printf("玩家赢\n");
	if (ret == '#')
		printf("电脑赢\n");
	if (ret == 'Q')
		printf("平局\n");
}
int main()
{
    
    
	srand((unsigned int)time(NULL));
	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;
}


game.c (definition file of backgammon function)

#include"game.h"
void InitBoard(char board[Row][Col], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
    
    
		for (j = 0; j < col; j++)
		{
    
    
			board[i][j] = ' ';

		}
	}
}


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

void PlayBoard(char board[Row][Col], int row, int col)
{
    
    
	int x, y = 0;
	while (1)
	{
    
    
		printf("玩家下棋>:\n");
		printf("请输入要下的坐标,中间用空格隔开\n");
		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 ComputerBoard(char board[Row][Col], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	printf("电脑下棋:>\n");
	while (1)
	{
    
    
		x = rand() % row;//生成0-row-1的数
		y = rand() % col;//生成0-col-1的数

		if (board[x][y] == ' ')
		{
    
    
			board[x][y] = '#';
			break;
		}

	}



}
int IsFull(char board[Row][Col], int row, int col)
{
    
    
	int i, j;
	for (i = 0; i < row; i++)
	{
    
    
		for (j = 0; j < col; j++)
		{
    
    
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}


int Is_Win(char board[Row][Col], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	//竖着
	if (j < col - 4)//防止越界
	if (board[i][j] == board[i][j + 1] && board[i][j] ==board[i][j + 2]&& board[i][j] == board[i][j + 3] && board[i][j]== board[i][j + 4])
			return board[i][j];

	
	//横着
	if (i < row - 4)
	    if(board[i][j] == board[i][j + 1] && board[i][j] ==board[i][j + 2]&& board[i][j] == board[i][j + 3] && board[i][j]== board[i][j + 4])
	     return board[i][j];
	//左对角线连成五子
	if (i < row - 4 && j < col - 4)
		if (board[i][j] ==board[i + 1][j + 1] && board[i][j] ==board[i + 2][j + 2]&& board[i][j] == board[i + 3][j + 3] && board[i][j] == board[i + 4][j + 4])
			return board[i][j];
	// 右对角线连成五子 
	if (i < row - 4 && j > 4)
		if (board[i][j] == board[i + 1][j - 1] &&board[i][j] ==board[i + 2][j - 2]&& board[i][j] == board[i + 3][j - 3] && board[i][j] == board[i + 4][j - 4])
			return board[i][j];
	
	if (IsFull(board, row, col) == 1)
	{
    
    
		return 'Q';
	}
	return 'C';

}

Summarize

  • Combined with the detailed explanation of our last backgammon implementation, backgammon is actually not difficult. If you haven’t written your own backgammon last time, I really recommend you to try backgammon. After all, you can only truly understand it if you have written the code yourself.
  • The above is all the content of today. If you have any questions, please point them out in the comment area or private message me. I will reply as soon as I see it!

It is not easy for a new blogger to create. If you feel that the content of the article is helpful to you, you may wish to click on this new blogger before leaving. Your support is my motivation to update! ! !

(Ke Li asks you to support the blogger three times in a row!!! Click the comment below to like and collect to help Ke Li)
insert image description here

Guess you like

Origin blog.csdn.net/syf666250/article/details/131313907