[Tutor] Todays Learning Python Question From a Newbie ;)
Jon Moore
jonathan.r.moore at gmail.com
Wed Feb 1 12:57:05 CET 2006
Hi,
Ok its the last exercise in the chapter of the python book (Python for the
absolute beginner) I am working my way through.
I have been learning about functions using a tic-tac-toe game as an example
and I understand it fairly clearly, however the author says the following:
Write a new computer_move() function for the tic-tac-toe game to plug the
hole in the computers stratergy. See if you can create an opponent that is
unbeatable!
My main problem is that I can not see how the computers stratergy can be
improved as at best I can only manage a tie with the computer!
If I could see past this, I could hopefully work out the code.
Copy of the function:
def computer_move(board, computer, human):
"""Make computer move."""
# make a copy to work with since function will be changing list
board = board[:]
# the best positions to have, in order
BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)
print "I shall take square number",
# if computer can win, take that move
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print move
return move
# done checking this move, undo it
board[move] = EMPTY
# if human can win, block that move
for move in legal_moves(board):
board[move] = human
if winner(board) == human:
print move
return move
# done checkin this move, undo it
board[move] = EMPTY
# since no one can win on next move, pick best open square
for move in BEST_MOVES:
if move in legal_moves(board):
print move
return move
--
Best Regards
Jon Moore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060201/9282a32f/attachment.htm
More information about the Tutor
mailing list