“This article has participated in the good writing call, click to see:Back end, big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

This article is to use C language to do a small box pushing game, it is relatively simple to achieve, and everyone together to recall the childhood holding the button machine to play box pushing days! At the end of the article with ten thousand word source!

directory

First, write first

Second, design ideas

1. Main interface functions are introduced

2. Select interface functions

3. Select the function

4. Game step replay function

5. Keyboard setting function

6. Game interface functions

7. Left function

8. Function to the right

9. Downward function

10. Upward function

11. Pass functions

12. Optimal recording function

13. Queue function

Exit the queue function

Third, the final conclusion

Four, complete source code

Hello, I’m Grey Ape! A super bug writing program ape!

Remember we had that pushed box, home button of nokia’s only, when I was a kid pushed box, snake is I most often play games every day, and then just have a friend recently asked me if I ever do a related development, so here today with you a simple C language implementation tuixiangzi game, with everyone together aftertaste childhood ah! Click like collection, get on the car sit well!

First, write first

Tuixiangzi game program is designed to separate each type of function class, set tuixiangzi house layout of two-dimensional array data structure, and record the number of steps and shortest best two-dimensional array, by function was carried out on the house layout, define the queue data structure classes, and set up the members of the public function and private member function.

At the same time, I use the derived class in c + + knowledge set is the same as the name of the class and not the object of the same name, is used to construct tuixiangzi game distribution function, set the data structure of a team with the function of entrance, at the same time design case as object, based on the horizontal ordinate position function, data interface design is based on the game, Start interface clearance tips, game options tips, when playing the game interface display map clearance tips and leaderboards, etc.

At the same time, according to the requirements of the game set output game main interface and game level selection interface, used for our operation, the structure of the game steps to replay the function, according to the above and various types of detailed functions set the main function and operation of the game, in order to achieve the smooth operation of the game requirements. In addition to the above, I also used the multi-grouping construction mode to make the program content easy to understand, and anyone who has the basic knowledge of the program can communicate and learn. Next I share with you the main module design, ** finally attached complete source code for your reference! ** friends don’t forget three support, collection learning ah!

Second, design ideas

In the program, I used classes and objects to complete the design of the game map, as well as the selection of levels and many other functions. And using the concept of data structure, the positioning of people and boxes and time steps and other functions are realized. In general, the program covers everything we’ve learned, and while the game is a little simple, it covers a lot of stuff,

The design purpose of the box pushing game is to make reasonable and effective use of data structure stack and queue, as well as the relevant knowledge of C++ classes and objects and derived classes, to separate each link in the game, and use derived classes to connect each link in series, so as to achieve the purpose of stable operation of the program. Here is the overall design:

1. Main interface functions are introduced

In the box under the begin of the derived class, our main function is to output the game interface, in this function, our internal functions to the game are introduced in detail, including the gameplay, and operating instructions, at the same time, main interface in detail to optimize the game, in the function in the function on the introduction of game, we respectively using the stars, Circles and special symbols represent boxes, target positions and small people pushing boxes. We call the functions of observation and selection to complete the game. Players can choose the game levels.

2. Select interface functions

In the game selection screen, we adopt double star interface graphical animation, set in the middle level options, select options are 1 to 4 levels to choose from, after the user select the levels, can be returned to the function will choose a number of levels, the function of players to choose the serial number for identification, once to select judgment of 1 to 4 levels, Then jump to the corresponding function interface in the program, for players to play the game, if the player input serial number is not between 1 and 4, the system will automatically prompt input error, please re-enter the words.

3. Select the function

In the derived class selectors, we set a replay, the main interface, the best record, withdrew from the four choices, when the players cannot customs clearance, can press the c key on the keyboard to jump out of selecting interface for player’s choice, and choice of level set, our players feedback over 1 to 4 four serial number for judgment, then in turn to play, back to the main interface, Jump out the best record, exit the system four functions.

4. Game step replay function

Play in the game after the customs clearance, we will set a gaming process repeats function, after the function, the player can watch the program to record and set a good game players to step step repeats, in this function, we mainly according to the players on the up and down or so key operational direction, the direction of the use of stored in the array, When the player chooses to replay the steps of the game, we can release the data in this two-dimensional array to achieve the effect of replay the steps of the game.

5. Keyboard setting function

After the start of the game, players are playing by the up and down or so direction key to small and medium-sized control operation, and the game of small and medium-sized the direction key control operation, what we have is based on Ascii keyboard keys, the left key is no. 75, right-click on key for 77 to 72, the key is no. 80, after the game, players to manipulate, the buttons at the same time, We will be in the internal functions to the memory of 75,77,72,84 number, when the return value is one of the four number, we will put the record in function, and add 1 in the gaming process, in which we also set the c key on the keyboard of 26, when choosing a c key can be carried out to select the operation, after the choice again, We have two options for players to return to the main screen and exit the game.

