-
Notifications
You must be signed in to change notification settings - Fork 4
/
Maze.cpp
223 lines (189 loc) · 5.34 KB
/
Maze.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <cstdio>
#include <queue>
#include "Maze.h"
const uint8_t NORTH = 0x01;
const uint8_t EAST = 0x02;
const uint8_t SOUTH = 0x04;
const uint8_t WEST = 0x08;
const uint8_t DONE_NORTH = 0x10;
const uint8_t DONE_EAST = 0x20;
const uint8_t DONE_SOUTH = 0x40;
const uint8_t DONE_WEST = 0x80;
const IndexVec IndexVec::vecNorth(0,1);
const IndexVec IndexVec::vecEast(1,0);
const IndexVec IndexVec::vecSouth(0,-1);
const IndexVec IndexVec::vecWest(-1,0);
const IndexVec IndexVec::vecDir[4] = {IndexVec::vecNorth, IndexVec::vecEast, IndexVec::vecSouth, IndexVec::vecWest};
void Maze::clear()
{
for (int i=0;i<MAZE_SIZE;i++) {
for (int j=0;j<MAZE_SIZE;j++) {
wall[i][j] = 0;
}
}
for (int i=0;i<MAZE_SIZE;i++) {
wall[MAZE_SIZE-1][i] |= NORTH | DONE_NORTH;
wall[i][MAZE_SIZE-1] |= EAST | DONE_EAST;
wall[0][i] |= SOUTH | DONE_SOUTH;
wall[i][0] |= WEST | DONE_WEST;
}
dirty = true;
}
bool Maze::loadFromFile(const char *_filename)
{
dirty = true;
FILE *inputFile;
inputFile = std::fopen(_filename, "r");
if (inputFile == NULL) {
std::printf("ERROR : Failed open wall data file\n");
return false;
}
for (int i=0;i<3;i++) {
int dummy;
if (std::fscanf(inputFile, "%d", &dummy) == EOF) {
std::printf("ERROR : Failed read wall data\n");
std::fclose(inputFile);
return false;
}
}
size_t cnt = 0;
char ch;
while (std::fscanf(inputFile, "%c", &ch) != EOF) {
if ( ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'f')) {
uint8_t wall_bin;
if ('0' <= ch && ch <= '9') wall_bin = ch - '0';
else wall_bin = ch - 'a' + 10;
size_t y = MAZE_SIZE -1 -cnt/MAZE_SIZE;
size_t x = cnt%MAZE_SIZE;
wall[y][x].byte = wall_bin | 0xf0;
cnt++;
}
}
std::fclose(inputFile);
return true;
}
void Maze::loadFromArray(const char asciiData[MAZE_SIZE+1][MAZE_SIZE+1])
{
dirty = true;
for (int i=0;i<MAZE_SIZE;i++) {
for (int j=0;j<MAZE_SIZE;j++) {
char ch = asciiData[MAZE_SIZE-1-i][j];
if ( ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'f')) {
uint8_t wall_bin;
if ('0' <= ch && ch <= '9') wall_bin = ch - '0';
else wall_bin = ch - 'a' + 10;
wall[i][j].byte = wall_bin | 0xf0;
}
}
}
}
void Maze::printWall(const uint8_t value[MAZE_SIZE][MAZE_SIZE]) const
{
bool printValueOn = false;
if (value) printValueOn = true;
for (int y=MAZE_SIZE-1;y>=0;y--) {
for (int x=0;x<MAZE_SIZE;x++) {
std::printf("+");
if(wall[y][x].bits.North) std::printf("----");
else std::printf(" ");
}
std::printf("+\n");
for (int x=0;x<MAZE_SIZE;x++) {
if (wall[y][x].bits.West) std::printf("|");
else std::printf(" ");
std::printf(" ");
if (printValueOn) std::printf("%3u", value[y][x]);
else std::printf(" ");
}
std::printf("|\n");
}
for (int i=0;i<MAZE_SIZE;i++) {
std::printf("-----");
}
std::printf("+\n");
}
void Maze::printWall(const bool value[MAZE_SIZE][MAZE_SIZE]) const
{
bool printValueOn = false;
if (value) printValueOn = true;
for (int y=MAZE_SIZE-1;y>=0;y--) {
for (int x=0;x<MAZE_SIZE;x++) {
std::printf("+");
if(wall[y][x].bits.North) std::printf("----");
else std::printf(" ");
}
std::printf("+\n");
for (int x=0;x<MAZE_SIZE;x++) {
if (wall[y][x].bits.West) std::printf("|");
else std::printf(" ");
std::printf(" ");
if (printValueOn){
if (value[y][x]) std::printf("* ");
else std::printf(" ");
}
else std::printf(" ");
}
std::printf("|\n");
}
for (int i=0;i<MAZE_SIZE;i++) {
std::printf("-----");
}
std::printf("+\n");
}
void Maze::printStepMap() const
{
printWall(stepMap);
}
void Maze::updateWall(const IndexVec &cur, const Direction& newState, bool forceSetDone)
{
//二重書き込みを防ぐ
if (!forceSetDone && wall[cur.y][cur.x].isDoneAll()) return;
dirty = true;
if (forceSetDone) wall[cur.y][cur.x] |= newState | (uint8_t)0xf0;
else wall[cur.y][cur.x] |= newState;
//今のEASTをx+1のWESTに反映
//今のNORTHをy+1のSOUTHに反映
//今のWESTをx-1のEASTに反映
//今のSOUTHをy-1のNORTHに反映
for (int i=0;i<4;i++) {
if (cur.canSum(IndexVec::vecDir[i])) {
IndexVec neighbor(cur + IndexVec::vecDir[i]);
//今のi番目の壁情報ビットとDoneビットを(i+2)%4番目(180度回転方向)に反映
if (forceSetDone) wall[neighbor.y][neighbor.x] |= (0x10 | newState[i]) << (i+2)%4;
else wall[neighbor.y][neighbor.x] |= ((newState[i+4]<<4) | newState[i]) << (i+2)%4;
}
}
}
void Maze::updateStepMap(const IndexVec &dist, bool onlyUseFoundWall)
{
if (!dirty && dist == lastStepMapDist && onlyUseFoundWall == lastOnlyUseFoundWall) return;
lastStepMapDist = dist;
lastOnlyUseFoundWall = onlyUseFoundWall;
dirty = false;
for(size_t i=0;i<MAZE_SIZE;i++) {
for(size_t j=0;j<MAZE_SIZE;j++) {
stepMap[i][j] = 0xff;
}
}
stepMap[dist.y][dist.x] = 0;
std::queue<IndexVec> q;
q.push(dist);
while (!q.empty()) {
const IndexVec cur = q.front();
q.pop();
Direction cur_wall = wall[cur.y][cur.x];
for (int i=0;i<4;i++) {
const IndexVec scanIndex = cur + IndexVec::vecDir[i];
const uint8_t curStep = stepMap[cur.y][cur.x];
if (!cur_wall[i] && stepMap[scanIndex.y][scanIndex.x] > curStep +1) {
//未探索壁をどうするか
if (onlyUseFoundWall && !cur_wall[i+4]) continue;
stepMap[scanIndex.y][scanIndex.x] = curStep +1;
//袋小路でない場合はqueueに入れる
if (wall[scanIndex.y][scanIndex.x].nWall() != 3) {
q.push(scanIndex);
}
}
}
}
}