-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
315 lines (283 loc) · 6.67 KB
/
board.cpp
File metadata and controls
315 lines (283 loc) · 6.67 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "board.h"
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
//Constraints constructor
constraint::constraint(int aRow, int aCol, string a_C){
row = aRow;
col = aCol;
c = a_C;
}
//Board constructor
Board::Board(vector<int> aRows, vector<int> aCols){
rows = aRows.size();
cols = aCols.size();
row_values = aRows;
col_values = aCols;
filled = new bool*[rows*2];
for(int r = 0; r < rows*2; r++){
filled[r] = new bool[cols*2];
for(int c = 0; c < cols*2; c++){
filled[r][c] = false;
}
}
}
//Board copy constructor
Board::Board(Board* &b){
rows = b->getRows();
cols = b->getCols();
row_values = b->getRowValues();
col_values = b->getColValues();
ships = b->ships;
constraints = b->constraints;
filled = b->filled;
}
//Returns number of rows
int Board::getRows(){
return rows;
}
//Returns number of columns
int Board::getCols(){
return cols;
}
//Checks if the point passed in is already occupied by another ship
bool Board::isFilled(int row, int col){
if(row < 0 || row >= rows || col >= cols || col <0){
return false;
}
if(filled[row][col]){
return true;
}
return false;
}
//Returns the column value for a given column
int Board::getColValue(int col){
return col_values[col];
}
//Returns the row value for a given row
int Board::getRowValue(int row){
return row_values[row];
}
//Sets the row value to a number at a given row
void Board::setRowValue(int row, int val){
row_values[row] = val;
}
//Sets the column value to a number at a given column
void Board::setColValue(int col, int val){
col_values[col] = val;
}
//Returns the vector of all the row values
vector<int> Board::getRowValues(){
return row_values;
}
//Returns the vector of all the column values
vector<int> Board::getColValues(){
return col_values;
}
//Prints out the board in correct format
void Board::print(){
cout << "Solution:\n";
for(Ship& s: ships){
int row = s.getRow();
int col = s.getCol();
string t = s.getType();
cout << t << " " << row << " " <<col << " ";
if(s.getSize() > 1){
if(s.isHoriz){
cout << "horizontal";
}else{
cout << "vertical";
}
}
cout << "\n";
}
cout << "+ ";
for(int i = 0; i < getCols(); i++){
cout << i << " ";
}
cout << "\n";
for(int y = 0; y < rows; y++){
cout << y << " ";
for(int x = 0; x < cols; x++){
if(isFilled(y,x)){
cout << "X ";
}else{
cout << ". ";
}
}
cout << "| " ;
cout<< "\n";
}
cout << " ";
for(int i = 0; i < getCols()*2; i++){
cout << "_" ;
}
cout << "\n\n";
}
//Adjusts the boards values after a ship is put in
void Board::updateValues(const Ship& s){
for(const XYPoint& p: s.points){
row_values[p.y] -=1;
col_values[p.x] -=1;
filled[p.y][p.x] = true;
}
}
//Undos the boards values after recursion is called
void Board::reverseValues(const Ship& s){
for(const XYPoint& p: s.points){
row_values[p.y] +=1;
col_values[p.x] +=1;
filled[p.y][p.x] = false;
}
}
//Checks all of the points around each point of a ship returns true if surroundings are clear
bool Board::check_surroundings(Ship curr_ship){
bool fits = true;
for(const XYPoint& p: curr_ship.points){
int row = p.y;
int col = p.x;
if(isFilled(row,col)){
fits = false;
}else if(isFilled(row-1,col)){
fits = false;
}else if(isFilled(row,col-1)){
fits = false;
}else if(isFilled(row-1,col-1)){
fits = false;
}else if(isFilled(row-1, col+1)){
fits =false;
}else if(isFilled(row,col+1)){
fits= false;
}else if(isFilled(row+1,col+1)){
fits = false;
}else if(isFilled(row+1,col)){
fits =false;
}else if(isFilled(row+1,col-1)){
fits =false;
}
}
return fits;
}
//Checks if the a horizontal or vertical ship can fit in the board
bool Board::fits(Ship curr_ship){
bool fits = false;
bool fits_in_surroundings = check_surroundings(curr_ship);
if(curr_ship.isHoriz && fits_in_surroundings){
fits = true;
for(const XYPoint& p: curr_ship.points){
if(getColValue(p.x) == 0){
fits = false;
}
}
if(fits){
return true;
}
}
if(!curr_ship.isHoriz && fits_in_surroundings){
fits = true;
for(const XYPoint& p: curr_ship.points){
if(getRowValue(p.y) == 0){
fits = false;
}
}
if(fits){
return true;
}
}
return fits;
}
//Checks if the solution passes the constraints
bool Board::passes_constraints(){
int constraints_passed = 0;
for(const constraint& con: constraints){
if(con.c == "o"){
for(Ship& s: ships){
if(s.points[0].y == con.row && s.points[0].x == con.col && s.getSize() == 1){
constraints_passed++;
}
}
}else if(con.c == "<"){
for(Ship& s: ships){
if(s.points[0].y == con.row && s.points[0].x == con.col && s.getHoriz()){
constraints_passed++;
break;
}
}
}else if(con.c == ">"){
for(Ship& s: ships){
if(s.points[s.points.size()-1].y == con.row && s.points[s.points.size()-1].x == con.col && s.getHoriz()){
constraints_passed++;
break;
}
}
}else if(con.c == "^"){
for(Ship& s: ships){
if(s.points[0].y == con.row && s.points[0].x == con.col && !s.getHoriz()){
constraints_passed++;
break;
}
}
}else if(con.c == "v"){
for(Ship& s: ships){
if(s.points[s.points.size()-1].y == con.row && s.points[s.points.size()-1].x == con.col && !s.getHoriz()){
constraints_passed++;
break;
}
}
}else if(con.c == "X"){
for(Ship& s: ships){
for(int x = 1; x < s.points.size()-1;x++){
if(s.points[x].y == con.row && s.points[x].x == con.col && !s.getHoriz() && s.getSize() > 2){
constraints_passed++;
break;
}
}
}
}else if(con.c == "_"){
if(!isFilled(con.row,con.col)){
constraints_passed++;
}
}
}
return constraints_passed == constraints.size();
}
//Loops through each board in solutions and checks if the board passed in is the same as one of the solutions
bool same_board(vector<Board*> Solutions, Board* b){
int count = 0;
for(int i = 0; i < Solutions.size(); i++){
bool add;
int count_ships = 0;
for(Ship& s1: b->ships){
bool ship_there = false;
for(Ship& s2: Solutions[i]->ships){
if(s1.getSize() == s2.getSize() && s1.isHoriz == s2.isHoriz && s1.getCol() == s2.getCol() && s1.getRow() == s2.getRow()){
ship_there = true;
}
}
if(!ship_there){
count_ships++;
}
}
add = count_ships>0;
if(add){
count++;
}
}
return count!=Solutions.size();
}
//Checks if all the lines are filled in the board
bool all_lines_filled(Board* b){
bool filled = true;
for(int i = 0; i < b->getRows();i++){
if(b->getRowValue(i) != 0 && b->getRowValue(i) < b->getRows() ){
filled = false;
}
}
for(int i = 0; i < b->getCols();i++){
if(b->getColValue(i)!= 0 && b->getColValue(i) < b->getCols()){
filled = false;
}
}
return filled;
}