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
| #include <iostream> #include<vector> #include<easyx.h> #include<list> #include"AStart.h" #include"Level.h" #include"Draw.h" using namespace std; class LevelMap:public Level { public: vector<vector<int>> level; virtual bool isValid(Pos v) override { return !((v.x<0||v.y<0)|| v.x> level[v.x].size()||v.y> level.size()|| level[v.y][v.x]==1); } }; void test() { vector < vector<int>> vec = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,1,1,1,0,0,0,0,0,1}, {1,0,1,1,1,1,0,1,0,1,0,0,0,0,1}, {1,0,1,0,1,0,1,0,1,0,0,0,0,0,1}, {1,0,1,1,1,0,1,1,1,0,0,0,0,0,1}, {1,0,0,0,0,0,1,0,1,0,0,0,0,0,1}, {1,0,0,0,0,0,1,1,1,0,0,0,0,0,1}, {1,0,0,1,1,1,1,1,0,0,0,0,0,0,1}, {1,0,0,0,1,1,0,1,0,0,0,0,0,0,1}, {1,0,0,1,0,1,1,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,1,0,1,0,0,0,0,0,0,1}, {1,0,1,0,1,1,0,1,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, }; LevelMap lm; lm.level = vec; AStart a(lm); Draw draw(600, vec); draw.update(); list<Pos> path; Pos start{ -1,-1 }; Pos end{ -1,-1 }; int x; int y; bool flag=false; while (true) { draw.level = lm.level; draw.update(); while (true) { MOUSEMSG msg = GetMouseMsg(); x = msg.x / draw.getSize(); y = msg.y / draw.getSize(); if (msg.uMsg == WM_RBUTTONDOWN) { lm.level[y][x] = draw.level[y][x] = draw.level[y][x] == Draw::road ? Draw::wall : Draw::road; draw.update(); } if (msg.uMsg == WM_LBUTTONDOWN) { if (!flag && draw.level[y][x] == Draw::road) { start.x = x; start.y = y; draw.level[y][x] = Draw::visit; flag = true; draw.update(); } else if (draw.level[y][x] == Draw::road) { end.x = x; end.y = y; draw.level[y][x] = Draw::end; path = a.find(start, end); flag = false; draw.update(); break; } }
} for (const auto& e : path) { draw.level[e.y][e.x] = Draw::visit; draw.update(); } } } int main() { test(); return 0; }
|