Skip to content

Commit 349eacc

Browse files
committed
Fixes to Exercise 4.2 starter code
1 parent 4f4a026 commit 349eacc

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

Exercises/4.2/Board.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ BoardState::BoardState()
2020
}
2121
}
2222

23-
std::vector<BoardState*> BoardState::GetPossibleMoves() const
23+
std::vector<BoardState*> BoardState::GetPossibleMoves(SquareState player) const
2424
{
2525
std::vector<BoardState*> retVal;
2626

@@ -32,7 +32,7 @@ std::vector<BoardState*> BoardState::GetPossibleMoves() const
3232
if (mBoard[row][col] == BoardState::Empty)
3333
{
3434
retVal.emplace_back(new BoardState(*this));
35-
retVal.back()->mBoard[row][col] = BoardState::Red;
35+
retVal.back()->mBoard[row][col] = player;
3636
break;
3737
}
3838
}
@@ -96,7 +96,7 @@ bool BoardState::IsFull() const
9696

9797
int BoardState::GetFourInARow() const
9898
{
99-
// Returns 1 if yellow wins, -1 if red wins, 0 otherwise
99+
// Returns -1 if yellow wins, 1 if red wins, 0 otherwise
100100

101101
// Check if there's a row with four in a row
102102
for (int row = 0; row < 6; row++)
@@ -109,11 +109,11 @@ int BoardState::GetFourInARow() const
109109
{
110110
if (mBoard[row][col] == BoardState::Yellow)
111111
{
112-
return 1;
112+
return -1;
113113
}
114114
else if (mBoard[row][col] == BoardState::Red)
115115
{
116-
return -1;
116+
return 1;
117117
}
118118
}
119119
}
@@ -130,11 +130,11 @@ int BoardState::GetFourInARow() const
130130
{
131131
if (mBoard[row][col] == BoardState::Yellow)
132132
{
133-
return 1;
133+
return -1;
134134
}
135135
else if (mBoard[row][col] == BoardState::Red)
136136
{
137-
return -1;
137+
return 1;
138138
}
139139
}
140140
}
@@ -144,18 +144,18 @@ int BoardState::GetFourInARow() const
144144
for (int col = 0; col < 4; col++)
145145
{
146146
for (int row = 0; row < 3; row++)
147-
{
147+
{
148148
if (mBoard[row][col] == mBoard[row + 1][col + 1] &&
149149
mBoard[row][col] == mBoard[row + 2][col + 2] &&
150150
mBoard[row][col] == mBoard[row + 3][col + 3])
151151
{
152152
if (mBoard[row][col] == BoardState::Yellow)
153153
{
154-
return 1;
154+
return -1;
155155
}
156156
else if (mBoard[row][col] == BoardState::Red)
157157
{
158-
return -1;
158+
return 1;
159159
}
160160
}
161161
}
@@ -172,11 +172,11 @@ int BoardState::GetFourInARow() const
172172
{
173173
if (mBoard[row][col] == BoardState::Yellow)
174174
{
175-
return 1;
175+
return -1;
176176
}
177177
else if (mBoard[row][col] == BoardState::Red)
178178
{
179-
return -1;
179+
return 1;
180180
}
181181
}
182182
}
@@ -209,7 +209,7 @@ bool TryPlayerMove(BoardState* state, int column)
209209
void CPUMove(BoardState* state)
210210
{
211211
// For now, this just randomly picks one of the possible moves
212-
std::vector<BoardState*> moves = state->GetPossibleMoves();
212+
std::vector<BoardState*> moves = state->GetPossibleMoves(BoardState::Red);
213213

214214
int index = Random::GetIntRange(0, moves.size() - 1);
215215

Exercises/4.2/Board.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
class BoardState
1313
{
1414
public:
15+
enum SquareState { Empty, Red, Yellow };
1516
BoardState();
16-
std::vector<BoardState*> GetPossibleMoves() const;
17+
std::vector<BoardState*> GetPossibleMoves(SquareState player) const;
1718
bool IsTerminal() const;
1819
float GetScore() const;
1920

20-
enum SquareState { Empty, Red, Yellow };
2121
SquareState mBoard[6][7];
2222
protected:
2323
bool IsFull() const;

0 commit comments

Comments
 (0)