First stab at the calculate_strength() function - all it nets us is a floating point exception when compiling with -DDEBUG
This commit is contained in:
parent
5205b9dfab
commit
977322d4ee
69
gamedata.cpp
69
gamedata.cpp
|
@ -1,5 +1,10 @@
|
||||||
#include "gamedata.h"
|
#include "gamedata.h"
|
||||||
#include "mathutils.h"
|
#include "mathutils.h"
|
||||||
|
#include "debug.h"
|
||||||
|
#include <list>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using std::list;
|
||||||
|
|
||||||
int GameData::PLAYER1_COLOUR = 0x4a483f;
|
int GameData::PLAYER1_COLOUR = 0x4a483f;
|
||||||
int GameData::PLAYER2_COLOUR = 0x090c7a;
|
int GameData::PLAYER2_COLOUR = 0x090c7a;
|
||||||
|
@ -58,6 +63,10 @@ bool GameData::add_vertex(int x, int y, int r, int colour)
|
||||||
(player == PLAYER2 && !player2_played))
|
(player == PLAYER2 && !player2_played))
|
||||||
{
|
{
|
||||||
Graph::add_vertex(x, y, r, colour, 10);
|
Graph::add_vertex(x, y, r, colour, 10);
|
||||||
|
#ifdef DEBUG
|
||||||
|
fprintf(stderr, "debug: GameData::add_vertex(): strength=%d\n",
|
||||||
|
calculate_strength(*(vertices.rbegin())));
|
||||||
|
#endif
|
||||||
if (player == PLAYER1) player1_played = true;
|
if (player == PLAYER1) player1_played = true;
|
||||||
if (player == PLAYER2) player2_played = true;
|
if (player == PLAYER2) player2_played = true;
|
||||||
toggle_turn();
|
toggle_turn();
|
||||||
|
@ -68,9 +77,69 @@ bool GameData::add_vertex(int x, int y, int r, int colour)
|
||||||
|
|
||||||
if (Graph::add_vertex(x, y, r, colour, 10, current))
|
if (Graph::add_vertex(x, y, r, colour, 10, current))
|
||||||
{
|
{
|
||||||
|
#ifdef DEBUG
|
||||||
|
fprintf(stderr, "debug: GameData::add_vertex(): strength=%d\n",
|
||||||
|
calculate_strength(*(vertices.rbegin())));
|
||||||
|
#endif
|
||||||
|
|
||||||
clear_current_vertex();
|
clear_current_vertex();
|
||||||
toggle_turn();
|
toggle_turn();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Oh the recursive recursion!
|
||||||
|
unsigned int GameData::calculate_strength(Vertex* node, unsigned int depth, list<Vertex*>* visited)
|
||||||
|
{
|
||||||
|
if (visited == NULL) visited = new list<Vertex*>;
|
||||||
|
visited->push_back(node);
|
||||||
|
|
||||||
|
list<Vertex*> all_nodes = get_colour(node->colour);
|
||||||
|
|
||||||
|
// Special case - a one-node tree just returns its own score!
|
||||||
|
if (all_nodes.size() == 1) return node->score;
|
||||||
|
|
||||||
|
|
||||||
|
// Find which vertices we need to visit from here
|
||||||
|
list<Edge> es = get_vertex_edges(node);
|
||||||
|
list<Vertex*> to_visit;
|
||||||
|
|
||||||
|
for (list<Edge>::iterator cursor = es.begin(); cursor != es.end();
|
||||||
|
cursor++)
|
||||||
|
{
|
||||||
|
Edge e = *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())
|
||||||
|
{
|
||||||
|
to_visit.push_back(e.b);
|
||||||
|
}
|
||||||
|
else if (e.a == node &&
|
||||||
|
find(visited->begin(), visited->end(), e.b) == visited->end())
|
||||||
|
{
|
||||||
|
to_visit.push_back(e.a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is the base case - this node has no unvisited neighbors
|
||||||
|
if (to_visit.empty()) return node->score / depth;
|
||||||
|
|
||||||
|
// Else, iterate through to_visit and visit them all, summing their
|
||||||
|
// effective strengths adjusted for depth.
|
||||||
|
// Since our trees are acyclic, this can't loop.
|
||||||
|
int modscore = node->score;
|
||||||
|
if (depth > 0) modscore /= depth;
|
||||||
|
|
||||||
|
for (list<Vertex*>::iterator cursor = to_visit.begin();
|
||||||
|
cursor != to_visit.end(); cursor++)
|
||||||
|
{
|
||||||
|
Vertex* v = *cursor;
|
||||||
|
modscore += calculate_strength(v, depth+1, visited);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (depth == 0) delete visited;
|
||||||
|
return modscore;
|
||||||
|
}
|
||||||
|
|
|
@ -29,6 +29,8 @@ class GameData : public Graph
|
||||||
bool add_vertex(int x, int y, int r, int colour);
|
bool add_vertex(int x, int y, int r, int colour);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
unsigned int calculate_strength(Vertex* node, unsigned int depth = 0, list<Vertex*>* visited = NULL);
|
||||||
|
|
||||||
Vertex* current;
|
Vertex* current;
|
||||||
Turn player;
|
Turn player;
|
||||||
bool player1_played;
|
bool player1_played;
|
||||||
|
|
32
graph.cpp
32
graph.cpp
|
@ -106,7 +106,7 @@ bool Graph::add_vertex(int x, int y, int r, int colour, int score, Vertex* src)
|
||||||
vertices.push_back(v);
|
vertices.push_back(v);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf(stderr, "debug: Graph::add_vertex(): added: x=%d, y=%d, r=%d\n", v->x, v->y, v->r);
|
fprintf(stderr, "debug: Graph::add_vertex(): added: x=%d, y=%d, r=%d, score=%d\n", v->x, v->y, v->r, v->score);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -121,3 +121,33 @@ Vertex::Vertex(int x, int y, int r, int colour, int score)
|
||||||
this->colour = colour;
|
this->colour = colour;
|
||||||
this->score = score;
|
this->score = score;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
list<Vertex*> Graph::get_colour(int colour)
|
||||||
|
{
|
||||||
|
list<Vertex*> answer;
|
||||||
|
|
||||||
|
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||||
|
cursor != vertices.end(); cursor++)
|
||||||
|
{
|
||||||
|
Vertex* v = *cursor;
|
||||||
|
if (v->colour == colour) answer.push_back(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
list<Edge> Graph::get_vertex_edges(Vertex* v)
|
||||||
|
{
|
||||||
|
list<Edge> answer;
|
||||||
|
|
||||||
|
for (list<Edge>::iterator cursor = edges.begin();
|
||||||
|
cursor != edges.end(); cursor++)
|
||||||
|
{
|
||||||
|
Edge e = *cursor;
|
||||||
|
if (e.a == v || e.b == v) answer.push_back(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
2
graph.h
2
graph.h
|
@ -48,6 +48,8 @@ class Graph
|
||||||
|
|
||||||
list<Vertex*> get_vertices() const { return vertices; }
|
list<Vertex*> get_vertices() const { return vertices; }
|
||||||
list<Edge> get_edges() const { return edges; }
|
list<Edge> get_edges() const { return edges; }
|
||||||
|
list<Vertex*> get_colour(int colour);
|
||||||
|
list<Edge> get_vertex_edges(Vertex* v);
|
||||||
|
|
||||||
bool point_in_vertex(int x, int y, int size);
|
bool point_in_vertex(int x, int y, int size);
|
||||||
Vertex * vertex_at(int x, int y);
|
Vertex * vertex_at(int x, int y);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user