This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
324 lines (294 loc) · 6.47 KB
/
board.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
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
316
317
318
319
320
321
322
323
#include <cassert>
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
#include "board.h"
#include "constants.h"
#include "drawing.h"
Board::Board()
{
m_shipsLeft = 0;
m_board.resize(BoardSize);
for (int h = 0; h < BoardSize; ++h)
m_board[h].resize(BoardSize);
for (int h = 0; h < BoardSize; ++h)
for (int w = 0; w < BoardSize; ++w)
m_board[h][w] = gratis;
}
Board::Board(const Board &board)
{
m_shipsLeft = board.m_shipsLeft;
m_board.resize(BoardSize);
for (int h = 0; h < BoardSize; ++h)
m_board[h].resize(BoardSize);
for (int h = 0; h < BoardSize; ++h)
for (int w = 0; w < BoardSize; ++w)
m_board[h][w] = board.m_board[h][w];
}
Board& Board::operator=(const Board &board)
{
m_board.resize(BoardSize);
for (int h = 0; h < BoardSize; ++h)
m_board[h].resize(BoardSize);
for (int h = 0; h < BoardSize; ++h)
for (int w = 0; w < BoardSize; ++w)
m_board[h][w] = board.m_board[h][w];
m_ships = board.m_ships;
return *this;
}
void Board::clean()
{
m_shipsLeft = 0;
m_ships.clear();
for (int h = 0; h < BoardSize; ++h)
for (int w = 0; w < BoardSize; ++w)
m_board[h][w] = gratis;
}
void Board::clear()
{
m_shipsLeft = 0;
m_board.clear();
m_ships.clear();
}
void Board::fillRandom()
{
clean();
for (int sz = 4; sz >= 1; --sz)
{
int cnt = 5 - sz;
for (int i = 0; i < cnt; ++i)
putRandomShip(sz);
}
}
Pt Board::getRandPoint()
{
return Pt(rand() % BoardSize, rand() % BoardSize);
}
CellState Board::getCellState(Pt pt)
{
return m_board[pt.x][pt.y];
}
Pt Board::getFreeRandPoint()
{
//TODO reimplement
Pt pt = getRandPoint();
CellState cs = getCellState(pt);
while (cs == wounded || cs == killed || cs == notAimed)
{
pt = getRandPoint();
cs = getCellState(pt);
}
return pt;
}
int Board::getShipsLeft()
{
return m_shipsLeft;
}
bool Board::isShipOnPoint(const Pt pt)
{
CellState &cell = m_board[pt.y][pt.x];
if (cell == alive || cell == wounded)
return true;
return false;
}
bool Board::isPointInBorders(const Pt &a) const
{
return a.x >= 0 && a.x < BoardSize && a.y >= 0 && a.y < BoardSize;
}
bool Board::addShip(const TShip &ship)
{
if (isAbleToPutShip(ship))
{
m_ships.push_back(ship);
for (int i = 0; i < (int)ship.size(); ++i)
{
Pt cur = ship[i];
m_board[cur.y][cur.x] = alive;
}
m_shipsLeft++;
return true;
}
return false;
}
void Board::fillFromBoard(const Board & board)
{
for (int h = 0; h < BoardSize; ++h)
for (int w = 0; w < BoardSize; ++w)
m_board[h][w] = board.m_board[h][w];
}
void Board::showInStream(std::ostream &out)
{
for (int i = 0; i < BoardSize; ++i)
{
for (int j = 0; j < BoardSize; ++j)
out << m_board[i][j] << " ";
out << "\n";
}
out << "\n\n";
}
bool Board::putRandomShip(const int &sz)
{
for (int indx = 0; indx < 100000; ++indx)
{
Pt pt = getRandPoint();
assert(pt.y >= 0 && pt.y < BoardSize);
assert(pt.x >= 0 && pt.y < BoardSize);
while (m_board[pt.y][pt.x] != gratis)
pt = getRandPoint();
int shipDirectionTemp = rand() % 4;
for (int shipDirection = shipDirectionTemp; shipDirection < 4; ++shipDirection)
{
TShip ship;
if (!isAbleToPutShipWithPointsAndDirection(pt, sz, shipDirection, ship))
{
for (int i = 0; i < (int)ship.size(); ++i)
std::cout << ship[i].x << ";" << ship[i].y << "\n";
continue;
}
else
{
if (!addShip(ship))
continue;
return true;
}
}
}
return false;
}
bool Board::isAbleToPutShip(const TShip &ship)
{
if (!isAbleToPutShipOnThisPoint(ship[0], { -1,-1 })) //first point checked without previous one
return false;
for (int i = 1; i < (int)ship.size(); ++i)
if (!isAbleToPutShipOnThisPoint(ship[i], ship[i - 1]))
return false;
return true;
}
void Board::moveCellInDirection(Pt &pt, const int &dir)
{
pt.x += DX[dir];
pt.y += DY[dir];
}
void Board::cleanCell(const Pt &pt)
{
for (int h = -1; h <= 1; ++h)
for (int w = -1; w <= 1; ++w)
{
Pt curPt(pt.x + w, pt.y + h);
if (isPointInBorders(curPt) && m_board[curPt.y][curPt.x] == gratis)
m_board[curPt.y][curPt.x] = notAimed;
}
}
bool Board::isAbleToPutShipOnThisPoint(const Pt &testPoint, const Pt &exceptionPoint) //exceptionPoint - point where a part of current ship lies
{
for (int w = -1; w <= 1; ++w)
for (int h = -1; h <= 1; ++h)
{
Pt check_pt(testPoint.x + w, testPoint.y + h);
if (!isPointInBorders(check_pt))
continue;
if (check_pt == testPoint)
continue;
if (check_pt == exceptionPoint)
continue;
CellState &cur = m_board[check_pt.y][check_pt.x];
if (cur == wounded || cur == alive || cur == killed)
return false;
}
return true;
}
bool Board::isAbleToPutShipWithPointsAndDirection(Pt pt, int sz, int shipDirection, TShip &resultingShip)
{
Pt curPt = pt, prev = { -1,-1 };
TShip ship;
ship.push_back(curPt);
for (int j = 1; j < sz; ++j)
{
prev = curPt;
curPt = move(curPt, DX[shipDirection], DY[shipDirection]);
if (!isPointInBorders(curPt))
return false;
if (isAbleToPutShipOnThisPoint(curPt, prev))
ship.push_back(curPt);
else
return false;
}
resultingShip = ship;
return true;
}
std::ostream& operator << (std::ostream &out, Board &a)
{
for (int i = 0; i < BoardSize; ++i)
{
for (int j = 0; j < BoardSize; ++j)
out << (char)a.m_board[i][j];
out << "\n";
}
out << "\n\n";
return out;
}
bool Board::makeShot(Pt pt, bool &isShotHit) //return true if shot was made
{
if (!isPointInBorders(pt))
return false;
CellState &cell = m_board[pt.y][pt.x];
isShotHit = false;
switch (cell)
{
case vacant:
case alive:
{
cell = wounded;
if (isKilled(pt))
killShip(pt);
isShotHit = true;
return true;
}
case wounded:
case killed:
case notAimed:
return false;
case gratis:
{
cell = notAimed;
return true;
}
}
assert(0);
return false;
}
bool Board::isKilled(Pt pt)
{
//0-down, 1-left, 2-up, 3-right
int dir = 0;
for (int i = 0; i < 4; ++i)
{
Pt nextPt = move(pt, DX[i], DY[i]);
while (isPointInBorders(nextPt) && (m_board[nextPt.y][nextPt.x] == alive || m_board[nextPt.y][nextPt.x] == wounded))
{
if (m_board[nextPt.y][nextPt.x] == alive)
return false;
nextPt = move(nextPt, DX[i], DY[i]);
}
}
return true;
}
void Board::killShip(Pt pt)
{
//0-down, 1-left, 2-up, 3-right
int dir = 0;
m_board[pt.y][pt.x] = killed;
cleanCell(pt);
m_shipsLeft--;
for (int i = 0; i < 4; ++i)
{
Pt nextPt = move(pt, DX[i], DY[i]);
while (isPointInBorders(nextPt) && m_board[nextPt.y][nextPt.x] == wounded)
{
m_board[nextPt.y][nextPt.x] = killed;
cleanCell(nextPt);
nextPt = move(nextPt, DX[i], DY[i]);
}
}
}