Hardware Reference
In-Depth Information
he list called wins consists of eight items, and each item is a list of three of the square num-
bers that constitute a win. he way you drive this is with the loop variables in the for state-
ments. he irst statement
for test in wins :
makes the variable called test take on the value of each of the sublists in turn. hen, when
it comes to the for statement
for squares in test :
which is nested inside the irst one, the variable squares will take on the values, in turn, of the
squares that you need to ill with one symbol to get a win. What happens now is that if the
board's symbol matches the one you are looking for, a count is incremented. If that count
reaches a value of three, the board has a win for that player. he logic variable win is set initially
to false, but if a win is detected, it is changed to true. It is this variable that is returned to the
calling program. he last part of the listing simply exercises the checkWin() function by call-
ing it to check for each playing symbol in turn. For the way the board list is set up, there is a win
for the O player. You can change the board list to have a win for the X player if you like.
More Infrastructure
he last pieces of infrastructure you need are a function to clear the board for a fresh game
and one that checks if any new moves can be made. hese are quite simple and are shown in
Listing 3-4.
Listing 3-4 Utility Functions
def wipeBoard() :
global board
for square in range(0, len(board) ) :
board[square] = ' '
def canMove(): # see if move can be made
move = False
for square in range(0, len(board) ) :
if board[square] == ' ':
move = True
return move
 
Search WWH ::




Custom Search