-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashiState.cpp
119 lines (90 loc) · 4.06 KB
/
HashiState.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "HashiState.hpp"
#include <iostream>
using namespace std;
bool operator==(const HashiState& memory1, const HashiState& memory2){
bool out;
for(int i=0; i<memory1.NumberOfCells; ++i){
out = out && memory1.Memory[i] == memory2.Memory[i];
}
return out;
}
HashiState::HashiState(HashiGrid* hashi){
// We assume there will never be grid with 0 islands
uint parity = hashi->NumberOfIslands % 2 == 0 ? 0 : 1;
NumberOfCells = hashi->NumberOfIslands / 2 + parity;
Memory = (HashiMemoryCell*) malloc(NumberOfCells);
for(int i=0; i<NumberOfCells; ++i){
Memory[i].island1Bottom = HASHI_MEMORY_WATER;
Memory[i].island1Right = HASHI_MEMORY_WATER;
Memory[i].island2Bottom = HASHI_MEMORY_WATER;
Memory[i].island2Right = HASHI_MEMORY_WATER;
}
}
HashiState::HashiState(HashiState* source){
NumberOfCells = source->NumberOfCells;
Memory = (HashiMemoryCell*) malloc(NumberOfCells);
for(int i=0; i<NumberOfCells; ++i){
Memory[i] = source->Memory[i];
}
}
HashiState::~HashiState(){
free(Memory);
}
void HashiState::PrettyPrint(){
for(int i=0; i<NumberOfCells; ++i){
string type[4];
type[0] = Memory[i].island1Bottom == HASHI_MEMORY_WATER ? "WATER" :
Memory[i].island1Bottom == HASHI_MEMORY_SIMPLE ? "SIMPLE" :
Memory[i].island1Bottom == HASHI_MEMORY_DOUBLE ? "DOUBLE" : "UNKNOWN";
type[1] = Memory[i].island1Bottom == HASHI_MEMORY_WATER ? "WATER" :
Memory[i].island1Bottom == HASHI_MEMORY_SIMPLE ? "SIMPLE" :
Memory[i].island1Bottom == HASHI_MEMORY_DOUBLE ? "DOUBLE" : "UNKNOWN";
type[2] = Memory[i].island1Bottom == HASHI_MEMORY_WATER ? "WATER" :
Memory[i].island1Bottom == HASHI_MEMORY_SIMPLE ? "SIMPLE" :
Memory[i].island1Bottom == HASHI_MEMORY_DOUBLE ? "DOUBLE" : "UNKNOWN";
type[3] = Memory[i].island1Bottom == HASHI_MEMORY_WATER ? "WATER" :
Memory[i].island1Bottom == HASHI_MEMORY_SIMPLE ? "SIMPLE" :
Memory[i].island1Bottom == HASHI_MEMORY_DOUBLE ? "DOUBLE" : "UNKNOWN";
cout << "Island " << i*2 << " has value bottom : " << type[0] << " and right :" << type[1] << endl;
cout << "Island " << i*2+1 << " has value bottom : " << type[2] << " and right :" << type[3] << endl;
}
}
// Islands have to be either horizontaly aligned or verticaly aligned
Island* HashiState::GetTopLeftIsland(Island* island1, Island* island2, bool* horizontalBridge){
GridCoords island1Coords = island1->Coords;
GridCoords island2Coords = island2->Coords;
*horizontalBridge = island1Coords.i == island2Coords.i;
if(*horizontalBridge){
if(island1Coords.i < island2Coords.i) return island1;
return island2;
} else {
if(island1Coords.j < island2Coords.j) return island1;
return island2;
}
}
void HashiState::UpdateByBuild(Bridge bridge){
// We search the most top-left island
bool horizontalBridge = false;
Island* islandToUpdate = GetTopLeftIsland(bridge.island1, bridge.island2, &horizontalBridge);
uint shift = islandToUpdate->ID % 2 == 0;
if(shift){
if(horizontalBridge) ++Memory[islandToUpdate->ID / 2].island2Right;
else ++Memory[islandToUpdate->ID / 2].island2Bottom;
} else{
if(horizontalBridge) ++Memory[islandToUpdate->ID / 2].island1Right;
else ++Memory[islandToUpdate->ID / 2].island1Bottom;
}
}
void HashiState::UpdateByDestroy(Bridge bridge){
// We search the most top-left island
bool horizontalBridge = false;
Island* islandToUpdate = GetTopLeftIsland(bridge.island1, bridge.island2, &horizontalBridge);
uint shift = islandToUpdate->ID % 2 == 0;
if(shift){
if(horizontalBridge) --Memory[islandToUpdate->ID / 2].island2Right;
else --Memory[islandToUpdate->ID / 2].island2Bottom;
} else{
if(horizontalBridge) --Memory[islandToUpdate->ID / 2].island1Right;
else --Memory[islandToUpdate->ID / 2].island1Bottom;
}
}