-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtents.h
158 lines (127 loc) · 4.34 KB
/
tents.h
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef __TENTS_H__
#define __TENTS_H__
#include <string>
#include <tuple>
#include <utility>
#include <vector>
enum class CellState {
EMPTY,
TREE,
TENT
};
using uint = unsinged int;
/*
* Custom comparator to find already existing variables, but only looking
* at the first two parameters of the tuple.
*/
struct compareVar
{
/* Local copy of the variable we'll be looking for. */
std::tuple<uint, uint, uint, uint, int> var;
/* Constructor to set the variable we will compare other variables to. */
compareVar(const std::tuple<uint, uint, uint, uint, int>& var): var(var) {}
/* Check if the variables have the same position on the board. */
bool operator()(const std::tuple<uint, uint, uint, uint, int>& var)
{
return (std::get<0>(var) == std::get<0>(this->var)
&& std::get<1>(var) == std::get<1>(this->var));
}
}
class Tents {
private:
uint rows;
uint cols;
bool valid;
/* The game board is a matrix of size rows x cols. */
std::vector<std::vector<CellState> > board;
/* Row and column restrictions. */
std::vector<uint> rowRestrictions;
std::vector<uint> colRestrictions;
/*
* Variables for the SAT solver. The first two parameters denote the row
* and column position of the variable, the next two parameters denote the
* row and column position of the adjacent tree and the last parameter is
* the variable number that is used in the SAT solver.
*/
std::vector<std::tuple<uint, uint, uint, uint, int> > variables;
/* The variables for every row and column. */
std::vector<std::vector<int> > rowVariables;
std::vector<std::vector<int> > colVariables;
/* The clauses for the SAT solver. The inner vector is a single clause. */
std::vector<std::vector<int> > clauses;
/*
* isTentPlacable(row, col):
* Checks if a tent can be placed on the cell denoted by row ${row} and column
* ${col}. Returns true if it is possible, false otherwise.
*/
bool isTentPlacable(uint, uint);
/*
* getPossibleTents(row, col):
* Returns the coordinates of possible tents that are orthogonally adjacent to
* the tree denoted by row ${row} and column ${column}.
*/
std::vector<std::pair<uint, uint> > getPossibleTents(uint, uint);
/*
* buildTreeClauses(numbers):
* Builds the clauses for a tree. The variable numbers used in the clauses are
* taken from the ${numbers} list.
*/
void buildTreeClauses(const std::vector<int>&);
/*
* areSouthEastAdjacent(v1, v2):
* Checks if the tree denoted by variable ${v1} is adjacent to the tree denoted
* by variable ${v2} in any of the following directions: east, south, south
* east. Returns true if they are adjacent, false otherwise.
*/
bool areSouthEastAdjacent(const std::tuple<uint, uint, uint, uint, uint>&,
const std::tuple<uint, uint, uint, uint, uint>&);
/*
* buildClauses():
* Builds the clauses for the SAT solver.
*/
void buildClauses();
public:
/*
* Tents(rows, cols):
* Initialize the board of size ${rows} x ${cols}. At the beginning
* every cell is empty.
*/
Tents(uint, uint);
/*
* Tents(str):
* Initialize the board with initialization string ${str}.
* If unsuccessful, method ${isValid} will return false.
*/
Tents(const std::string &str);
/*
* isValid():
* Check if all required game parameters have been set successfully.
*/
bool isValid();
/*
* setRowRestriction(rowRestrictions):
* Initialize row restrictions. Returns true if successfull,
* false otherwise.
*/
bool setRowRestrictions(const std::vector<uint>&);
/*
* setColRestriction(rowRestrictions):
* Initialize column restrictions. Returns true if successfull,
* false otherwise.
*/
bool setColRestrictions(const std::vector<uint>&);
/*
* placeTree(row, col):
* If the cell denoted by row ${row} and column ${col} is empty,
* place a tree on that cell. Returns true if successfull,
* false otherwise.
*/
bool placeTree(uint, uint);
/*
* removeTree(row, col):
* Remove tree on cell denoted by row ${row} and column ${col}.
* Returns true if successfull, false otherwise.
*/
bool removeTree(uint, uint);
};
#endif