推箱子小游戏

#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; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值