-
Notifications
You must be signed in to change notification settings - Fork 5
/
boardnode.cpp
100 lines (84 loc) · 2.34 KB
/
boardnode.cpp
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
#include "include/boardnode.h"
#include <vector>
#include <assert.h>
#include <utility>
using namespace std;
namespace Catan {
namespace Generate {
int BoardNode::NumNeighbours() {
return NEIGHBOUR_COUNT;
}
vector<BoardNode*> BoardNode::NonNullNeighbours() {
vector<BoardNode*> nodes;
for (int i = 0; i < NEIGHBOUR_COUNT; i++) {
if (neighbours[i] != NULL) {
nodes.push_back(neighbours[i]);
}
}
return nodes;
}
bool BoardNode::CanPlaceChit() {
return type != DESERT && type != WATER;
}
void BoardNode::ClearPorts() {
for (int i = 0; i < NEIGHBOUR_COUNT; i++) {
Port *port = ports[i];
if (port != NULL) {
port->attachedNode = NULL;
ports[i] = NULL;
}
}
}
bool BoardNode::IsWater(BoardNode *node) {
return node == NULL || node->type == WATER;
}
ShoreEdge *BoardNode::GenerateShoreLine() {
// Find the first edge next to water/null
ShoreEdge *currentEdge = NULL;
ShoreEdge *firstEdge = NULL;
BoardNode *currentNode = this;
int currentIndex = 0;
for (int i = 0; i < NEIGHBOUR_COUNT; i++) {
if (IsWater(neighbours[i])) {
currentEdge = new ShoreEdge(this, i);
firstEdge = currentEdge;
currentIndex = i;
}
}
while (true) {
// Get next edge
currentIndex = (currentIndex + 1) % NEIGHBOUR_COUNT;
// Need to switch to new node, because we can't go this way.
if (!IsWater(currentNode->neighbours[currentIndex])) {
currentNode = currentNode->neighbours[currentIndex];
currentIndex = (currentIndex + 3) % NEIGHBOUR_COUNT;
} else if (firstEdge->attachedNode == currentNode && firstEdge->index == currentIndex){
currentEdge->next = firstEdge;
firstEdge->prev = currentEdge;
break;
} else {
// Generate Edge
ShoreEdge *nextEdge = new ShoreEdge(currentNode, currentIndex);
currentEdge->next = nextEdge;
nextEdge->prev = currentEdge;
currentEdge = nextEdge;
}
}
return firstEdge;
}
void BoardNode::AddPort(int index, Port *port) {
ports[index] = port;
port->attachedNode = this;
}
vector<pair<Port*, int>> BoardNode::NonNullPorts() {
vector<pair<Port*, int>> nonNullPorts;
for (int i = 0; i < NEIGHBOUR_COUNT; i++) {
Port *port = ports[i];
if (port != NULL) {
nonNullPorts.push_back(make_pair(port, i));
}
}
return nonNullPorts;
}
}
}