Game Development Reference
In-Depth Information
structure, the player's tile, and the XY coordinates for player's move, this function should
return True if the Reversi game rules allow that move and False if they don't.
The easiest check we can do to disqualify a move is to see if the XY coordinates are on
the game board or if the space at XY is not empty. This is what the if statement on line 48
checks for. isOnBoard() is a function we will write that makes sure both the X and Y
coordinates are between 0 and 7 .
For the purposes of this function, we will go ahead and mark the XY coordinate pointed
to by xstart and ystart with the player's tile. We set this place on the board back to a
space before we leave this function.
The player's tile has been passed to us, but we will need to be able to identify the other
player's tile. If the player's tile is 'X' then obviously the other player's tile is 'O' . And it is
the same the other way.
Finally, if the given XY coordinate ends up as a valid position, we will return a list of all
the opponent's tiles that would be flipped by this move.
59. for xdirection, ydirection in [[0, 1], [1, 1], [1,
0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]:
The for loop iterates through a list of lists which represent directions you can move on
the game board. The game board is a Cartesian coordinate system with an X and Y
direction. There are eight directions you can move: up, down, left, right, and the four
diagonal directions. We will move around the board in a direction by adding the first value
in the two-item list to our X coordinate, and the second value to our Y coordinate.
Because the X coordinates increase as you go to the right, you can "move" to the right by
adding 1 to the X coordinate. Moving to the left is the opposite: you would subtract 1 (or
add -1 ) from the X coordinate. We can move up, down, left, and right by adding or
subtracting to only one coordinate at a time. But to move diagonally, we need to add or
subtract to both coordinates. For example, adding 1 to the X coordinate to move right and
adding -1 to the Y coordinate to move up would result in moving to the up-right diagonal
direction.
Checking Each of the Eight Directions
Here is a diagram to make it easier to remember which two-item list represents which
direction:
Search WWH ::




Custom Search