Want to become a game development guru? Let’s try the simple three-piece chess implementation first! ! ! (Detailed explanation of header files & source files)

I believe there are many friends who like to play games. Have you ever thought about implementing a game by yourself?
Today, let me take you through a simple implementation and explanation of the classic game "Three Pieces"!
Don’t blame me for being long-winded! ! !
Note: If you don’t understand anything in the program, remember to read the notes I prepared for you! ! !

Header file game.h

First of all, let’s introduce what a header file is:
Common ones like #include<stdio.h>, #include <stdlib.h>, #include<string.h> that we often quote at the beginning, these end with “.h” are so-called header file . In more detail, the above #include is followed by <>, that is, the header file of the library function is enclosed in angle brackets . The game.h file like the one I wrote below, which is a header file created by the programmer himself, is an ordinary header file . Header files created by programmers are often used to store 1. References to library function header files 2. Declarations of custom functions. In this way, in different files of the same project, just like my source files game.c and test.c below, quote #include "game.h" at the beginning , that is, the corresponding ordinary header file. (This avoids the need to repeatedly reference library function header files and repeatedly declare custom functions in each source file)

Attachment: Library functions are functions that come with the editor . After applying its header file, we can use the corresponding functions according to the format. It avoids the repetitive and meaningless work done by programmers to implement commonly used functions, and greatly improves programming efficiency.

#define _CRT_SECURE_NO_WARNINGS
//库函数头文件的引用
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//define定义的标识符常量
#define ROW 3
#define COL 3
//棋盘初始化函数的声明
void initial_board(char board[ROW][COL], int row, int col);
//棋盘打印函数的声明
void print_board(char board[ROW][COL],int row ,int col );
//判断输赢及状态函数的声明
char is_win(char board[ROW][COL], int row, int col);

Source file game.c

Let’s introduce what a source file is:
In the process of developing software, we need to save the written code to a file, so that the code will not be lost, can be found by the compiler, and can finally become an executable file. This kind of file used to save code is called a source file . In other words, the source file is used to store the main program and the specific implementation of the function .
We learned above that the function declaration is placed in the header file (.h), so where is the actual specific implementation of the function? Yes, in the corresponding source file. The source file can store the specific implementation of the function (that is, the specific implementation of the custom function in the main program).
Attachment: C language files have specific suffixes to facilitate recognition by the compiler and understanding by programmers. For example: the suffix of C language source files is .c , and the suffix of C language header files is .h

#include"game.h"
//棋盘初始化函数的实现
int a = 0;
void initial_board(char board[ROW][COL], int row, int col)
{
    
    
    //用for循环给二维数组每个元素初始化为空格
	int i = 0;
	for (i=0;i<row;i++)
	{
    
    
		int j = 0;
		for (j=0;j<col;j++)
		{
    
    
			board[i][j] = ' ';
		}
	}
}
//棋盘打印函数的实现
void print_board(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");
		for (j = 0; j < col; j++)
		{
    
    
		    //控制打印分割行
			if (i < row - 1)
			{
    
    
				printf("----");
			}
		}
		printf("\n");
	}
}
//判断输赢及状态函数的实现
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][0] != ' ')
		{
    
    
			return board[i][0];
		}
	}
	//列
	int j = 0;
	for (j = 0; j < col; j++)
	{
    
    
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ')
		{
    
    
			return board[0][j];
		}
	}
	//斜
	if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') || (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' '))
	{
    
    
		return board[1][1];
	}
	//判断是平局还是继续
	int h = 0;
	//继续
	for (h = 0; h < row; h++)
	{
    
    
		int j = 0;
		for (j = 0; j < col; j++)
		{
    
    
			if (board[h][j] == ' ')
			{
    
    
				return 'c';
			}
		}
	}
	//平局
	return 'p';
}

Source file test.c

If the header file (game.h) and the source file (game.c) that stores the specific implementation of the function are the leaves of the branches, then the source file that stores the main body of the program is the trunk. This source file stores the main body of the program, that is, the overall idea of ​​the program and the basic implementation process of the entire program.

#include"game.h"
//打印菜单函数的实现
void menu()
{
    
    
	printf("**************************\n");
	printf("****  0.exit  1.play  ****\n");
	printf("**************************\n");
}
//game函数的实现
void game()
{
    
    
	//创建数组
	char board[ROW][COL];
	//初始化棋盘
	initial_board(board,ROW,COL);
	//打印(初始)棋盘
	print_board(board, ROW, COL);
	//开始下棋
	while (1)
	{
    
    
	    //玩家下棋
		while (1)
		{
    
    
			int a = 0;
			int b = 0;
			printf("请输入落子坐标,并中间用空格隔开:");
			scanf("%d %d", &a, &b);
			if ((a>=1&&a<=ROW)&&(b>=1&&b<=COL)&&(board[a - 1][b - 1] == ' '))
			{
    
    
				board[a-1][b-1] = '*';
				break;
			}
			else
			{
    
    
				printf("落子不符合条件,请正确下棋");
			}
		}
		//打印棋盘
		printf("玩家下棋\n");
		print_board(board, ROW, COL);
        //判断输赢与状态 并返回值相应的值
		char sult = is_win(board, ROW, COL);
		//根据返回值做出调整
		if (sult == '*')
		{
    
    
			printf("玩家赢\n");
			break;
		}
		if (sult == 'p')
		{
    
    
			printf("平局\n");
			break;
		}
		else
		{
    
    
			;
		}
	    //电脑下棋
		while (1)
		{
    
    
		    //产生两个随机值并%ROW(COL)以产生0~ROW(COL)的随机值
			int ret1 = rand() % ROW;
			int ret2 = rand() % COL;
			if (board[ret1][ret2] == ' ')
			{
    
    
				board[ret1][ret2] = '#';
				break;
			}
		}
		//打印棋盘
		printf("电脑下棋\n");
		print_board(board, ROW, COL);
		//判断输赢与状态 并返回值相应的值
		char sul = is_win(board, ROW, COL);
		//根据返回值做出调整
		if (sul == '#')
		{
    
    
			printf("电脑赢\n");
			break;
		}
		if (sul == 'p')
		{
    
    
			printf("平局\n");
		}
		else
		{
    
    
			;
		}

	}
}

//定义了一个全局变量n
int n = 0;

int main()
{
    
    
     
	srand((unsigned int)time(NULL));
	do
	{
    
    
	    //调用菜单打印函数
		menu();
		printf("请输入:>");
		//让玩家根据菜单做出选择(用键盘输入值)
		scanf("%d", &n);
		switch (n)
		{
    
    
		  case 0:
		  {
    
    
			  printf("退出游戏\n");
			  break;
		  }
		  case 1:
		  {
    
    
			  printf("游戏开始:\n");
			  //调用game函数
			  game();
			  break;
		  }
		  default:
		  {
    
    
			  printf("输入错误,请重新输入\n");
			  break;
		  }

		}

	}while(n);

	return 0;
}

Finally, if this article helped you, can you please click like
it~~~Please, this is really important to me! ! !

Guess you like

Origin blog.csdn.net/qq_56847032/article/details/116111107