6. Game interface functions

In the game interface function, we set up a variety of shapes of the game to represent our different content inside the game, respectively to ‘■’ on behalf of the wall, ‘○’ on behalf of the target position, ‘★’ on behalf of boxes, ‘♀’ representative, and ‘? ‘represents the effect of boxes in the target position display, at the same time, at the bottom of the interface, We added undo (Ctrl+ Z), select (C) and game step recording options, which can record the player’s steps in real time. The effect picture is as follows:

7. Left function

Left in the game keys function, we have conducted a number of judgments, based on which the us is a little to the left position of address for the return value, if we detect the array value within the judgment is zero, we will record figures on the left side of the functions assigned to an array of four, and then judge, at the same time, if the array has few equals 2, We want to make the personnel move to the target position, and restore the function of the target position to the original position, at the same time mark the position, record the personnel passing through the position, so that we can facilitate the operation of the replay function.

If we detect the function value is zero, or 3, we can perform the push the boxes into the blank location on the function of operation, if the function is 5 or 1, we need to perform the box from the target position will launch the function of the operation, if the function value of 3 and 2, we want to enforce a push the box to the target location on the function of the operation, And the person in the target position is used to offset the situation that is still not moving.

8. Function to the right

When we perform function to the right, on the basis of the principle and function similar to the left, we address for judgment of right and return value, if the function value is equal to zero, we make the following operation, if the return value is equal to 2, and we want to move staff to the target position, restore target location and the original status flag at the same time, the address on the record is still in the position, Convenient we repeated operation, otherwise, if the function value of 3 and 0, we push the box to the blank position, if the function value of 5 or is not equal to 1, we’re going to the box from the target position, move to the next target location, and the target location, if the function value is 3 to 2, to push the box to the target location.

9. Downward function

In down function, we to return address down buttons, and read the judgment, if is recorded in the array to 2, to people who want to move to the target position, to restore the original state of target location mark at the same time, the address on the record is still in the target location, convenient we repeated operation, if the array has a value of 3 and is equal to zero, Pushed box above to the blank position, will launch the box from the positions, if a location or target location, we will be the value of 5 or 4, if the next position is blank, then we will be the array values assigned to 3 and 4, if the array has a value of 3 and 2, we will push the box to the target location, and if the array value is 5, Push the box above the target position, still in the target position, otherwise if the person is not above the target position, we assign the value of this function to 0, and cancel out the case of the person not moving.

10. Upward function

When performing up function, we value returns the address of the corresponding function to judge, if the array value is 0, then we will be the array values assignment 4, otherwise, if the function value of 2, we need to show that people in the target location, at the same time to restore the original state of target location mark, record the address of a man on the target location in this position the replay is convenient for operation, If the array position for 3 and 0, we push the box to the blank Spaces, or if the array value is 5 or is not equal to 1, to box is introduced from the target position, if the next location or target location, we will be the output value assignment of 5, will be another array assignment 4, if the array assignment 3, and the other an array value is 2, To push the box to the target position, assign the value to 5, place the box at the target position, still above the target position, and adjust the fluctuation if the person is above the target position.

11. Pass functions

After players for customs clearance, our system will automatically identify, and return to the interface of a customs clearance, we will work with the players ask the interface, the operation is divided into four steps, and continue to watch the customs clearance process video, view the best record, exit interface, back to the main interface, when the player after the selection of 1 to 4, we will return the corresponding operation, And you can press any key to return to the main page.

12. Optimal recording function

In best record function, we set a value to record the steps game players, when the game players to manipulate time, the key is defined the function of the variable is added once, when we read to the players after customs clearance, will should be maintained data, when we test that has the best record, that is, the steps to the minimum, to the best record.

13. Queue function

In the queue in the function, the main data structure of knowledge, set up the function into the queue, and defines a pointer variable pointing to the first node, the related numerical after into the queue, will automatically numerical value to the team head position, and loops make searchp node is not null, then according to the order, numerical in turn from the team will head to tail for assignment operators,

Exit the queue function

In the queue operations, the main rules according to the queue, set the pointer variable, the head element is assigned to a pointer, at the same time to determine if statement will have to be a for loop such as I value less than the length of the square, use the team head, receive searchp numerical, at the same time, let the count value continuously for relief operations,

Third, the final conclusion

By here, the basic function of tuixiangzi game is finished, finally to summarize it, we design tuixiangzi game, reasonable use of the knowledge of data structure in the queue, when using these knowledge, we also carried on the detailed optimization process of the game, through this tuixiangzi game design, You should be able to learn the queue in the actual application of the operation, it is also very good to let everybody to a solid knowledge of data structure and review, also learned from the game interface, layout structure thought and method, and the effective connection between the keyboard and the code, so this design can learn things still have a lot of.

I put the full source code at the end, so you can modify the map interface and set the level yourself.

