Game Development Reference
In-Depth Information
modifying the one passed to it by the board parameter. Line 100 calls
getValidMoves() , which returns a list of xy coordinates with all the legal moves the
player could make. The board copy is then marked with a period in those spaces. How
getValidMoves() works is described next.
105. def getValidMoves(board, tile):
106. # Returns a list of [x,y] lists of valid moves for
the given player on the given board.
107. validMoves = []
108.
109. for x in range(8):
110. for y in range(8):
111. if isValidMove(board, tile, x, y) != False:
112. validMoves.append([x, y])
113. return validMoves
The getValidMoves() function returns a list of two-item lists that hold the XY
coordinates for all valid moves for tile's player, given a particular game board data structure
in board .
This function uses two loops to check every single XY coordinate (all sixty four of them)
by calling isValidMove() on that space and checking if it returns False or a list of
possible moves (in which case it is a valid move). Each valid XY coordinate is appended to
the list, validMoves .
The bool() Function
Remember how you could use the int() and str() functions to get the integer and
string value of other data types? For example, str(42) would return the string '42' , and
int('100') would return the integer 100 .
There is a similar function for the Boolean data type, bool() . Most other data types
have one value that is considered the False value for that data type, and every other value
is consider True . The integer 0 , the floating point number 0.0 , the empty string, the
empty list, and the empty dictionary are all considered to be False when used as the
condition for an if or loop statement. All other values are True . Try entering the
following into the interactive shell:
>>> bool(0)
False
>>> bool(0.0)
False
>>> bool('')
False
>>> bool([])
Search WWH ::




Custom Search