-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGomokuBoard.cpp
195 lines (168 loc) · 4.79 KB
/
GomokuBoard.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
/*
GomokuBoard.cpp
Actual game board for tile placement and win/tie checking
@author Matthew Clark
@date 09/02/2016
*/
#include "GomokuBoard.h"
GomokuBoard::GomokuBoard()
{
//initializes board
mLastTile = NULL;
mBoardSize = 15;
mTilesPlaced = 0;
//creates stones at all tile positions
vecGameArea.resize(mBoardSize);
for (size_t i = 0; i < vecGameArea.size(); i++) {
vecGameArea[i].resize(mBoardSize);
}
}
GomokuBoard::~GomokuBoard()
{
}
bool GomokuBoard::addStone(int xPos, int yPos, int color, std::string entName, std::string nodeName)
{
//if the tile is empty, add a stone with specified color
if (emptyTile(xPos, yPos)) {
mLastTile = &vecGameArea[xPos][yPos];
mLastTile->color = color;
mLastTile->entName = entName;
mLastTile->nodeName = nodeName;
mLastTile->xGrid = xPos;
mLastTile->yGrid = yPos;
mTilesPlaced++;
return true;
}
//otherwise return that spot wasn't empty
return false;
}
bool GomokuBoard::emptyTile(int xPos, int yPos)
{
//returns if tile color is empty
return vecGameArea[xPos][yPos].color == stoneColor::NONE;
}
bool GomokuBoard::boardFilled() {
//returns if the number of current tiles is the total number of tiles
return (mTilesPlaced == mBoardSize * mBoardSize);
}
bool GomokuBoard::gameWon() {
int inRow = 5; //number in row to win
int xCenter = mLastTile->xGrid; //last tile placed x position
int yCenter = mLastTile->yGrid; //last tile placed y position
int lastColor = mLastTile->color; //last tile placed color
int xCurr = 0; //current x position of check
int yCurr = 0; //current y position of check
//NSEW -> cardinal directions
int iNum0 = 0; //number in E-W row
int iNum45 = 0; //number in SW-NE row
int iNum90 = 0; //number in N-S row
int iNum135 = 0; //number in NW-SE row
bool bWon = false; //if number of stones in row required to win
//check 2 * inRow - 1 tiles for each row
//ex. 5 inRow = 9 total tiles checked; 4 for each side of last tile placed
for (int i = 0; i < 2 * inRow - 1; i++) {
//check E-W row
xCurr = xCenter - (inRow - 1) + i;
yCurr = yCenter;
//check if coordinates on the board
if (xCurr >= 0 && xCurr <= mBoardSize - 1) {
//add to row length if stone being checked is still the same color
if (vecGameArea[xCurr][yCurr].color == lastColor) {
iNum0++;
if (iNum0 >= inRow) bWon = true;
}
//otherwise reset number of stones in row
else {
iNum0 = 0;
}
}
//check SW-NE row
xCurr = xCenter - (inRow - 1) + i;
yCurr = yCenter + (inRow - 1) - i;
//check if coordinates on the board
if (xCurr >= 0 && xCurr <= mBoardSize - 1 && yCurr >= 0 && yCurr <= mBoardSize - 1) {
//add to row length if stone being checked is still the same color
if (vecGameArea[xCurr][yCurr].color == lastColor) {
iNum45++;
if (iNum45 >= inRow) bWon = true;
}
//otherwise reset number of stones in row
else {
iNum45 = 0;
}
}
//check N-S row
xCurr = xCenter;
yCurr = yCenter + (inRow - 1) - i;
//check if coordinates on the board
if (yCurr >= 0 && yCurr <= mBoardSize - 1) {
//add to row length if stone being checked is still the same color
if (vecGameArea[xCurr][yCurr].color == lastColor) {
iNum90++;
if (iNum90 >= inRow) bWon = true;
}
//otherwise reset number of stones in row
else {
iNum90 = 0;
}
}
//check NW-SE row
xCurr = xCenter + (inRow - 1) - i;
yCurr = yCenter + (inRow - 1) - i;
//check if coordinates on the board
if (xCurr >= 0 && xCurr <= mBoardSize - 1 && yCurr >= 0 && yCurr <= mBoardSize - 1) {
//add to row length if stone being checked is still the same color
if (vecGameArea[xCurr][yCurr].color == lastColor) {
iNum135++;
if (iNum135 >= inRow) bWon = true;
}
//otherwise reset number of stones in row
else {
iNum135 = 0;
}
}
}
//if a row with inRow found, return that a win was found
if (bWon) return true;
//otherwise return no win
else return false;
}
void GomokuBoard::clearBoard()
{
//reset last tile placed and total tiles
mLastTile = NULL;
mTilesPlaced = 0;
//reset all tiles on board to default
vecGameArea.clear();
vecGameArea.resize(mBoardSize);
for (size_t i = 0; i < vecGameArea.size(); i++) {
vecGameArea[i].resize(mBoardSize);
}
}
GameTile GomokuBoard::getStone(int xPos, int yPos)
{
return vecGameArea[xPos][yPos];
}
GameTile* GomokuBoard::getLastStone()
{
return mLastTile;
}
int GomokuBoard::getBoardSize()
{
return mBoardSize;
}
std::vector<GameTile> GomokuBoard::getAllStones()
{
//search the board and return all tiles that aren't empty
std::vector<GameTile> vecStones;
for (size_t i = 0; i < vecGameArea.size(); i++) {
for (size_t j = 0; j < vecGameArea[i].size(); j++) {
if (!emptyTile(i, j)) vecStones.push_back(vecGameArea[i][j]);
}
}
return vecStones;
}
std::vector< std::vector<GameTile> > GomokuBoard::getGameArea()
{
return vecGameArea;
}