Four, complete source code

//推箱子小游戏
#include<iostream>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#include<fstream>
#include<iomanip>
using namespace std;
const int roomsize = 9;//设计房子内部为正方形,边长为9
int map[roomsize + 2][roomsize + 2]; //推箱子房子布局的数据结构:二维数组
int followmap[1000];
int data;//记录最短步骤数目
int times = 0;
int array[2] = { 100, 100 }; //记录最好成绩
char String[30] = "开始比赛...........";
//以下为前几轮游戏房子中细节布局的数据结构:二维数组的实际内容
int map1[roomsize + 2][roomsize + 2] =
{   //0,1,2,3,4,5,6,7,8,9,10
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, //0
	{ -1, 0, 0, 0, 0, 1, 1, 1, 1, 1, -1 },          //1
	{ -1, 0, 0, 0, 0, 1, 2, 0, 0, 1, -1 },          //2
	{ -1, 1, 1, 1, 0, 1, 0, 3, 0, 1, -1 },          //3
	{ -1, 1, 2, 1, 0, 1, 0, 0, 0, 1, -1 },          //4
	{ -1, 1, 2, 1, 0, 1, 0, 3, 0, 1, -1 },          //5
	{ -1, 1, 2, 1, 1, 1, 0, 3, 0, 1, -1 },          //6
	{ -1, 1, 0, 0, 0, 0, 3, 4, 0, 1, -1 },          //7
	{ -1, 1, 0, 0, 1, 0, 0, 0, 0, 1, -1 },          //8
	{ -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1 },          //9
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } //10
};
int map2[roomsize + 2][roomsize + 2] =
{   //0,1,2,3,4,5,6,7,8,9,10
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },//0
	{ -1, 0, 1, 1, 1, 1, 1, 0, 0, -1, -1 },        //1
	{ -1, 0, 1, 4, 0, 0, 1, 1, 0, -1, -1 },        //2
	{ -1, 0, 1, 0, 3, 0, 0, 1, 0, -1, -1 },        //3
	{ -1, 1, 1, 1, 0, 1, 0, 1, 1, -1, -1 },        //4
	{ -1, 1, 2, 1, 0, 1, 0, 0, 1, -1, -1 },        //5
	{ -1, 1, 2, 3, 0, 0, 1, 0, 1, -1, -1 },        //6
	{ -1, 1, 2, 0, 0, 0, 3, 0, 1, -1, -1 },        //7
	{ -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1 },        //8
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },//9
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } //10
};
int map3[roomsize + 2][roomsize + 2] =
{   //0,1,2,3,4,5,6,7,8,9,10
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },//0
	{ -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1 },        //1
	{ -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1 },        //2
	{ -1, 1, 1, 0, 0, 0, 0, 1, 1, -1, -1 },        //36
	{ -1, 1, 0, 3, 0, 3, 3, 0, 1, -1, -1 },        //4
	{ -1, 1, 2, 2, 2, 2, 2, 2, 1, -1, -1 },        //5
	{ -1, 1, 0, 3, 3, 0, 3, 0, 1, -1, -1 },        //6
	{ -1, 1, 1, 1, 0, 1, 1, 1, 1, -1, -1 },        //7
	{ -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1 },        //8
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },//9
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } //10
};
int map4[roomsize + 2][roomsize + 2] =
{   //0,1,2,3,4,5,6,7,8,9,10
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },//0
	{ -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1 },        //1
	{ -1, 1, 0, 0, 0, 0, 0, 0, 1, -1, -1 },        //2
	{ -1, 1, 0, 3, 0, 1, 1, 1, 1, -1, -1 },        //3
	{ -1, 1, 0, 0, 0, 2, 2, 1, 1, -1, -1 },        //4
	{ -1, 1, 0, 0, 1, 2, 1, 1, 1, -1, -1 },        //5
	{ -1, 1, 0, 3, 0, 4, 3, 0, 1, -1, -1 },        //6
	{ -1, 1, 0, 0, 0, 0, 0, 0, 1, -1, -1 },        //7
	{ -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1 },        //8
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },//9
	{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } //10
};



class node
{
public:
	int data[1000];
	int positionL;//位置左
	int positionH;//位置
	node *next;

};
/*
*队列
*/
class linkqueue//定义队列类
{
private://定义私有数据成员
	node *front;
	int count;
public://定义公有数据成员
	linkqueue();
	~linkqueue();
	void insert(int item[]);//定义公有成员函数
	void out(int item[]);
	void clearqueue(void);
	int getcount();
};
linkqueue::linkqueue()//定义相同类名的派生类
{
	front = new node;//将定义的变量赋值给队头
	front->next = NULL;//队头指向空节点
	count = 0;
}
linkqueue::~linkqueue()
{
	clearqueue();
	count = 0;
}

