-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyDown.cpp
More file actions
153 lines (153 loc) · 4.76 KB
/
keyDown.cpp
File metadata and controls
153 lines (153 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Created by Mplan on 2022/7/24.
//
#include <iostream>
#include <conio.h>
#include "function.h"
using namespace std;
extern int map[8][8];
//按键交互:移动
void keyDown() {
//动起来:人动,箱子动
//定位人物
int i, j;
for (i = 0; i < 8; ++i) {
for (j = 0; j < 8; ++j) {
if (map[i][j] == 5 || map[i][j] == 8) {
break;
}
}
if (map[i][j] == 5 || map[i][j] == 8) {
break;
}
}
//找到人后:让人动:按键操作
//等待键盘输出一个值
char userKey = _getch();
switch (userKey) {
//向上
case 'w':
case 'W':
case 72:
//先判断当前位置
if (map[i][j] == 5) {
if (map[i - 1][j] == 0) {
map[i][j] = 0;
map[i - 1][j] = 5;
} else if (map[i - 1][j] == 3) {
map[i][j] = 0;
map[i - 1][j] = 8;
} else if (map[i - 1][j] == 4) {
moveBox('w',i,j);
} else if (map[i - 1][j] == 7) {
moveBox('w',i,j);
}
} else if (map[i][j] == 8) {
if (map[i - 1][j] == 0) {
map[i][j] = 3;
map[i - 1][j] = 5;
} else if (map[i - 1][j] == 3) {
map[i][j] = 3;
map[i - 1][j] = 8;
} else if (map[i - 1][j] == 4) {
moveBox('w',i,j);
} else if (map[i - 1][j] == 7) {
moveBox('w',i,j);
}
}
break;
//向下
case 's':
case 'S':
case 80:
if (map[i][j] == 5) {
if (map[i + 1][j] == 0) {
map[i][j] = 0;
map[i + 1][j] = 5;
} else if (map[i + 1][j] == 3) {
map[i][j] = 0;
map[i + 1][j] = 8;
} else if (map[i + 1][j] == 4) {
moveBox('s',i,j);
} else if (map[i + 1][j] == 7) {
moveBox('s',i,j);
}
} else if (map[i][j] == 8) {
if (map[i + 1][j] == 0) {
map[i][j] = 3;
map[i + 1][j] = 5;
} else if (map[i + 1][j] == 3) {
map[i][j] = 3;
map[i + 1][j] = 8;
} else if (map[i + 1][j] == 4) {
moveBox('s',i,j);
} else if (map[i + 1][j] == 7) {
moveBox('s',i,j);
}
}
break;
//向左
case 'a':
case 'A':
case 75:
if (map[i][j] == 5) {
if (map[i][j - 1] == 0) {
map[i][j] = 0;
map[i][j - 1] = 5;
} else if (map[i][j - 1] == 3) {
map[i][j] = 0;
map[i][j - 1] = 8;
} else if (map[i][j - 1] == 4) {
moveBox('a',i,j);
} else if (map[i][j - 1] == 7) {
moveBox('a',i,j);
}
}
if (map[i][j] == 8) {
if (map[i][j - 1] == 0) {
map[i][j] = 3;
map[i][j - 1] = 5;
} else if (map[i][j - 1] == 3) {
map[i][j] = 3;
map[i][j - 1] = 8;
} else if (map[i][j - 1] == 4) {
moveBox('a',i,j);
} else if (map[i][j - 1] == 7) {
moveBox('a',i,j);
}
}
break;
//向右
case 'd':
case 'D':
case 77:
if (map[i][j] == 5) {
if (map[i][j + 1] == 0) {
map[i][j] = 0;
map[i][j + 1] = 5;
} else if (map[i][j + 1] == 3) {
map[i][j] = 0;
map[i][j + 1] = 8;
} else if (map[i][j + 1] == 4) {
moveBox('d',i,j);
} else if (map[i][j + 1] == 7) {
moveBox('d',i,j);
}
}
if (map[i][j] == 8) {
if (map[i][j + 1] == 0) {
map[i][j] = 3;
map[i][j + 1] = 5;
} else if (map[i][j + 1] == 3) {
map[i][j] = 3;
map[i][j + 1] = 8;
} else if (map[i][j + 1] == 4) {
moveBox('d',i,j);
} else if (map[i][j + 1] == 7) {
moveBox('d',i,j);
}
}
break;
}
cout << map[i][j];
}