Game Development Reference
In-Depth Information
15 - Reversi
Figure 15-7: Each two-item list represents one of the eight directions.
59. for xdirection, ydirection in [[0, 1], [1, 1], [1,
0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]:
60. x, y = xstart, ystart
61. x += xdirection # first step in the direction
62. y += ydirection # first step in the direction
Line 60 sets an
x
and
y
variable to be the same value as
xstart
and
ystart
,
respectively. We will change
x
and
y
to "move" in the direction that
xdirection
and
ydirection
dictate.
xstart
and
ystart
will stay the same so we can remember
which space we originally intended to check. (Remember, we need to set this place back to
a space character, so we shouldn't overwrite the values in them.)
We make the first step in the direction as the first part of our algorithm.
63. if isOnBoard(x, y) and board[x][y] == otherTile:
64. # There is a piece belonging to the other
player next to our piece.
65. x += xdirection
66. y += ydirection
67. if not isOnBoard(x, y):
68. continue
Remember, in order for this to be a valid move, the first step in this direction must be 1)
on the board and 2) must be occupied by the other player's tile. Otherwise there is no
chance to flip over any of the opponent's tiles. In that case, the
if
statement on line 63 is
not
True
and execution goes back to the
for
statement for the next direction.