void linkqueue::out(int item[])//出队列
{
	node *searchp;
	searchp = front->next;//将队头元素赋值给指针
	for (int i = 0; i<(roomsize + 2)*(roomsize + 2); i++)//使i的值小于正方形滴边长
		item[i] = searchp->data[i];
	front->next = searchp->next;//队头接收searchp
	delete searchp;
	count--;
}

void linkqueue::insert(int item[])//进队
{
	node *newnodep = new node, *searchp = front; 
	while (searchp->next != NULL)//循环使searchp节点不为空
		searchp = searchp->next;
	for (int i = 0; i<121; i++)
		newnodep->data[i] = item[i];
	newnodep->next = searchp->next;
	searchp->next = newnodep;
	count++;
}
void linkqueue::clearqueue(void)//定义依据于linkequeue的派生类
{

	if (front->next == NULL)//若头结点为空。则返回
		return;
	while (front->next != NULL)//若队头结点不为空,则将头结点赋值给指针
	{
		node *searchp;
		searchp = front->next;
		front->next = searchp->next;
		delete searchp;
	}
	count = 0;
}
int linkqueue::getcount()//定义依据于linkequeue的派生类用来返回count滴值
{
	return count;
}
/*
*栈
*/
class  seqstack//定义类
{
public://设定公有成员函数
	seqstack();
	~seqstack();
	void clearseqstack(void);
	void push(int item[], int &line, int &lie);//括号内为只接收变量滴地址
	void pop(int item[], int &line, int &lie);
private:
	node *top;
};
seqstack::seqstack()
{
	top = new node;
	top->next = NULL;
}
seqstack::~seqstack()
{}

void seqstack::push(int item[], int &line, int &lie)//定义以seqtack类滴push(推)的类,用来记录推动箱子的地址和位置
{
	node *newnodep, *searchp = top;
	newnodep = new node;
	for (int i = 0; i<(roomsize + 2)*(roomsize + 2); i++)
		newnodep->data[i] = item[i];
	newnodep->positionH = line;//将地址进行返回
	newnodep->positionL = lie;//将地址进行返回
	newnodep->next = searchp->next;
	searchp->next = newnodep;

}

void seqstack::pop(int item[], int &line, int &lie)
{
	node *newnodep, *searchp = top;
	if (searchp->next != NULL)
	{
		newnodep = top->next;
		for (int i = 0; i<(roomsize + 2)*(roomsize + 2); i++)
			item[i] = newnodep->data[i];
		line = newnodep->positionH;
		lie = newnodep->positionL;
		top->next = newnodep->next;
		delete newnodep;
	}
}
void seqstack::clearseqstack(void)
{
	if (top->next == NULL)
		return;
	while (top->next != NULL)
	{
		node *searchp;
		searchp = top->next;
		top->next = searchp->next;
		delete searchp;
	}
}

//对象:箱子
class box//记录人位置滴函数
{
	int positionh;//人的位置纵坐标
	int positionl;//人的位置横坐标
	int flag;//标志位,记录人在目标位置上
	int gate;//这个变量是记录关数
	int count;//这个变量是记录步数
	seqstack st;
	linkqueue linkqu;
public:
	box();
	void begin();//开始界面
	void choose_gate();//选关提示
	void choose();//游戏时c选项的提示
	void replay();//重玩
	void playing();//玩游戏时界面
	void display();//显示地图
	void left();//左方向
	void right();//右方向
	void down();//下方向
	void up();//上方向
	void test_flag();//过关提示
	void record();//这段函数为排行榜
	void travers();
	void returnpush();
	void returninseart();
	//void random();//这段函数为随机生成室内布局,暂时没有提供
};

