Separated game-specific graph behavior into a subclass, implemented 2 players alternating turns
This commit is contained in:
76
graph.cpp
76
graph.cpp
@ -2,9 +2,9 @@
|
||||
#include "mathutils.h"
|
||||
#include "debug.h"
|
||||
|
||||
Graph::Graph()
|
||||
Graph::Graph(bool planar)
|
||||
{
|
||||
current_vertex = NULL;
|
||||
this->planar = planar;
|
||||
}
|
||||
|
||||
Graph::~Graph()
|
||||
@ -17,7 +17,7 @@ Graph::~Graph()
|
||||
}
|
||||
}
|
||||
|
||||
bool Graph::vertex_present(int x, int y, int r)
|
||||
bool Graph::point_in_vertex(int x, int y, int r)
|
||||
{
|
||||
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||
cursor != vertices.end(); cursor++)
|
||||
@ -30,6 +30,19 @@ bool Graph::vertex_present(int x, int y, int r)
|
||||
}
|
||||
|
||||
|
||||
Vertex * Graph::vertex_at(int x, int y)
|
||||
{
|
||||
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||
cursor != vertices.end(); cursor++)
|
||||
{
|
||||
Vertex* v = *cursor;
|
||||
if (MathUtils::distance(v->x, v->y, x, y) <= v->r) return v;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
bool Graph::vertex_would_overlap(int x, int y, int r)
|
||||
{
|
||||
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||
@ -58,30 +71,7 @@ bool Graph::crosses_edge(Edge e)
|
||||
}
|
||||
|
||||
|
||||
void Graph::do_vertex(int x, int y, int r, int colour)
|
||||
{
|
||||
if (vertex_present(x, y, r)) select_vertex(x, y);
|
||||
else add_vertex(x, y, r, colour);
|
||||
}
|
||||
|
||||
|
||||
Vertex* Graph::select_vertex(int x, int y)
|
||||
{
|
||||
for (list<Vertex*>::iterator cursor = vertices.begin();
|
||||
cursor != vertices.end(); cursor++)
|
||||
{
|
||||
Vertex* v = *cursor;
|
||||
if (MathUtils::distance(v->x, v->y, x, y) <= v->r)
|
||||
{
|
||||
current_vertex = v
|
||||
return;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void Graph::add_vertex(int x, int y, int r, int colour)
|
||||
bool Graph::add_vertex(int x, int y, int r, int colour, Vertex* src)
|
||||
{
|
||||
Vertex* v = new Vertex(x, y, r, colour);
|
||||
|
||||
@ -92,36 +82,34 @@ void Graph::add_vertex(int x, int y, int r, int colour)
|
||||
fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to vertex collision: x=%d, y=%d, r=%d\n", v->x, v->y, v->r);
|
||||
#endif
|
||||
delete v;
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (current_vertex == NULL)
|
||||
if (src != NULL)
|
||||
{
|
||||
current_vertex = v;
|
||||
vertices.push_back(v);
|
||||
return;
|
||||
}
|
||||
Edge e;
|
||||
e.a = src;
|
||||
e.b = v;
|
||||
|
||||
Edge e;
|
||||
e.a = current_vertex;
|
||||
e.b = v;
|
||||
|
||||
if (crosses_edge(e))
|
||||
{
|
||||
if (planar && crosses_edge(e))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to edge collision: x1=%d, y1=%d, x2=%d, y2=%d\n", e.a->x, e.a->y, e.b->x, e.b->y);
|
||||
fprintf(stderr, "debug: Graph::add_vertex(): failed to add due to edge collision: x1=%d, y1=%d, x2=%d, y2=%d\n", e.a->x, e.a->y, e.b->x, e.b->y);
|
||||
#endif
|
||||
delete v;
|
||||
return;
|
||||
delete v;
|
||||
return false;
|
||||
}
|
||||
|
||||
edges.push_back(e);
|
||||
}
|
||||
|
||||
vertices.push_back(v);
|
||||
edges.push_back(e);
|
||||
current_vertex = v;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "debug: Graph::add_vertex(): added: x=%d, y=%d, r=%d\n", v->x, v->y, v->r);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user