From e51a3286413149f59328536e24f27396f0e6a8b0 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 24 Jun 2011 12:18:52 -0400 Subject: [PATCH] More implementation for attacking --- gamedata.cpp | 38 ++++++++++++++++++++++---------------- gamedata.h | 5 +++-- graph.cpp | 18 +++++++++++++++++- graph.h | 3 ++- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/gamedata.cpp b/gamedata.cpp index 8357431..ef72b7d 100644 --- a/gamedata.cpp +++ b/gamedata.cpp @@ -32,7 +32,7 @@ void GameData::do_vertex(int x, int y, int r) { if (current != NULL && (MathUtils::distance(current->x, current->y, x, y) - > get_move_radius())) + > get_range())) { select_vertex(x, y); return; @@ -122,26 +122,20 @@ float GameData::calculate_strength(Vertex* node) float GameData::calculate_strength_r(Vertex* node, unsigned int depth, list& visited) { // Find which vertices we need to visit from here - list es = get_vertex_edges(node); + list neighbors = get_neighbors(node); list to_visit; visited.push_back(node); - for (list::iterator cursor = es.begin(); cursor != es.end(); - cursor++) + for (list::iterator cursor = neighbors.begin(); + cursor != neighbors.end(); cursor++) { - Edge e = *cursor; + Vertex* v = *cursor; // if this is true, we haven't visited the vertex on the other end of // this edge yet - if (e.a == node && - find(visited.begin(), visited.end(), e.b) == visited.end()) + if (find(visited.begin(), visited.end(), v) == visited.end()) { - to_visit.push_back(e.b); - } - else if (e.b == node && - find(visited.begin(), visited.end(), e.a) == visited.end()) - { - to_visit.push_back(e.a); + to_visit.push_back(v); } } @@ -169,8 +163,20 @@ float GameData::calculate_strength_r(Vertex* node, unsigned int depth, list Graph::get_colour(int colour) } -list Graph::get_vertex_edges(Vertex* v) +list Graph::get_edges(Vertex* v) { list answer; @@ -151,3 +151,19 @@ list Graph::get_vertex_edges(Vertex* v) return answer; } + + +list Graph::get_neighbors(Vertex* v) +{ + list answer; + + for (list::iterator cursor = edges.begin(); + cursor != edges.end(); cursor++) + { + Edge e = *cursor; + if (e.a == v) answer.push_back(e.b); + else if (e.b == v) answer.push_back(e.a); + } + + return answer; +} diff --git a/graph.h b/graph.h index e94c379..24139b8 100644 --- a/graph.h +++ b/graph.h @@ -49,7 +49,8 @@ class Graph list get_vertices() const { return vertices; } list get_edges() const { return edges; } list get_colour(int colour); - list get_vertex_edges(Vertex* v); + list get_edges(Vertex* v); + list get_neighbors(Vertex* v); bool point_in_vertex(int x, int y, int size); Vertex * vertex_at(int x, int y);