-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
147 lines (131 loc) · 4.03 KB
/
Game.cpp
File metadata and controls
147 lines (131 loc) · 4.03 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
#include "Game.h"
#include <iostream>
#include <queue>
#include <map>
#include <thread>
#include <chrono>
void Game::autoSolve() {
auto [startX, startY] = std::pair<int,int>(player_.getX(), player_.getY());
auto [goalX, goalY] = currentMap_->findExit();
std::queue<std::pair<int,int>> q;
std::map<std::pair<int,int>, std::pair<int,int>> parent;
q.push({startX, startY});
parent[{startX, startY}] = {-1,-1};
int dirs[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
bool found = false;
while(!q.empty()) {
auto [x,y] = q.front(); q.pop();
if (x == goalX && y == goalY) {
found = true;
break;
}
for (auto& d : dirs) {
int nx = x + d[0], ny = y + d[1];
if (currentMap_->isValid(nx,ny) && !parent.count({nx,ny})) {
parent[{nx,ny}] = {x,y};
q.push({nx,ny});
}
}
}
if (!found) {
std::cout << "No path found!\n";
return;
}
// 回溯路径
std::vector<std::pair<int,int>> path;
for (auto cur = std::pair<int,int>(goalX,goalY);
cur != std::pair<int,int>(-1,-1); cur = parent[cur]) {
path.push_back(cur);
}
std::reverse(path.begin(), path.end());
std::cout << "Auto-walking to exit (" << goalX << "," << goalY << ")...\n";
std::this_thread::sleep_for(std::chrono::milliseconds(800));
// 模拟行走
for (auto [x,y] : path) {
player_ = Player(x, y, player_.getHP());
system("clear");
currentMap_->display(player_.getX(), player_.getY());
std::cout << "HP: " << player_.getHP() << std::endl;
std::cout << "[Auto walking...]\n";
if (currentMap_->isTrap(x,y)) {
player_.takeDamage(2);
currentMap_->removeTrap(x,y);
std::cout << "Ouch! Trap hit! HP-2\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(300));
if (player_.getHP() <= 0) {
std::cout << "Game Over during auto-walk!\n";
return;
}
}
std::cout << "🎉 You reached the exit safely!\n";
}
Game::Game() {
// 两张示例地图
maps_.push_back(Map(15, 10, {
"###############",
"#.....T.......#",
"#.#####.#####.#",
"#.............#",
"#..###..###...#",
"#..#.....#....#",
"#..#####.#....#",
"#.....T.......#",
"#.............#",
"###############"
}));
maps_.push_back(Map(15, 10, {
"###############",
"#T............#",
"#.#####.#####.#",
"#.....T.......#",
"#..###..###...#",
"#..#.....#....#",
"#..#####.#....#",
"#.............#",
"#....T........#",
"###############"
}));
}
void Game::showMenu() {
std::cout << "Select a map:\n1. Map1\n2. Map2\n> ";
int choice; std::cin >> choice;
if(choice<1 || choice>maps_.size()) choice = 1;
currentMap_ = &maps_[choice-1];
player_ = Player(1,1,10);
}
void Game::gameLoop() {
std::string input;
while(player_.getHP()>0) {
system("clear");
currentMap_->display(player_.getX(), player_.getY());
std::cout << "HP: " << player_.getHP() << std::endl;
std::cout << "Move (w/a/s/d) or type 'auto' to auto-solve: ";
std::cin >> input;
if (input == "auto") {
autoSolve();
return;
} else if (input.size() == 1)
handleInput(input[0]);
}
std::cout << "Game Over!\n";
}
void Game::handleInput(char input) {
int dx=0, dy=0;
if(input=='w') dy=-1;
else if(input=='s') dy=1;
else if(input=='a') dx=-1;
else if(input=='d') dx=1;
int newX = player_.getX()+dx;
int newY = player_.getY()+dy;
if(!currentMap_->isWall(newX,newY)) player_.move(dx,dy);
if(currentMap_->isTrap(player_.getX(),player_.getY())) {
player_.takeDamage(2);
currentMap_->removeTrap(player_.getX(),player_.getY());
std::cout << "Ouch! Trap hit! HP-2\n";
}
}
void Game::run() {
showMenu();
gameLoop();
}