-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
61 lines (53 loc) · 2.07 KB
/
Map.cpp
File metadata and controls
61 lines (53 loc) · 2.07 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
#include "Map.h"
#include <iostream>
#include <algorithm>
#include <queue>
#include <utility>
bool Map::isValid(int x, int y) {
return x >= 0 && x < width_ && y >= 0 && y < height_ && grid_[y][x] != 1;
}
std::pair<int,int> Map::findExit() {
// 寻找右下角或最外层的第一个空地作为出口
for (int y = height_-1; y >= 0; --y) {
for (int x = width_-1; x >= 0; --x) {
if (grid_[y][x] == 0 || grid_[y][x] == 2)
return {x, y};
}
}
return {width_-2, height_-2}; // fallback
}
Map::Map(int width, int height, const std::vector<std::string>& layout)
: width_(width), height_(height) {
grid_.resize(height_, std::vector<int>(width_, 0));
explored_.resize(height_, std::vector<bool>(width_, false));
for (int y = 0; y < height_; ++y) {
for (int x = 0; x < width_; ++x) {
if (layout[y][x] == '#') grid_[y][x] = 1; // 墙
else if (layout[y][x] == 'T') grid_[y][x] = 2; // 陷阱
else grid_[y][x] = 0; // 空地
}
}
}
void Map::revealAround(int playerX, int playerY, int vision) {
for (int y = std::max(0, playerY - vision); y <= std::min(height_-1, playerY + vision); ++y) {
for (int x = std::max(0, playerX - vision); x <= std::min(width_-1, playerX + vision); ++x) {
explored_[y][x] = true;
}
}
}
void Map::display(int playerX, int playerY) {
revealAround(playerX, playerY, 2); // 每次显示前更新迷雾
for (int y = 0; y < height_; ++y) {
for (int x = 0; x < width_; ++x) {
if (!explored_[y][x]) std::cout << "?"; // 未探索区域显示 ?
else if (x == playerX && y == playerY) std::cout << "P";
else if (grid_[y][x] == 1) std::cout << "#";
else if (grid_[y][x] == 2) std::cout << "T";
else std::cout << ".";
}
std::cout << std::endl;
}
}
bool Map::isWall(int x, int y) { return grid_[y][x] == 1; }
bool Map::isTrap(int x, int y) { return grid_[y][x] == 2; }
void Map::removeTrap(int x, int y) { if(grid_[y][x]==2) grid_[y][x]=0; }