Write a simple backgammon game in C language

Today I learned a backgammon game taught by Brother Peng. After learning it, I summarized it myself. I would like to share it here to remind myself not to forget. Although it is very simple, it is still necessary to clarify the logic. After studying this game, I feel the importance of drawing pictures to clarify logic. There are many programs that cannot be written, and it feels like the logical relationship is not clear, so in the future study, it is necessary to draw more pictures and clarify the relationship, which is the most important thing. Well, let me share the picture I drew for the first time!

 Next step by step to create this game:

The first step is to create a new project:

 

 

 

 

 

 

 

 

 Now that part of the code is written, let’s first test whether the array can be printed normally, and whether the previous logic is correct. For simplicity, take a screenshot directly:

 

 

 After testing, the logic of functions such as initializing arrays and printing arrays is correct:

 Then I wrote the whole program:

//  下面是game.c文件的全部内容:
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

//初始化函数
void InitArr(char arr[ROW][COL], int row, int col) 
{
	int i = 0, j = 0;
	for (i = 0; i < row; i++) 
	{
		for (j = 0; j < col; j++) 
		{
			arr[i][j] = ' ';   // 数组中的每一个元素都初始化成  ‘  ’空格
		}
	}
}


// 打印数组
void Display(char arr[ROW][COL], int row, int col) 
{
	int i = 0;
	for (i = 0; i < row; i++) 
	{
		//printf(" %c | %c | %c \n", arr[i][0], arr[i][1], arr[i][2]);
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", arr[i][j]);
			if (j < col - 1)
				printf("|");  //如果不是最后一列就打印 |
		}
		printf("\n");

		if (i < row - 1) // 如果是倒数第二行以前就执行
		{
			//printf("---|---|---\n");
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
		
	
	}
}


// 玩家下棋
void WanJia(char arr[ROW][COL], int row, int col) 
{
	printf("玩家请下棋:\n");
	int x = 0, y = 0;  // 玩家输入的坐标
	while (1) 
	{
		printf("请输入坐标:");
		scanf("%d %d", &x, &y);
		printf("\n");
		if ((x >= 1 && x <= row) && (y >= 1 && y <= col))   // 检测用户输入的是不是  1到 row(3)
		{
			if (arr[x - 1][y - 1] = ' ')  // 如果用户输入的行列是空字符
			{
				arr[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("输入错误,请重新输入\n");
		}

	}
}


// 判断数组是不是都输入满了
int IsFull(char arr[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 ( arr[i][j] == ' ')
				return 0;
		}
	}
	return 1;  // 前面没有检测到不满,这里就是满了。
}


//  判断输赢

char IsWin(char arr[ROW][COL], int row, int col)
{
	//  判断每一行是不是都是一样的字符
	int i = 0;
	for (i = 0; i < row; i++)
	{
		//  如果这一行的字符都相等 且 不是空字符
		if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2] && arr[i][1] != ' ')  
			return arr[i][1];
	}


	// 判断每一列是不是都是一样的字符
	int j = 0;
	for (j = 0; j < col; j++)
	{
		if(arr[0][j] == arr[1][j] && arr[1][j] == arr[2][j] && arr[1][j] != ' ')
			return arr[1][j];
	}


	//  判断每个对角线是不是都是一样的字符
	if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[1][1] != ' ')
	{
		return arr[1][1];
	}
	if (arr[2][0] == arr[1][1] && arr[1][1] == arr[0][2] && arr[1][1] != ' ')
	{
		return arr[1][1];
	}



	// 判断是不是平局 

	//  判断是不是输入满了   如果满了就返回1   否则就返回0
	int ret = IsFull(arr, row, col);
	if (ret == 1)
		return 'Q';   // 如果已经填满了 就是平局(因为前面两个判断都是谁赢了)


	//  上面的谁?赢了  平局都没有符合条件的, 所以下面就剩下继续了
	return 'C';
}



//  电脑下棋
void DianNao(char arr[ROW][COL], int row, int col)
{
	printf("电脑开始下棋!\n");
	while (1)
	{
		int x = rand() % row;     //使用随机数  对行数 取余 都是不足一行的数
		int y = rand() % col;     // 在调用rand()  函数之前  一定要先设置  srand()
		if (arr[x][y] == ' ')
		{
			arr[x][y] = '#';
			break;     //  如果向数组里面写完了值,  就跳出循环
		}
	}

}




//***********************************************************************************

// 下面是game.h头文件的内容:


#pragma once

#include"stdio.h"
#include"stdlib.h"
#include"time.h"

//  声明常量
#define ROW 3   //  ROW  代表3行   以后想要改变行数从这里直接改
#define COL 3   //  COL  代表3列   以后想要改变列数从这里直接改


//  声明函数


//初始化数组
void InitArr(char arr[ROW][COL], int row, int col);

// 打印数组
void Display(char arr[ROW][COL], int row, int col);

// 玩家下棋
void WanJia(char arr[ROW][COL], int row, int col);

//  判断输赢
char IsWin(char arr[ROW][COL], int row, int col);


//  电脑下棋
void DianNao(char arr[ROW][COL], int row, int col);





//***********************************************************************************
// 下面是test.c的文件内容   主函数在这个文件里


#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void menu() 
{
	printf("****************************\n");
	printf("******  1、开始游戏  *******\n");
	printf("******  0、结束游戏  *******\n");
	printf("****************************\n");
}

void game() 
{
	// 1、创建数组
	char arr[ROW][COL];

	// 2、初始化数组
	InitArr(arr, ROW, COL);

	// 3、打印数组
	Display(arr, ROW, COL);

	// 创建接收结果的变量
	char ret = 0;
	while (1)
	{
		// 玩家下棋
		WanJia(arr,ROW, COL);
		// 打印数组
		Display(arr, ROW, COL);
		// 判断输赢
		ret = IsWin(arr, ROW, COL);
		if (ret != 'C')
			break;   // 如果返回结果不是C就跳出循环
		// 电脑下棋
		DianNao(arr, ROW, COL);
		// 打印数组
		Display(arr, ROW, COL);
		// 判断输赢
		ret = IsWin(arr, ROW, COL);
		if (ret != 'C')
			break;   // 如果返回结果不是C就跳出循环
	}
	if (ret == '*')
		printf("********************玩家赢!*********************\n");
	if (ret == '#')
		printf("####################电脑赢######################\n");
	if (ret == 'Q')
		printf("QQQQQQQQQQQQQQQQQ    平局     QQQQQQQQQQQQQQQQQ\n");
	// 打印数组
	Display(arr, ROW, COL);
}

int main() 
{
	int input = 0;
	srand((unsigned int)time(NULL));     //  先设置了srand()   ,后面才能使用随机数函数rand().
	do
	{
		menu();
		printf("请输入你的选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("开始游戏!\n");
			game();
			break;

		case 0:
			printf("结束游戏!\n");
			break;

		default:
			printf("输入错误,重新输入!\n");
			break;
		}

	} while (input);

}



Write three files according to the above code, and you can write a three-character chess game. Let's take a look at the effect after running!

 Well, this is so much code I wrote this morning. Hereby record it to prevent yourself from forgetting the process.

Guess you like

Origin blog.csdn.net/xingyuncao520025/article/details/131120339