<br><br><div><span class="gmail_quote">On 02/02/06, <b class="gmail_sendername">Alan Gauld</b> <<a href="mailto:alan.gauld@freenet.co.uk">alan.gauld@freenet.co.uk</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Bob,<br><br>>> Write a new computer_move() function for the tic-tac-toe game to plug<br>>> the hole in the computers stratergy. See if you can create an opponent<br>>> that is unbeatable!<br>>><br>
>> My main problem is that I can not see how the computers stratergy can<br>>> be improved as at best I can only manage a tie with the computer!<br><br>Does the computer ever go first?</blockquote><div><br>Yes if you let it!
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Does the computer start with a random location?</blockquote><div><br>No, if you look at the code below, it has a predefined set of 'best moves'.
<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">If yes to the above then the computer can be beaten since the entire<br>course of a Tic-Tac-Toe game (or OXO as we call it in the UK!)
<br>depends upon the first move location.</blockquote><div><br>Thanks to André, there is a way to win every time if you take the first move (see below), so there MUST be a whole in the computers stratergy! Based on what we all know about the game, I would say that you can not make it so that the computer can win every time, but it should be possable to make it tie.
<br><br>x: 0<br>o: 4<br>x: 7<br>o: 2<br>x: 6<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">If no to the above then, provided the gameplay is OK, I don't know
<br>what the author means either.<br><br>Alan G.<br></blockquote></div><br>The code is as follows:<br clear="all"><br># set global constants<br>X = "X"<br>O = "O"<br>EMPTY = " "<br>TIE = "TIE"
<br>NUM_SQUARES = 9<br><br># set game instructions<br>def display_instruct():<br> """Display game instructions."""<br> print \<br> """<br> Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe.
<br> This will be a showdown between your human brain and my silicon processor. <br><br> You will make your move known by entering a number, 0 - 8. The number <br> will correspond to the board position as illustrated:
<br> <br> 0 | 1 | 2<br> ---------<br> 3 | 4 | 5<br> ---------<br> 6 | 7 | 8<br><br> Prepare yourself, human. The ultimate battle is about to begin. \n
<br> """<br><br># set question<br>def ask_yes_no(question):<br> """Ask a yes or no question."""<br> response = None<br> while response not in ("y", "n"):
<br> response = raw_input(question).lower()<br> return response<br><br># set ask number<br>def ask_number(question, low, high):<br> """Ask for a number within the range"""<br> response = None
<br> while response not in range(low, high):<br> response = int(raw_input(question))<br> return response<br><br># set pieces<br>def pieces():<br> """Determine if player or computer goes first."""
<br> go_first = ask_yes_no("Do you wish to go first? (y/n): ")<br> if go_first == "y":<br> print "\nThen take the first move. You will need it ;)"<br> human = X<br> computer = O
<br> else:<br> print "\nYour bravery will be your undoing....I will go first."<br> computer = X<br> human = O<br> return computer, human<br><br># create new board<br>def new_board():<br>
"""Create a new game board."""<br> board = []<br> for square in range(NUM_SQUARES):<br> board.append(EMPTY)<br> return board<br><br># display the board<br>def display_board(board):
<br> """Display the board on the screen"""<br> print "\n\t", board[0], "|", board[1], "|", board[2]<br> print "\t", "---------"<br> print "\t", board[3], "|", board[4], "|", board[5]
<br> print "\t", "---------"<br> print "\t", board[6], "|", board[7], "|", board[8], "\n"<br><br># set legal moves<br>def legal_moves(board):<br> """Create list of legal moves."""
<br> moves = []<br> for square in range(NUM_SQUARES):<br> if board[square] == EMPTY:<br> moves.append(square)<br> return moves<br><br># set winner<br>def winner(board):<br> """Determine the game winner"""
<br> WAYS_TO_WIN = ((0, 1, 2),<br> (3, 4, 5),<br> (6, 7, 8),<br> (0, 3, 6),<br> (1, 4, 7),<br> (2, 5, 8),<br> (0, 4, 8),
<br> (2, 4, 6))<br><br> for row in WAYS_TO_WIN:<br> if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:<br> winner = board[row[0]]<br> return winner<br><br> if EMPTY not in board:
<br> return TIE<br> return None<br><br># set human move<br>def human_move(board, human):<br> """Get human move."""<br> legal = legal_moves(board)<br> move = None<br> while move not in legal:
<br> move = ask_number("Where will you move? (0-8): ", 0, NUM_SQUARES)<br> if move not in legal:<br> print "\nThat square is already occupied. Please choose another.\n"<br> print "Fine..."
<br> return move<br><br># set computer move<br>def computer_move(board, computer, human):<br> """Make computer move."""<br> # Make a copy of the board to work with since the function will be changing the list
<br> board = board[:]<br> # The best positions to have, in order<br> BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)<br><br> print "I shall take a square number",<br> # if computer can win, take that move
<br> for move in legal_moves(board):<br> board[move] = computer<br> if winner(board) == computer:<br> print move<br> return move<br> # done checking this move, undo it<br> board[move] = EMPTY
<br><br> #if human can win, block that move<br> for move in legal_moves(board):<br> board[move] = human<br> if winner(board) == human:<br> print move<br> return move<br> # done checking this move, undo it
<br> board[move] = EMPTY<br><br> # since no one can win on next move, pick best open square<br> for move in BEST_MOVES:<br> if move in legal_moves(board):<br> print move<br> return move
<br><br># set next turn<br>def next_turn(turn):<br> """Switch turns."""<br> if turn == X:<br> return O<br> else:<br> return X<br><br># congratulate winner<br>def congrat_winner(the_winner, computer, human):
<br> """Congratulate the winner"""<br> if the_winner != TIE:<br> print the_winner, "won!\n"<br> else:<br> print "Its a tie!\n"<br><br> if the_winner == computer:
<br> print "As I predicted human, I am triumphant once more. \n" \<br> "Proof that computers are superior to humans in all regards."<br><br> elif the_winner == human:<br> print "Nooooo! It cannot be! Somehow you tricked me human! \n" \
<br> "But never again.......I will win next time!"<br><br> elif the_winner == TIE:<br> print "You were most lucky human, and somehow managed to tie me! \n" \<br> "Celebrate today....for this will not happen again!"
<br><br># set main function<br>def main():<br> display_instruct()<br> computer, human = pieces()<br> turn = X<br> board = new_board()<br> display_board(board)<br><br> while not winner(board):<br> if turn == human:
<br> move = human_move(board, human)<br> board[move] = human<br> else:<br> move = computer_move(board, computer, human)<br> board[move] = computer<br> display_board(board)
<br> turn = next_turn(turn)<br><br> the_winner = winner(board)<br> congrat_winner(the_winner, computer, human)<br><br># start the program<br>main()<br> <br><br>raw_input("\n\nPress the enter key to exit.")
<br>-- <br>Best Regards<br><br>Jon Moore