Game Development Reference
In-Depth Information
37. # Creates a brand new, blank board data structure.
38. board = []
39. for i in range(8):
40. board.append([' '] * 8)
41.
42. return board
43.
44.
45. def isValidMove(board, tile, xstart, ystart):
46. # Returns False if the player's move on space xstart,
ystart is invalid.
47. # If it is a valid move, returns a list of spaces that
would become the player's if they made a move here.
48. if board[xstart][ystart] != ' ' or not isOnBoard
(xstart, ystart):
49. return False
50.
51. board[xstart][ystart] = tile # temporarily set the
tile on the board.
52.
53. if tile == 'X':
54. otherTile = 'O'
55. else:
56. otherTile = 'X'
57.
58. tilesToFlip = []
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
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
69. while board[x][y] == otherTile:
70. x += xdirection
71. y += ydirection
72. if not isOnBoard(x, y): # break out of
while loop, then continue in for loop
73. break
74. if not isOnBoard(x, y):
75. continue
76. if board[x][y] == tile:
77. # There are pieces to flip over. Go in the
reverse direction until we reach the original space,
noting all the tiles along the way.
78. while True:
79. x -= xdirection
80. y -= ydirection
81. if x == xstart and y == ystart:
Search WWH ::




Custom Search