/*****************************2018.7.6***********************************/
#include
#include
#include
#include
#include
#include
#define
KEY_UP
72
#define
KEY_DOWN
80
#define
KEY_LEFT
75
#define
KEY_RIGHT
77
#define
MIN_LEVEL
1
#define
MAX_LEVEL
4
using
namespace
std
;
void
SelectColor
(
int
iColor){
//颜色函数
HANDLE hConsole
=
GetStdHandle
((STD_OUTPUT_HANDLE));
//得到控制台输出设备的句柄
SetConsoleTextAttribute
(hConsole, iColor);
//设置控制台设备的属性
}
int
aiMap1[
14
][
16
]
=
{
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
},
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
},
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
},
{
0
,
0
,
0
,
0
,
0
,
0
,
1
,
1
,
1
,
1
,
1
,
1
,
0
,
0
,
0
,
0
},
{
0
,
0
,
0
,
0
,
1
,
1
,
1
,
2
,
2
,
2
,
2
,
1
,
0
,
0
,
0
,
0
},
{
0
,
0
,
0
,
1
,
1
,
3
,
4
,
2
,
1
,
1
,
2
,
1
,
1
,
0
,
0
,
0
},
{
0
,
0
,
0
,
1
,
2
,
3
,
4
,
5
,
4
,
2
,
2
,
3
,
1
,
0
,
0
,
0
},
{
0
,
0
,
0
,
1
,
2
,
3
,
2
,
4
,
2
,
4
,
3
,
1
,
1
,
0
,
0
,
0
},
{
0
,
0
,
0
,
1
,
1
,
1
,
1
,
1
,
1
,
2
,
2
,
1
,
0
,
0
,
0
,
0
},
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
1
,
1
,
1
,
0
,
0
,
0
,
0
},
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
},
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
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b) :
flag
(a),
status
(b) {}
int
Get_Flag
() {
return
flag; }
bool
Get_Status
() {
return
status; }
void
Change_Status
(
bool
s) { status
=
s; }
};
class
Board
{
Object
*
board[
14
][
16
];
int
x, y;
//人的位置坐标
static
int
step;
//游戏步数
public:
Board
();
~Board
();
void
Input
(
char
c);
bool
Move_Box
(
int
begin_x,
int
begin_y,
int
end_x,
int
end_y);
//箱子移动及其判断方式
bool
Move
(
int
begin_x,
int
begin_y,
int
end_x,
int
end_y,
char
c);
//人的移动及其判断方式
void
Print
()
const
;
//打印界面
bool
Judge_Win
();
//判断是否胜利
void
Set_game
(
int
i);
//设置游戏关卡数
void
Menu
();
//主菜单
void
New_Game
(
int
i);
//开始游戏
void
Fresh
();
//清空棋盘以进入下一关
};
int
Board::step
=
0
;
void
Board::New_Game
(
int
i) {
Set_game
(i);
while
(
1
) {
Print
();
char
c
=
_getch
();
Input
(c);
if
(
Judge_Win
()
&&
i
==
MAX_LEVEL) {
cout
<<
"
\t\t
恭喜通关!!
\n
"
;
break
;
}
if
(
Judge_Win
()) {
i
++
;
cout
<<
"
\t\t
恭喜过关 下面来到第"
<<
i
<<
"关"
<<
endl;
Fresh
();
Set_game
(i);
}
}
}
Board::~Board
() {
for
(
int
i
=
0
; i
<
14
;i
++
)
for
(
int
j
=
0
; j
<
16
;j
++
) {
if
(board[i][j]
!=
NULL
)
delete
board[i][j];
}
}
Board::Board
() {
for
(
int
i
=
0
; i
<
14
;i
++
)
for
(
int
j
=
0
; j
<
16
;j
++
)
board[i][j]
=
NULL
;
}
void
Board::Fresh
() {
for
(
int
i
=
0
; i
<
14
;i
++
)
for
(
int
j
=
0
; j
<
16
;j
++
) {
if
(board[i][j]
!=
NULL
)
delete
board[i][j];
}
step
=
0
;
}
void
Board::Input
(
char
c) {
switch
(c) {
case
KEY_LEFT:
if
(
Move
(x, y, x, y
-
1
, c)) {
step
++
;
y
=
y
-
1
;
}
break
;
case
KEY_UP:
if
(
Move
(x, y, x
-
1
, y, c)) {
step
++
;
x
=
x
-
1
;
}
break
;
case
KEY_DOWN:
if
(
Move
(x, y, x
+
1
, y, c)) {
step
++
;
x
=
x
+
1
;
}
break
;
case
KEY_RIGHT:
if
(
Move
(x, y, x, y
+
1
, c)) {
step
++
;
y
=
y
+
1
;
}
break
;
}
}
void
Board::Set_game
(
int
l) {
switch
(l) {
case
1
:
for
(
int
i
=
0
;i
<
14
;i
++
)
for
(
int
j
=
0
;j
<
16
;j
++
) {
if
(aiMap1[i][j]
==
1
) board[i][j]
=
new
Object
(
2
,
false
);
//设置墙
if
(aiMap1[i][j]
==
2
) board[i][j]
=
new
Object
(
1
,
false
);
//设置空白
if
(aiMap1[i][j]
==
3
)board[i][j]
=
new
Object
(
1
,
true
);
//设置目的地
if
(aiMap1[i][j]
==
4
)board[i][j]
=
new
Object
(
3
,
false
);
//设置箱子
if
(aiMap1[i][j]
==
5
) {
board[i][j]
=
new
Object
(
4
,
false
);
//设置人
x
=
i, y
=
j;
}
if
(aiMap1[i][j]
==
6
)board[i][j]
=
new
Object
(
3
,
true
);
//设置已经到达目的地的箱子
}
break
;
case
2
:
for
(
int
i
=
0
;i
<
14
;i
++
)
for
(
int
j
=
0
;j
<
16
;j
++
) {
if
(aiMap2[i][j]
==
1
) board[i][j]
=
new
Object
(
2
,
false
);
if
(aiMap2[i][j]
==
2
) board[i][j]
=
new
Object
(
1
,
false
);
if
(aiMap2[i][j]
==
3
)board[i][j]
=
new
Object
(
1
,
true
);
if
(aiMap2[i][j]
==
4
)board[i][j]
=
new
Object
(
3
,
false
);
if
(aiMap2[i][j]
==
5
) {
board[i][j]
=
new
Object
(
4
,
false
);
x
=
i, y
=
j;
}
if
(aiMap2[i][j]
==
6
)board[i][j]
=
new
Object
(
3
,
true
);
}
break
;
case
3
:
for
(
int
i
=
0
;i
<
14
;i
++
)
for
(
int
j
=
0
;j
<
16
;j
++
) {
if
(aiMap3[i][j]
==
1
) board[i][j]
=
new
Object
(
2
,
false
);
if
(aiMap3[i][j]
==
2
) board[i][j]
=
new
Object
(
1
,
false
);
if
(aiMap3[i][j]
==
3
)board[i][j]
=
new
Object
(
1
,
true
);
if
(aiMap3[i][j]
==
4
)board[i][j]
=
new
Object
(
3
,
false
);
if
(aiMap3[i][j]
==
5
) {
board[i][j]
=
new
Object
(
4
,
false
);
x
=
i, y
=
j;
}
if
(aiMap3[i][j]
==
6
)board[i][j]
=
new
Object
(
3
,
true
);
}
break
;
case
4
:
for
(
int
i
=
0
;i
<
14
;i
++
)
for
(
int
j
=
0
;j
<
16
;j
++
) {
if
(aiMap4[i][j]
==
1
) board[i][j]
=
new
Object
(
2
,
false
);
if
(aiMap4[i][j]
==
2
) board[i][j]
=
new
Object
(
1
,
false
);
if
(aiMap4[i][j]
==
3
)board[i][j]
=
new
Object
(
1
,
true
);
if
(aiMap4[i][j]
==
4
)board[i][j]
=
new
Object
(
3
,
false
);
if
(aiMap4[i][j]
==
5
) {
board[i][j]
=
new
Object
(
4
,
false
);
x
=
i, y
=
j;
}
if
(aiMap4[i][j]
==
6
)board[i][j]
=
new
Object
(
3
,
true
);
}
break
;
}
}
bool
Board::Move
(
int
begin_x,
int
begin_y,
int
end_x,
int
end_y,
char
c) {
bool
begin
=
board[begin_x][begin_y]->
Get_Status
(), end
=
board[end_x][end_y]->
Get_Status
();
//space
if
(board[end_x][end_y]->
Get_Flag
()
==
1
) {
board[begin_x][begin_y]->
Change_Status
(end);
board[end_x][end_y]->
Change_Status
(begin);
swap
(board[begin_x][begin_y], board[end_x][end_y]);
return
true
;
}
//wall
else
if
(board[end_x][end_y]->
Get_Flag
()
==
2
)
return
false
;
//box
else
if
(board[end_x][end_y]->
Get_Flag
()
==
3
) {
int
flag
=
0
;
switch
(c) {
case
KEY_LEFT:
if
(
Move_Box
(end_x, end_y, end_x, end_y
-
1
))flag
=
1
;
break
;
case
KEY_RIGHT:
if
(
Move_Box
(end_x, end_y, end_x, end_y
+
1
)) flag
=
1
;
break
;
case
KEY_UP:
if
(
Move_Box
(end_x, end_y, end_x
-
1
, end_y))flag
=
1
;
break
;
case
KEY_DOWN:
if
(
Move_Box
(end_x, end_y, end_x
+
1
, end_y))flag
=
1
;
break
;
}
if
(flag) {
board[begin_x][begin_y]->
Change_Status
(end);
board[end_x][end_y]->
Change_Status
(begin);
swap
(board[begin_x][begin_y], board[end_x][end_y]);
return
flag;
}
}
return
false
;
}
bool
Board::Move_Box
(
int
begin_x,
int
begin_y,
int
end_x,
int
end_y) {
if
(board[end_x][end_y]->
Get_Flag
()
==
1
) {
bool
begin
=
board[begin_x][begin_y]->
Get_Status
(), end
=
board[end_x][end_y]->
Get_Status
();
board[begin_x][begin_y]->
Change_Status
(end);
board[end_x][end_y]->
Change_Status
(begin);
swap
(board[begin_x][begin_y], board[end_x][end_y]);
return
true
;
}
return
false
;
}
bool
Board::Judge_Win
() {
bool
flag
=
true
;
for
(
int
i
=
0
; i
<
14
;i
++
)
for
(
int
j
=
0
; j
<
16
;j
++
)
if
(board[i][j]
!=
NULL
&&
board[i][j]->
Get_Flag
()
==
3
&&
board[i][j]->
Get_Status
()
!=
true
)
flag
=
false
;
return
flag;
}
void
Board::Print
()
const
{
system
(
"cls"
);
for
(
int
i
=
0
; i
<
14
;i
++
) {
for
(
int
j
=
0
; j
<
16
;j
++
) {
if
(board[i][j]
==
NULL
)cout
<<
" "
;
if
(board[i][j]
!=
NULL
) {
if
(board[i][j]->
Get_Flag
()
==
2
) {
SelectColor
(
14
);
/*墙*/
printf
(
"▓"
);
}
if
(board[i][j]->
Get_Flag
()
==
1
) {
if
(board[i][j]->
Get_Status
()) {
SelectColor
(
11
);
/*目的地*/
printf
(
"○"
);
}
else
printf
(
" "
);
}
if
(board[i][j]->
Get_Flag
()
==
4
) {
SelectColor
(
10
);
printf
(
"♀"
);
}
if
(board[i][j]->
Get_Flag
()
==
3
) {
if
(board[i][j]->
Get_Status
()
==
true
) {
SelectColor
(
9
);
printf
(
"☆"
);
}
else
{
SelectColor
(
11
);
printf
(
"●"
);
}
}
}
}
cout
<<
endl;
}
cout
<<
"
\n\n\t\t
已走步数:"
<<
step
<<
endl;
}
void
Board::Menu
() {
system
(
"cls"
);
/*清屏*/
system
(
"color 0E"
);
/*设置颜色*/
printf
(
"
\n\n\t\t
Welcome to play box!
\n\n
"
);
printf
(
"
\t\t
Person:♀ Wall:▓ Box: ● Target:○ Reach target:☆
\n
"
);
printf
(
"
\t\t
Up:w
\n\t\t
Down:s
\n\t\t
Left:a
\n\t\t
Right:d
\n\n
"
);
printf
(
"
\t\t
Press number 1 to start new game
\n\t\t\n
"
);
printf
(
"
\t\t
Press number 2 to choose level
\n\t\t\n
"
);
printf
(
"
\t\t
Press number 3 to quit from game
\n\t\t\n
"
);
printf
(
"
\t\t
Press right number to continue:"
);
int
i;
cin
>>
i;
switch
(i) {
case
1
:
New_Game
(
1
);
break
;
case
2
:
cout
<<
"
\t\t
请输入进入的关卡(1-4)
\n
"
;
int
level;
cin
>>
level;
while
(level
<
MIN_LEVEL
||
level
>
MAX_LEVEL) {
cout
<<
"
\t\t
输入错误!!请输入重新进入的关卡(1 - 4)
\n
"
;
cin
>>
level;
}
New_Game
(level);
break
;
case
3
:
exit
(
0
);
}
}
int
main
() {
Board game;
cout
<<
"begin"
<<
endl;
game.
Menu
();
}