"Game" to write 2048 mini games

We can search for 2048 in the WeChat applet and play a few games. After experiencing a few games, it is not difficult to find that the logic of the game is actually very simple.
I drew a mind map of the general logic that I thought of, and then used programming to implement it step by step. Of course, I also optimized my own code by referring to the excellent logic of others.
Insert picture description here1. Menu
There are only three pieces of information in the menu: start the game, exit the game, and operation instructions. Use switch() in the main function to list the three options corresponding to the three information of the above menu for the player to input.

void  menu()//菜单
{
	printf("************************\n");
	printf("******** 1.play ********\n");
	printf("******** 2.exit ********\n");
	printf("Control by:w/s/a/d or W/S/A/D\n");
	printf("************************\n");
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));

	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 2:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
	return 0;
}

2. The
interface of building a grid game looks like this (as shown in the figure below). This is a 4X4 dial. What we can think of is that we can create a four-row and four-column array a[4][4] to fill in the numbers. .
Insert picture description here
Of course, the border of the dial should also be printed out. In order to make the interface more beautiful and refreshing, there are a total of 16 small grids, and the space of each small grid should be as large as possible. Insert picture description here
The implementation scheme is shown in the figure above, each small square is composed of three vertical lines, and the code is as follows:

void board(void) //4x4的图格
{
	int i = 0;
	int j = 0;
	for (i = 0; i < 4; ++i)
	{          
		for (j = 0; j < 4; ++j)  
			printf("|    ");
		printf("|\n");

		for (j = 0; j < 4; ++j) 
		{ 
			if (a[i][j] == 0)
				printf("|    ");
			else
				printf("|%4d", a[i][j]);
		}
		printf("|\n");

		for (j = 0; j < 4; ++j)    
			printf("|____");
		printf("|\n");
	}
}

3. Initialize the array to
be continued

Guess you like

Origin blog.csdn.net/NanlinW/article/details/90262729