Newer
Older
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
enum PieceType {
empty,
black,
white,
}
/// This method flips a black piece to a white one, and vice versa. I'm still
/// unsure about having it as a global function, but don't know where else to
/// put it.
PieceType getOpponent(PieceType player) =>
(player == PieceType.black) ? PieceType.white : PieceType.black;
/// A position on the reversi board. Just an [x] and [y] coordinate pair.
class Position {
final int x;
final int y;
const Position(this.x, this.y);
}
/// An immutable representation of a reversi game's board.
class GameBoard {
static final int height = 8;
static final int width = 8;
final List<List<PieceType>> rows;
// Because calculating out all the available moves for a player can be
// expensive, they're cached here.
final _availableMoveCache = <PieceType, List<Position>>{};
/// Default constructor, which creates a board with pieces in starting
/// position.
GameBoard() : rows = _emptyBoard;
/// Copy constructor.
GameBoard.fromGameBoard(GameBoard other)
: rows = List.generate(height, (i) => List.from(other.rows[i]));
/// Retrieves the type of piece at a location on the game board.
PieceType getPieceAtLocation(int x, int y) {
assert(x >= 0 && x < width);
assert(y >= 0 && y < height);
return rows[y][x];
}
/// Gets the total number of pieces of a particular type.
int getPieceCount(PieceType pieceType) {
return rows.fold(
0,
(s, e) => s + e.where((e) => e == pieceType).length,
);
}
/// Calculates the list of available moves on this board for a player. These
/// moves are calculated for the first call and cached for any subsequent
/// ones.
List<Position> getMovesForPlayer(PieceType player) {
if (player == PieceType.empty) {
return [];
}
if (_availableMoveCache.containsKey(player)) {
return _availableMoveCache[player]!;
}
final legalMoves = <Position>[];
for (var x = 0; x < width; x++) {
for (var y = 0; y < width; y++) {
if (isLegalMove(x, y, player)) {
legalMoves.add(Position(x, y));
}
}
}
_availableMoveCache[player] = legalMoves;
return legalMoves;
}
/// Returns a new GameBoard instance representing the state this one would
/// have after [player] puts a piece at [x],[y]. This method does not check if
/// the move was legal, and will blindly trust its input.
GameBoard updateForMove(int x, int y, PieceType player) {
assert(player != PieceType.empty);
final newBoard = GameBoard.fromGameBoard(this);
if (!isLegalMove(x, y, player)) {
return newBoard;
}
newBoard.rows[y][x] = player;
for (var dx = -1; dx <= 1; dx++) {
for (var dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue;
newBoard._traversePath(x, y, dx, dy, player, true);
}
}
return newBoard;
}
/// Returns true if it would be a legal move for [player] to put a piece down
/// at [x],[y].
bool isLegalMove(int x, int y, PieceType player) {
assert(player != PieceType.empty);
assert(x >= 0 && x < width);
assert(y >= 0 && y < height);
// It's occupied, yo. No can do.
if (rows[y][x] != PieceType.empty) return false;
// Try each of the eight cardinal directions, looking for a row of opposing
// pieces to flip.
for (var dx = -1; dx <= 1; dx++) {
for (var dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue;
if (_traversePath(x, y, dx, dy, player, false)) return true;
}
}
// No flippable opponent pieces were found in any directions. This is not a
// legal move.
return false;
}
// This method walks the board in one of eight cardinal directions (determined
// by the [dx] and [dy] parameters) beginning at [x],[y], and attempts to
// determine if a move at [x],[y] by [player] would result in pieces getting
// flipped. If so, the method returns true, otherwise false. If [flip] is set
// to true, the pieces are flipped in place to their new colors before the
// method returns.
bool _traversePath(int x, int y, int dx, int dy, PieceType player, bool flip) {
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
var foundOpponent = false;
var curX = x + dx;
var curY = y + dy;
while (curX >= 0 && curX < width && curY >= 0 && curY < height) {
if (rows[curY][curX] == PieceType.empty) {
// This path led to an empty spot rather than a legal move.
return false;
} else if (rows[curY][curX] == getOpponent(player)) {
// Update flag and keep going, hoping to hit one of player's pieces.
foundOpponent = true;
} else if (foundOpponent) {
// Found opposing pieces and then one of player's afterward. This is
// a legal move.
if (flip) {
// Backtrack, flipping pieces to player's color.
while (curX != x || curY != y) {
curX -= dx;
curY -= dy;
rows[curY][curX] = player;
}
}
return true;
} else {
// Found one of player's pieces, but no opposing pieces.
return false;
}
curX += dx;
curY += dy;
}
return false;
}
}
const _emptyBoard = [
[
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
],
[
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
],
[
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
],
[
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.black,
PieceType.white,
PieceType.empty,
PieceType.empty,
PieceType.empty,
],
[
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.white,
PieceType.black,
PieceType.empty,
PieceType.empty,
PieceType.empty,
],
[
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
],
[
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
],
[
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
PieceType.empty,
],
];