Separated game-specific graph behavior into a subclass, implemented 2 players alternating turns
This commit is contained in:
76
gamedata.cpp
Normal file
76
gamedata.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "gamedata.h"
|
||||
#include "mathutils.h"
|
||||
|
||||
int GameData::PLAYER1_COLOUR = 0x4a483f;
|
||||
int GameData::PLAYER2_COLOUR = 0x090c7a;
|
||||
|
||||
GameData::GameData()
|
||||
: Graph(true)
|
||||
{
|
||||
current = NULL;
|
||||
player = PLAYER1;
|
||||
}
|
||||
|
||||
GameData::~GameData() { }
|
||||
|
||||
void GameData::toggle_turn()
|
||||
{
|
||||
if (player == PLAYER1) player = PLAYER2;
|
||||
else if (player == PLAYER2) player = PLAYER1;
|
||||
}
|
||||
|
||||
|
||||
void GameData::do_vertex(int x, int y, int r)
|
||||
{
|
||||
int colour;
|
||||
if (player == PLAYER1) colour = PLAYER1_COLOUR;
|
||||
if (player == PLAYER2) colour = PLAYER2_COLOUR;
|
||||
|
||||
if (point_in_vertex(x, y, r)) select_vertex(x, y);
|
||||
else add_vertex(x, y, r, colour);
|
||||
}
|
||||
|
||||
|
||||
void GameData::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) &&
|
||||
(v->colour == PLAYER1_COLOUR && player == PLAYER1 ||
|
||||
v->colour == PLAYER2_COLOUR && player == PLAYER2))
|
||||
{
|
||||
current = v;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool GameData::add_vertex(int x, int y, int r, int colour)
|
||||
{
|
||||
// this is the special case for adding the first vertex for each player
|
||||
if (current == NULL)
|
||||
{
|
||||
if ((player == PLAYER1 && !player1_played) ||
|
||||
(player == PLAYER2 && !player2_played))
|
||||
{
|
||||
Graph::add_vertex(x, y, r, colour);
|
||||
if (player == PLAYER1) player1_played = true;
|
||||
if (player == PLAYER2) player2_played = true;
|
||||
toggle_turn();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Graph::add_vertex(x, y, r, colour, current))
|
||||
{
|
||||
clear_current_vertex();
|
||||
toggle_turn();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
Reference in New Issue
Block a user