box::box()
{
	positionh = 0;
	positionl = 0;
	flag = 0;
	count = 0;
	gate = 0;
}
void box::begin()
{
	system("color 17");

	/*******************************输出游戏主界面***********************************/
	cout << "   ╭────────────-─────────────────-──╮" << endl <<//1                                                                       "<<endl<<//1
		"  │                                                                  │" << endl <<//2
		"  │              ★☆★       推箱子游戏        ★☆★               │" << endl <<//3
		"  │★☆★★☆★★☆★★☆★★☆★★☆★★☆★★☆★★☆★★☆★★☆★│" << endl <<//4
		"  │                            游戏介绍                              │" << endl <<//5
		"  │                                                                  │" << endl <<//6
		"  │怎么玩这个游戏呢?我来介绍一下:这是小人人(♀)小星星就是箱子啦(★)│" << endl <<//7
		"  │你要把星星放在这个地方喔(○),等到有了㊣.你就赢咯!快来挑战吧!!   │" << endl <<//8
		"  │                                                                  │" << endl <<//9
		"  │                           操作指令                               │" << endl <<//10
		"  │                                                                  │" << endl <<//11
		"  │使用方向键控制哦!'Ctrl+z' 用来撤销,'c'用来选择 'Esc'退出游戏!  │" << endl <<//12
		"  │                                                                  │" << endl <<//13
		"  │                                                                  │" << endl <<//14
		"  ╰─────────────────────────────────╯" << endl; //15
	choose_gate();//选择关数
	cout << String << endl;
	Sleep(1000);
	system("cls");
	linkqu.clearqueue();
	st.clearseqstack();
	playing();
}
void box::choose_gate()
{
	//system("color 10");
	int j, k;

		/*******************************输出游戏关卡选择界面***********************************/
	cout << "              ★               ╭────╮            ★      " << endl
		<< "             ★★             │ 关卡选择 │          ★★      " << endl
		<< "            ★  ★            │ 1.first  │         ★  ★        " << endl
		<< "       ★★★    ★★★       │ 2.scend  │    ★★★    ★★★     " <<endl
		<< "        ★          ★        │ 3.third  │     ★          ★        " << endl
		<< "         ★        ★         │ 4.forth  │      ★        ★          " << endl
		<< "          ★  ★  ★          │★★★★  │       ★  ★  ★            " << endl
		<< "          ★★  ★★          │  ★★★★│       ★★  ★★         " << endl
		<< "          ★      ★          ╰─────╯       ★      ★         " << endl<<endl<<endl;
	cout << "请选择关卡哟:";
	cin >> gate;
	do
	{
		switch (gate)
		{
		case 1:
			for (j = 0; j<roomsize + 2; j++)//此处 j控制行,k控制列
			for (k = 0; k<roomsize + 2; k++)
				map[j][k] = map1[j][k];
			positionh = 7; positionl = 7;
			break;
		case 2:
			for (j = 0; j<roomsize + 2; j++)
			for (k = 0; k<roomsize + 2; k++)
				map[j][k] = map2[j][k];
			positionh = 2; positionl = 3;
			break;
		case 3:
			for (j = 0; j<roomsize + 2; j++)
			for (k = 0; k<roomsize + 2; k++)
				map[j][k] = map3[j][k];
			positionh =7, positionl = 5;
			break;
		case 4:
			for (j = 0; j<roomsize + 2; j++)
			for (k = 0; k<roomsize + 2; k++)
				map[j][k] = map4[j][k];
			positionh = 6, positionl = 5;
			break;
		default:
			cout << "输入错误啦^_^请重新输入哟@v@!";
			cin >> gate;
		}
	} while (gate>4);
}
void box::choose()//选项
{
	int choice;
	cout << " ╭────────╮" << endl
		<< " │1. 重播         │" << endl
		<< " │2. 主界面       │" << endl
		<< " │3. 最好的记录   │" << endl
		<< " │4. 退出         │" << endl
		<< " ╰────────╯" << endl;
	cin >> choice;
	switch (choice)
	{
	case 1:
		system("cls");
		replay();
		break;
	case 2:
		system("cls");
		begin();
		break;
	case 3:
		record();
		system("cls");
		playing();
		break;
	case 4:
		exit(0);
	}
}
void box::replay()//将游戏步骤进行重播
{
	int j, k;
	count = 0;
	flag = 0;
	st.clearseqstack();
	linkqu.clearqueue();
	do
	{
		switch (gate)
		{
		case 1:
			for (j = 0; j<roomsize + 2; j++)
			for (k = 0; k<roomsize + 2; k++)
				map[j][k] = map1[j][k];
			positionh = 7; positionl = 7;
			break;
		case 2:
			for (j = 0; j<roomsize + 2; j++)
			for (k = 0; k<roomsize + 2; k++)
				map[j][k] = map2[j][k];
			positionh = 2; positionl = 3;
			break;
		case 3:
			for (j = 0; j<roomsize + 2; j++)
			for (k = 0; k<roomsize + 2; k++)
				map[j][k] = map3[j][k];
			positionh = positionl = 4;
			break;
		case 4:
			for (j = 0; j<roomsize + 2; j++)
			for (k = 0; k<roomsize + 2; k++)
				map[j][k] = map4[j][k];
			positionh = 6, positionl = 5;
			break;
		
		}
	} while (gate>4);
	playing();
}
void box::playing()//Ascii码键盘键位:左为75 右为77 上为72 下为80
{
	int choice, i, l, r, item[1000],j,k;
	count = 0;
	cout << "游戏开始";
	while (1)
	{
		display();
		switch (_getch())
		{
		case 72:
			returninseart();
			returnpush();
			up();
			count++;
			break;
		case 80:
			returninseart();
			returnpush();
			down();
			count++;
			break;
		case 75:
			returninseart();
			returnpush();
			left();
			count++;
			break;
		case 77:
			returninseart();
			returnpush();
			right();
			count++;
			break;
		//case 'x':
		case 26:
			i = 0;
			system("cls");
			st.pop(item, l, r);
			for (j = 0; j<roomsize + 2; j++)
			for (k = 0; k<roomsize + 2; k++)
			{
				map[j][k] = item[i];
				i++;
			}
			positionl = r; positionh = l;
			display();
			break;
		case 'c':
		case 'C':
			choose();
			break;
		//case 'q':
		case 27:
			cout << " ╭──────────────╮" << endl
				<< " │请给你选择喔:              │" << endl
				<< " │   1. 我要返回主界面        │" << endl
				<< " │   2. 我不玩了退出游戏      │" << endl
				<< " ╰──────────────╯" << endl;
			cin >> choice;
			switch (choice)
			{
			case 1:
				count = 0;
				Sleep(500);
				system("cls");
				begin();
				break;
			case 2:
				exit(0);
			}
		default:
			break;
		}
		system("cls");
	}
}
void box::display()
{
	cout << endl << endl << endl << endl << endl << endl;
	for (int i = 1; i <= roomsize; i++)
	{
		cout << setw(30);
		for (int j = 1; j <= roomsize; j++)
		{
			if (map[i][j] == 0) cout << "  ";
			if (map[i][j] == 1) cout << "■";//墙
			if (map[i][j] == 2) cout << "○";//目标位置
			if (map[i][j] == 3) cout << "★";//箱子
			if (map[i][j] == 4) cout << "♀";//人
			if (map[i][j] == 5) cout << "㊣";//箱子在目标位置上
		}
		cout << endl;
	}
	cout << endl << endl;
	cout << "撤销(Ctrl+z)★★★" << "选择(c)★★★" << "游戏步数:" << count << endl;
}
void box::left()//向左函数
{
	if (map[positionh][positionl - 1] == 0)
	{
		map[positionh][positionl - 1] = 4;
		if (flag == 1)
		{
			map[positionh][positionl] = 2;  flag = 0;
		}
		else
			map[positionh][positionl] = 0;
		positionl--;
	}
	else if (map[positionh][positionl - 1] == 2)//人要到目标位置上
	{
		map[positionh][positionl - 1] = 4;
		if (flag == 1)
			map[positionh][positionl] = 2;//恢复目标位置
		else
		{
			map[positionh][positionl] = 0;//恢复原来的状态
			flag = 1;//标志位,记录人在目标位置上
		}
		positionl--;
	}
	else if (map[positionh][positionl - 1] == 3 && map[positionh][positionl - 2] == 0)//将箱子推到空白位置上
	{
		map[positionh][positionl - 2] = 3;
		map[positionh][positionl - 1] = 4;
		if (flag == 1)
		{
			map[positionh][positionl] = 2; flag = 0;
		}
		else
			map[positionh][positionl] = 0;
		positionl--;
	}
	else if (map[positionh][positionl - 1] == 5 && map[positionh][positionl - 2] != 1)//要将箱子从目标位置上推出
	{
		if (map[positionh][positionl - 2] == 2)//下一个位置还是目标位置
		{
			map[positionh][positionl - 2] = 5;
			map[positionh][positionl - 1] = 4;
			if (flag == 1)
				map[positionh][positionl] = 2;
			else
			{
				map[positionh][positionl] = 0; flag = 1;
			}
		}
		else if (map[positionh][positionl - 2] == 0)//下一个位置是空白
		{
			map[positionh][positionl - 2] = 3;
			map[positionh][positionl - 1] = 4;
			if (flag == 1)
				map[positionh][positionl] = 2;
			else
			{
				map[positionh][positionl] = 0; flag = 1;
			}
		}
		positionl--;
	}
	else if (map[positionh][positionl - 1] == 3 && map[positionh][positionl - 2] == 2)//要将箱子推到目标位置上
	{
		map[positionh][positionl - 2] = 5;//箱子在目标位置上
		map[positionh][positionl - 1] = 4;
		if (flag == 1)//人在目标位置上
		{
			map[positionh][positionl] = 2; flag = 0;
		}
		else //人不在目标位置上
			map[positionh][positionl] = 0;
		positionl--;
	}
	else count--;//抵消人不动的情况
	test_flag();

}
void box::right()//向右函数
{
	if (map[positionh][positionl + 1] == 0)
	{
		map[positionh][positionl + 1] = 4;
		if (flag == 1)
		{
			map[positionh][positionl] = 2;  flag = 0;
		}
		else
			map[positionh][positionl] = 0;
		positionl++;
	}
	else if (map[positionh][positionl + 1] == 2)//人要到目标位置上
	{
		map[positionh][positionl + 1] = 4;
		if (flag == 1)
			map[positionh][positionl] = 2;//恢复目标位置
		else
		{
			map[positionh][positionl] = 0;//恢复原来的状态
			flag = 1;//标志位,记录人在目标位置上
		}
		positionl++;
	}
	else if (map[positionh][positionl + 1] == 3 && map[positionh][positionl + 2] == 0)//将箱子推到空白位置上
	{
		map[positionh][positionl + 2] = 3;
		map[positionh][positionl + 1] = 4;
		if (flag == 1)
		{
			map[positionh][positionl] = 2; flag = 0;
		}
		else
			map[positionh][positionl] = 0;
		positionl++;
	}
	else if (map[positionh][positionl + 1] == 5 && map[positionh][positionl + 2] != 1)//要将箱子从目标位置上推出
	{
		if (map[positionh][positionl + 2] == 2)//下一个位置还是目标位置
		{
			map[positionh][positionl + 2] = 5;
			map[positionh][positionl + 1] = 4;
			if (flag == 1)
				map[positionh][positionl] = 2;
			else
			{
				map[positionh][positionl] = 0; flag = 1;
			}
		}
		else if (map[positionh][positionl + 2] == 0)//下一个位置是空白
		{
			map[positionh][positionl + 2] = 3;
			map[positionh][positionl + 1] = 4;
			if (flag == 1)
				map[positionh][positionl] = 2;
			else
			{
				map[positionh][positionl] = 0; flag = 1;
			}
		}
		positionl++;
	}
	else if (map[positionh][positionl + 1] == 3 && map[positionh][positionl + 2] == 2)//要将箱子推到目标位置上
	{
		map[positionh][positionl + 2] = 5;//箱子在目标位置上
		map[positionh][positionl + 1] = 4;
		if (flag == 1)//人在目标位置上
		{
			map[positionh][positionl] = 2; flag = 0;
		}
		else //人不在目标位置上
			map[positionh][positionl] = 0;
		positionl++;
	}
	else count--;//抵消人不动的情况
	test_flag();
}
void box::down()//向下函数
{
	if (map[positionh + 1][positionl] == 0)
	{
		map[positionh + 1][positionl] = 4;
		if (flag == 1)
		{
			map[positionh][positionl] = 2; flag = 0;
		}
		else
			map[positionh][positionl] = 0;
		positionh++;
	}
	else if (map[positionh + 1][positionl] == 2)//人要到目标位置上
	{
		map[positionh + 1][positionl] = 4;
		if (flag == 1)
			map[positionh][positionl] = 2;//恢复目标位置
		else
		{
			map[positionh][positionl] = 0;//恢复原来的状态
			flag = 1;//标志位,记录人在目标位置上
		}
		positionh++;
	}
	else if (map[positionh + 1][positionl] == 3 && map[positionh + 2][positionl] == 0)//将箱子推到空白位置上
	{
		map[positionh + 2][positionl] = 3;
		map[positionh + 1][positionl] = 4;
		if (flag == 1)
		{
			map[positionh][positionl] = 2; flag = 0;
		}
		else
			map[positionh][positionl] = 0;
		positionh++;
	}
	else if (map[positionh + 1][positionl] == 5 && map[positionh + 2][positionl] != 1)//要将箱子从目标位置上推出
	{
		if (map[positionh + 2][positionl] == 2)//下一个位置还是目标位置
		{
			map[positionh + 2][positionl] = 5;
			map[positionh + 1][positionl] = 4;
			if (flag == 1)
				map[positionh][positionl] = 2;
			else
			{
				map[positionh][positionl] = 0; flag = 1;
			}
		}
		else if (map[positionh + 2][positionl] == 0)//下一个位置是空白
		{
			map[positionh + 2][positionl] = 3;
			map[positionh + 1][positionl] = 4;
			if (flag == 1)
				map[positionh][positionl] = 2;
			else
			{
				map[positionh][positionl] = 0; flag = 1;
			}
		}
		positionh++;
	}
	else if (map[positionh + 1][positionl] == 3 && map[positionh + 2][positionl] == 2)//要将箱子推到目标位置上
	{
		map[positionh + 2][positionl] = 5;//箱子在目标位置上
		map[positionh + 1][positionl] = 4;
		if (flag == 1)//人在目标位置上
		{
			map[positionh][positionl] = 2; flag = 0;
		}
		else //人不在目标位置上
			map[positionh][positionl] = 0;
		positionh++;
	}
	else count--;//抵消人不动的情况
	test_flag();
}
void box::up()//向上函数
{
	if (map[positionh - 1][positionl] == 0)
	{
		map[positionh - 1][positionl] = 4;
		if (flag == 1)
		{
			map[positionh][positionl] = 2; flag = 0;
		}
		else
			map[positionh][positionl] = 0;
		positionh--;
	}
	else if (map[positionh - 1][positionl] == 2)//人要到目标位置上
	{
		map[positionh - 1][positionl] = 4;
		if (flag == 1)
			map[positionh][positionl] = 2;//恢复目标位置
		else
		{
			map[positionh][positionl] = 0;//恢复原来的状态
			flag = 1;//标志位,记录人在目标位置上
		}
		positionh--;
	}
	else if (map[positionh - 1][positionl] == 3 && map[positionh - 2][positionl] == 0)//将箱子推到空白位置上
	{
		map[positionh - 2][positionl] = 3;
		map[positionh - 1][positionl] = 4;
		if (flag == 1)
		{
			map[positionh][positionl] = 2; flag = 0;
		}
		else
			map[positionh][positionl] = 0;
		positionh--;
	}
	else if (map[positionh - 1][positionl] == 5 && map[positionh - 2][positionl] != 1)//要将箱子从目标位置上推出
	{
		if (map[positionh - 2][positionl] == 2)//下一个位置还是目标位置
		{
			map[positionh - 2][positionl] = 5;
			map[positionh - 1][positionl] = 4;
			if (flag == 1)
				map[positionh][positionl] = 2;
			else
			{
				map[positionh][positionl] = 0; flag = 1;
			}
		}
		else if (map[positionh - 2][positionl] == 0)//下一个位置是空白
		{
			map[positionh - 2][positionl] = 3;
			map[positionh - 1][positionl] = 4;
			if (flag == 1)
				map[positionh][positionl] = 2;
			else
			{
				map[positionh][positionl] = 0; flag = 1;
			}
		}
		positionh--;
	}
	else if (map[positionh - 1][positionl] == 3 && map[positionh - 2][positionl] == 2)//要将箱子推到目标位置上
	{
		map[positionh - 2][positionl] = 5;//箱子在目标位置上
		map[positionh - 1][positionl] = 4;
		if (flag == 1)//人在目标位置上
		{
			map[positionh][positionl] = 2; flag = 0;
		}
		else //人不在目标位置上
			map[positionh][positionl] = 0;
		positionh--;
	}
	else count--;//抵消人不动的情况
	test_flag();

}
void box::test_flag()
{
	int choice;
	int item[1000];
	for (int i = 1; i <= roomsize; i++)
	for (int j = 1; j <= roomsize; j++)
	{
		if (map[i][j] == 3)
			return;
	}
	system("cls");
	count++;
	data = count;
	times++;
	display();
	returninseart();
	cout << "╭──────────────╮" << endl
		<< "│恭喜小可爱呀!你通关啦哟!  │" << endl
		<< "│★★★ 再来一局不?★★★   │" << endl
		<< "│1. 继续                     │" << endl
		<< "│2. 观看通关过程             │" << endl
		<< "│3. 最好滴记录               │" << endl
		<< "│4. 退出呀                   │" << endl
		<< "╰──────────────╯" << endl;
	cin >> choice;
	switch (choice)
	{
	case 1:
		count = 0;
		Sleep(500);
		system("cls");
		begin();
		break;
	case 2:
		travers();
		cout << "按任意键回到主界面哟..." << endl;
		_getch();
		system("cls");
		begin();
		break;
	case 3:
		record();
		system("cls");
		cout << "按任意键回到主界面哟..." << endl;
		begin();
		break;
	case 4:
		cout << "★★★嘻嘻!欢迎再次游戏★★★" << endl;
		cout << "★★★按任意键退出喔★★★" << endl;
		_getch();
		exit(0);
	}
}
void box::record()//最佳记录
{
	int rhigh;
	if (times % 2)
		array[0] = data;
	else
		array[1] = data;
	if (array[0]>array[1])
		rhigh = array[1];
	else
		rhigh = array[0];
	if (times % 2)
		array[0] = rhigh;
	else
		array[1] = rhigh;
	cout << "最优秀滴记录:" << rhigh << endl;
	_getch();
}
void box::travers()
{
	int i, l = linkqu.getcount(), item[1000];
	while (l)
	{
		i = 0;
		linkqu.out(item);
		for (int j = 0; j<roomsize + 2; j++)
		for (int k = 0; k<roomsize + 2; k++)
		{
			map[j][k] = item[i];
			i++;
		}
		system("cls");
		display();
		Sleep(50);
		l--;
	}
}
void box::returnpush()
{
	int i = 0, l, r;
	for (int j = 0; j<roomsize + 2; j++)
	for (int k = 0; k<roomsize + 2; k++)
	{
		if (map[j][k] == 4)
		{
			l = j;
			r = k;
		}
		followmap[i] = map[j][k];
		i++;
	}
	st.push(followmap, l, r);
}
void box::returninseart()
{
	int i = 0;
	for (int j = 0; j<roomsize + 2; j++)
	for (int k = 0; k<roomsize + 2; k++)
	{
		followmap[i] = map[j][k];
		i++;
	}
	linkqu.insert(followmap);
}

//主程序
int main()
{
	box Mybox;
	system("color B0");
	Mybox.begin();
	return 0; 
}
Copy the code

Feel useful small partners can like attention yo!

If you have any questions, please leave them in the comments section!

I’m grey Ape! See you next time!