using namespace std;
const int roomsize = 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] =
{
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, 0, 0, 0, 0, 1, 1, 1, 1, 1, -1 },
{ -1, 0, 0, 0, 0, 1, 2, 0, 0, 1, -1 },
{ -1, 1, 1, 1, 0, 1, 0, 3, 0, 1, -1 },
{ -1, 1, 2, 1, 0, 1, 0, 0, 0, 1, -1 },
{ -1, 1, 2, 1, 0, 1, 0, 3, 0, 1, -1 },
{ -1, 1, 2, 1, 1, 1, 0, 3, 0, 1, -1 },
{ -1, 1, 0, 0, 0, 0, 3, 4, 0, 1, -1 },
{ -1, 1, 0, 0, 1, 0, 0, 0, 0, 1, -1 },
{ -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }
};
int map2[roomsize + 2][roomsize + 2] =
{
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, 0, 1, 1, 1, 1, 1, 0, 0, -1, -1 },
{ -1, 0, 1, 4, 0, 0, 1, 1, 0, -1, -1 },
{ -1, 0, 1, 0, 3, 0, 0, 1, 0, -1, -1 },
{ -1, 1, 1, 1, 0, 1, 0, 1, 1, -1, -1 },
{ -1, 1, 2, 1, 0, 1, 0, 0, 1, -1, -1 },
{ -1, 1, 2, 3, 0, 0, 1, 0, 1, -1, -1 },
{ -1, 1, 2, 0, 0, 0, 3, 0, 1, -1, -1 },
{ -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }
};
int map3[roomsize + 2][roomsize + 2] =
{
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1 },
{ -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1 },
{ -1, 1, 1, 0, 0, 0, 0, 1, 1, -1, -1 },
{ -1, 1, 0, 3, 0, 3, 3, 0, 1, -1, -1 },
{ -1, 1, 2, 2, 2, 2, 2, 2, 1, -1, -1 },
{ -1, 1, 0, 3, 3, 0, 3, 0, 1, -1, -1 },
{ -1, 1, 1, 1, 0, 1, 1, 1, 1, -1, -1 },
{ -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }
};
int map4[roomsize + 2][roomsize + 2] =
{
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1 },
{ -1, 1, 0, 0, 0, 0, 0, 0, 1, -1, -1 },
{ -1, 1, 0, 3, 0, 1, 1, 1, 1, -1, -1 },
{ -1, 1, 0, 0, 0, 2, 2, 1, 1, -1, -1 },
{ -1, 1, 0, 0, 1, 2, 1, 1, 1, -1, -1 },
{ -1, 1, 0, 3, 0, 4, 3, 0, 1, -1, -1 },
{ -1, 1, 0, 0, 0, 0, 0, 0, 1, -1, -1 },
{ -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }
};
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++)
item[i] = searchp->data[i];
front->next = searchp->next;
delete searchp;
count--;
}
void linkqueue::insert(int item[])
{
node *newnodep = new node, *searchp = front;
while (searchp->next != NULL)
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)
{
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()
{
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)
{
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();
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();
};
box::box()
{
positionh = 0;
positionl = 0;
flag = 0;
count = 0;
gate = 0;
}
void box::begin()
{
system("color 17");
cout << " ╭────────────-─────────────────-──╮" << endl <<
" │ │" << endl <<
" │ ★☆★ 推箱子游戏 ★☆★ │" << endl <<
" │★☆★★☆★★☆★★☆★★☆★★☆★★☆★★☆★★☆★★☆★★☆★│" << endl <<
" │ 游戏介绍 │" << endl <<
" │ │" << endl <<
" │怎么玩这个游戏呢?我来介绍一下:这是小人人(♀)小星星就是箱子啦(★)│" << endl <<
" │你要把星星放在这个地方喔(○),等到有了㊣.你就赢咯!快来挑战吧!! │" << endl <<
" │ │" << endl <<
" │ 操作指令 │" << endl <<
" │ │" << endl <<
" │使用方向键控制哦!'Ctrl+z' 用来撤销,'c'用来选择 'Esc'退出游戏! │" << endl <<
" │ │" << endl <<
" │ │" << endl <<
" ╰─────────────────────────────────╯" << endl;
choose_gate();
cout << String << endl;
Sleep(1000);
system("cls");
linkqu.clearqueue();
st.clearseqstack();
playing();
}
void box::choose_gate()
{
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++)
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()
{
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 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 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;
}
玩不了啊