[Tutor] checking elements of a list
sbrozo
sbrozo at zoominternet.net
Thu Apr 14 05:04:24 CEST 2005
def computer_move(board, computer, human):
board = board[:]
BEST_MOVES = [4,0,2,6,8,1,3,5,7]
corners = [(0,8),(2,6)]
print "I shall take square number",
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print move
return move
board[move] = EMPTY
for move in legal_moves(board):
board[move] = human
if winner(board) == human:
print move
return move
board[move] = EMPTY
for move in legal_moves(board): # trying to prevent trap of taking 2 corners first with the computer always taking the center
board[move] = computer
for pair in corners:
if board[pair[0]] == board[pair[1]] == human: # checking if corners human
BEST_MOVES = [1,3,5,7]
print move
return move
board[move] = EMPTY
for move in BEST_MOVES:
if move in legal_moves(board):
print move
return move
def next_turn(turn):
if turn == X:
return O
else:
return X
This is one of the functions in a tic tac toe game from "Python Programming (for the absolute beginner)" by Michael Dawson and it asks to improve on the computer AI.
I put in a list with the name corners to have it test the board and if I have taken 2 diagonal corners then have the computer use a new list of BEST_MOVES for a reply.
It works if I have taken corners pair (0,8) from the list but not when I take the (2,6) pair. I have reversed the pairs in the 'corners' list but it still only works with the (0,8)
pair. Do you need the code for the entire game to follow my train of thought? I am an extreme newbie as I'm sure you can tell, and I have spent hours trying to
complete this exercise. Many thanks in advance for any advice you can pass along.
Steve Brozo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050413/53030e99/attachment.htm
More information about the Tutor
mailing list