-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
283 lines (240 loc) · 8.34 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
#include "Board.h"
#include "Stones.h"
#include <iostream>
//constructor for class Board
Board::Board(void)
{
//creating 2D vector of [boardSize x boardSize]
gameBoard.resize(boardSize);
for (short int i = 0; i < boardSize; ++i)
gameBoard[i].resize(boardSize);
//initialization to default values for gameBoard vector and freeFields vector
for (short int row = 0; row < boardSize; ++row)
{
for (short int col = 0; col < boardSize; ++col)
{
//board is filled with "\0" - end of string sequence
gameBoard[row][col] = "\0";
//indexes are for user different than for program
freeFields.push_back(std::to_string(row + 1) + std::to_string(col + 1));
}
}
}
void Board::printBoard(void)
{
std::cout << " " << std::endl;
std::cout << " 1 2 3 4 " << std::endl;
std::cout << " +---------------------------+ " << std::endl;
bool firstStonePart;
for (short int row = 0; row < boardSize; ++row)
{
//row names start at 1
std::cout << " " << row + 1 << " |";
firstStonePart = true;
for (short int col = 0; col < boardSize; ++col)
{
//indexes in array gameBoard starts at 0 as usual; thats why row-1 resp. col-1
//dividing data string to two logical rows but still contained in one table row
//format of stone is: 00
// 00
if (firstStonePart)
{
//if cell on gameBoard does not hold stone, print spaces(nothing)
if (gameBoard[row][col].empty())
std::cout << " ";
else
std::cout << " " << gameBoard[row][col].substr(0,2);
std::cout << " |";
//we are on last col of first stone logical row
//now lets repeat the process for second logical row
if (col == boardSize - 1)
{
//reset col counter, will start counting next loop at 0 again
col = -1;
firstStonePart = false;
//end of first logical row
std::cout << std::endl << " |";
}
}
else
{
//if cell on gameBoard does not hold stone, print spaces(nothing)
if (gameBoard[row][col].empty())
std:: cout << " ";
else
std::cout << " " << gameBoard[row][col].substr(2,4);
std::cout << " |";
}
}
std::cout << std::endl << " +---------------------------+ " << std::endl;
}
}
bool Board::checkIfFieldFree(const std::string &chosenField)
{
return (gameBoard[charIntToInt(chosenField[0]) - 1][charIntToInt(chosenField[1]) - 1] == "\0");
}
void Board::compareBinaryStrings(const std::string &stone1, const std::string &stone2, std::vector<int> &Masks)
{
for (int i = 0; i < Stones::stoneStringSize; ++i)
{
//XOR operation : ^; returns 1 if numbers are different
if (charIntToInt(stone1[i]) ^ charIntToInt(stone2[i]) == 1)
//delete mask option from vector
Masks.at(i) = 0;
}
}
void Board::setMaskVectors(std::vector<std::vector<int> > &Masks)
{
for (int i = 0; i < Masks.size(); ++i)
{
Masks[i] =
{
//Masks for every bit for binary string stones
//cannot be specified in binary like 0bxxxx, because this is not compiled as -std=c++14
8, //0b1000
4, //0b0100
2, //0b0010
1 //0b0001
};
}
}
void Board::clearEmptyMaskVectors(std::vector<std::vector<int> > &vectorOfMasks)
{
//remove all elements that contain mask equal to zero, it is used with meaning of deleted value
for (int i = 0; i < vectorOfMasks.size(); ++i)
vectorOfMasks.at(i).erase(std::remove(vectorOfMasks.at(i).begin(), vectorOfMasks.at(i).end(), 0), vectorOfMasks.at(i).end());
}
bool Board::checkForWinners(const std::string &chosenField)
{
//valid indexing is from zero to boardSize-1
int chosenFieldRow = charIntToInt(chosenField[0]) - 1;
int chosenFieldCol = charIntToInt(chosenField[1]) - 1;
std::vector<std::vector<int> > vectorOfMasks;
vectorOfMasks.resize(4);
//set all Masks to default value
setMaskVectors(vectorOfMasks);
//vectorOfMasks.at(0) == horizontalMask
//vectorOfMasks.at(1) == VerticalMask
//vectorOfMasks.at(2) == MainDiagonalMask
//vectorOfMasks.at(3) == SecDiagonalMask
for (int index = 1; index < boardSize; ++index)
{
//check Horizontally
//if vector Masks is empty then not only one of stone characteristics were same
//rest does not have to be searched
if (!vectorOfMasks.at(0).empty())
{
//not horizontally filled, cannot be succesful later on
if ((gameBoard[chosenFieldRow][0] == "\0") || (gameBoard[chosenFieldRow][index] == "\0"))
vectorOfMasks.at(0).clear();
else
//compare bit characteristics in stones that are horizontally ordered
compareBinaryStrings(gameBoard[chosenFieldRow][0], gameBoard[chosenFieldRow][index], vectorOfMasks.at(0));
}
//check Vertically
//if vector Masks is empty then not only one of stone characteristics were same
//rest does not have to be searched
if (!vectorOfMasks.at(1).empty())
{
//not vertically filled, cannot be succesful later on
if ((gameBoard[0][chosenFieldCol] == "\0") || (gameBoard[index][chosenFieldCol] == "\0"))
vectorOfMasks.at(1).clear();
else
//compare bit characteristics in stones that are horizontally ordered
compareBinaryStrings(gameBoard[0][chosenFieldCol], gameBoard[index][chosenFieldCol], vectorOfMasks.at(1));
}
//check Main Diagonal
//if vector Masks is empty then not only one of stone characteristics were same
//rest does not have to be searched
if (chosenFieldRow == chosenFieldCol)
{
//field cells on main diagonal [1,1] [2,2] [3,3] [4,4]
//represented in C indexing as [0,0] [1,1] [2,2] [3,3]
if (!vectorOfMasks.at(2).empty())
{
//not vertically filled, cannot be succesful later on
if ((gameBoard[0][0] == "\0") || (gameBoard[index][index] == "\0"))
vectorOfMasks.at(2).clear();
else
compareBinaryStrings(gameBoard[0][0], gameBoard[index][index], vectorOfMasks.at(2));
}
}
if (chosenFieldCol == boardSize - chosenFieldRow + 1)
{
//field cells on secondary diagonal [1,4] [2,3] [3,2] [4,1]
// represented in C indexing as [0,3] [1,2] [2,1] [3,0]
if (!vectorOfMasks.at(3).empty())
{
//not vertically filled, cannot be succesful later on
if ((gameBoard[0][boardSize - 1] == "\0") || (gameBoard[index][boardSize - 1 - index] == "\0"))
vectorOfMasks.at(3).clear();
else
compareBinaryStrings(gameBoard[0][boardSize - 1], gameBoard[index][boardSize - 1 - index], vectorOfMasks.at(3));
}
}
}
clearEmptyMaskVectors(vectorOfMasks);
//some common characteristic was found
//we have a winner here
if (!vectorOfMasks.at(0).empty() || !vectorOfMasks.at(1).empty())
return true;
if (!vectorOfMasks.at(2).empty() && (chosenFieldRow == chosenFieldCol))
return true;
else if (!vectorOfMasks.at(3).empty() && (chosenFieldCol == boardSize - chosenFieldRow + 1))
return true;
return false;
}
bool Board::isBoardFull(void)
{
for (int row = 0; row < boardSize; ++row)
{
for (int col = 0; col < boardSize; ++col)
{
if (gameBoard[row][col] == "\0")
return false;
}
}
return true;
}
void Board::gameStatus(const std::string &chosenField, PlayersEnum playerChoice)
{
//we found winner
if (checkForWinners(chosenField))
{
if (playerChoice == PlayersEnum::computer)
{
std::cout << "computer wins" << std::endl;
exit (EXIT_SUCCESS);
}
else
{
std::cout << "player wins" << std::endl;
exit (EXIT_SUCCESS);
}
}
//no winner and board is full, game also ended here
else if (isBoardFull())
{
std::cout << "no winner" << std::endl;
exit (EXIT_SUCCESS);
}
else
std::cout << "playing" << std::endl;
}
void Board::placeStone(const std::string chosenStone, const std::string chosenField)
{
//get string indexes to integer and write stone on that position
// - 1 because of different indexing for player, he expects 1 - 4
gameBoard[charIntToInt(chosenField[0]) - 1][charIntToInt(chosenField[1]) - 1] = chosenStone;
}
std::string Board::generateRandomField(void)
{
srand((int)time(0));
int randomNumber = (rand() % freeFields.size());
return freeFields.at(randomNumber);
}
void Board::reduceFreeFields(const std::string chosenField)
{
//remove chosen stone from vector of freeStones via std::remove from <algorithm>
freeFields.erase(std::remove(freeFields.begin(), freeFields.end(), chosenField), freeFields.end());
}