#include "Graph.hpp" #include #define MAX 100 using namespace std; //城堡名数组 char vertexName[MAX]; class Warrior { private: int hp; int wealth; char pos; public: Warrior() { hp = 100; wealth = 0; pos = 'A'; } bool isWin() { return pos == 'P'; } bool isDie() { return this->hp < 1; }; void changeHp(int loss) { hp -= loss; } void changeWealth(int gain) { wealth += gain; } int gethp() { return hp; } int getWealth() { return wealth; } char getPos() { return pos; } void show() { cout << "---------------------------------------" << endl << "当前勇士状态 | 血量:" << hp << " 财富值:" << wealth << endl << "---------------------------------------" << endl; } void move(char des, int loss, int gain) { pos = des; this->changeHp(loss); this->changeWealth(gain); } }; class Game { public: void StartGame(Graph g); void HighestHP(Graph g); void HighestWealth(Graph g); void RouteSelect(Graph g); void MainLoop(Graph); }; void Game::StartGame(Graph g) { Warrior warrior; system("cls"); while ((!warrior.isDie()) && (!warrior.isWin())) { char des; cout << "当前在城堡 "<> des; if (des >= 'a' && des <= 'z') des -= 32; //读取小写字母 while (des < 'A' || des>'P') { cout << "没有该城堡,请重新输入:"; cin >> des; if (des >= 'a' && des <= 'z') des -= 32; } while (!g.adjacent(des, warrior.getPos())) { cout << "没有去往" << des << "城堡的路,请重新输入:"; cin >> des; if (des >= 'a' && des <= 'z') des -= 32; } while (des == warrior.getPos()) { cout << "不可以停下不走!请重新输入:"; cin >> des; if (des >= 'a' && des <= 'z') des -= 32; } warrior.move(des, g.get_weight(warrior.getPos(), des), g.get_wealth(des)); warrior.show(); } if (warrior.isDie()) cout << "游戏失败,不要灰心,重新再来!" << endl; if (warrior.isWin()) { system("cls"); cout << endl << "游戏胜利!感谢游玩!最终勇者状态:" << endl; warrior.show(); } system("Pause"); } void Game::HighestHP(Graph g) { cout << "最佳生命值路径结果如下:" << endl; g.dijkstra('A'); /*auto = g.dijkstra('A'); vector vertices = g.get_vertices(); for (auto vertex : vertices) if (dis[vertex] >= 0) cout <<"到城堡" << vertex << "的最佳生命值: " << 100-dis[vertex] << endl;*/ system("Pause"); } void Game::HighestWealth(Graph g) { char route[15], cur_pos = 'A'; cout << "A -> "; int wealth = 0; while (cur_pos != 'O') { string neighbors = g.get_neighbours(cur_pos); unsigned int len = neighbors.size(); int max_weal = 0; char max_weal_pos; for (int i = 0; i < len; i++) { if (g.get_wealth(neighbors[i]) > max_weal && (neighbors[i]!=cur_pos)) { max_weal = g.get_wealth(neighbors[i]); max_weal_pos = neighbors[i]; } } cout << max_weal_pos << " -> "; cur_pos = max_weal_pos; wealth += max_weal; } cout << "P" << endl << "最高财富值:" << wealth << endl; system("Pause"); } void Game::RouteSelect(Graph g) { int choice = 1; while (choice) { system("cls"); cout << "===最优路线寻找===" << endl << "1.生命值最高" << endl << "2.财富值最高" << endl << "0.返回上层子菜单" << endl << "请输入:"; cin >> choice; switch (choice) { case 0:choice = 0; break; case 1:this->HighestHP(g); break; case 2:this->HighestWealth(g); break; default:cout << "输入有误,请重新输入!" << endl; break; } } } void Game::MainLoop(Graph g) { int choice = 1; while (choice) { system("cls"); cout << " 欢迎来到闯关夺宝游戏, 地图数据已读取,游戏地图如下(括号内为财富值):" << endl << "-----------------------------------------------------------------------------------------------------" << endl; g.show(); cout << "勇士初始在A城堡,到达P城堡即胜利,若HP归零则失败。" << endl; cout << "-----------------------------------------------------------------------------------------------------" << endl << "-----------游戏玩法------------" << endl << "1.开始游戏" << endl << "2.最优路线" << endl << "0.离开游戏" << endl << "请选择:"; cin >> choice; switch (choice) { case 1: this->StartGame(g); break; case 2: this->RouteSelect(g); break; case 0: exit(0); default: cout << "输入有误,游戏结束。"; break; } } } void DrawGraph(Graph &g) { //城堡数,路径数 int vertexNum, edgeNum; //打开文件 ifstream mapFile("map.txt", ios::in); if (!mapFile.is_open()) { cerr << "打开文件错误!" << endl; } //读入数据 else { mapFile >> vertexNum >> edgeNum; } //读入城堡名及财富值 for (int i = 0; i < vertexNum; i++) { mapFile >> vertexName[i] >> g.wealth[i]; } //添加顶点到图 for (int i = 0; i < vertexNum; i++) { g.add_vertex(vertexName[i]); } //添加边到图 for (int i = 0; i < edgeNum; i++) { int vertex1, vertex2, edge; mapFile >> vertex1 >> vertex2 >> edge; g.add_edge(vertexName[vertex1], vertexName[vertex2], edge); } } int main() { Graph g; DrawGraph(g); //test01(g); Game game; game.MainLoop(g); return 0; }