<br><br><div><span class="gmail_quote">On 02/02/06, <b class="gmail_sendername">Alan Gauld</b> &lt;<a href="mailto:alan.gauld@freenet.co.uk">alan.gauld@freenet.co.uk</a>&gt; 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>&gt;&gt; Write a new computer_move() function for the tic-tac-toe game to plug<br>&gt;&gt; the hole in the computers stratergy. See if you can create an opponent<br>&gt;&gt; that is unbeatable!<br>&gt;&gt;<br>
&gt;&gt; My main problem is that I can not see how the computers stratergy can<br>&gt;&gt; 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&nbsp; 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&nbsp; 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 = &quot;X&quot;<br>O = &quot;O&quot;<br>EMPTY = &quot; &quot;<br>TIE = &quot;TIE&quot;
<br>NUM_SQUARES = 9<br><br># set game instructions<br>def display_instruct():<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Display game instructions.&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; print \<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe.&nbsp; 
<br>&nbsp;&nbsp;&nbsp; This will be a showdown between your human brain and my silicon processor.&nbsp; <br><br>&nbsp;&nbsp;&nbsp; You will make your move known by entering a number, 0 - 8.&nbsp; The number <br>&nbsp;&nbsp;&nbsp; will correspond to the board position as illustrated:
<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 | 1 | 2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ---------<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3 | 4 | 5<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ---------<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6 | 7 | 8<br><br>&nbsp;&nbsp;&nbsp; Prepare yourself, human.&nbsp; The ultimate battle is about to begin. \n
<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;<br><br># set question<br>def ask_yes_no(question):<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Ask a yes or no question.&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; response = None<br>&nbsp;&nbsp;&nbsp; while response not in (&quot;y&quot;, &quot;n&quot;):
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; response =&nbsp; raw_input(question).lower()<br>&nbsp;&nbsp;&nbsp; return response<br><br># set ask number<br>def ask_number(question, low, high):<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Ask for a number within the range&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; response = None
<br>&nbsp;&nbsp;&nbsp; while response not in range(low, high):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; response =&nbsp; int(raw_input(question))<br>&nbsp;&nbsp;&nbsp; return response<br><br># set pieces<br>def pieces():<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Determine if player or computer goes first.&quot;&quot;&quot;
<br>&nbsp;&nbsp;&nbsp; go_first =&nbsp; ask_yes_no(&quot;Do you wish to go first? (y/n): &quot;)<br>&nbsp;&nbsp;&nbsp; if go_first == &quot;y&quot;:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;\nThen take the first move. You will need it ;)&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; human = X<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; computer = O
<br>&nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;\nYour bravery will be your undoing....I will go first.&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; computer = X<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; human = O<br>&nbsp;&nbsp;&nbsp; return computer, human<br><br># create new board<br>def new_board():<br>
&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Create a new game board.&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; board = []<br>&nbsp;&nbsp;&nbsp; for square in range(NUM_SQUARES):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; board.append(EMPTY)<br>&nbsp;&nbsp;&nbsp; return board<br><br># display the board<br>def display_board(board):
<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Display the board on the screen&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; print &quot;\n\t&quot;, board[0], &quot;|&quot;, board[1], &quot;|&quot;, board[2]<br>&nbsp;&nbsp;&nbsp; print &quot;\t&quot;, &quot;---------&quot;<br>&nbsp;&nbsp;&nbsp; print &quot;\t&quot;, board[3], &quot;|&quot;, board[4], &quot;|&quot;, board[5]
<br>&nbsp;&nbsp;&nbsp; print &quot;\t&quot;, &quot;---------&quot;<br>&nbsp;&nbsp;&nbsp; print &quot;\t&quot;, board[6], &quot;|&quot;, board[7], &quot;|&quot;, board[8], &quot;\n&quot;<br><br># set legal moves<br>def legal_moves(board):<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Create list of legal moves.&quot;&quot;&quot;
<br>&nbsp;&nbsp;&nbsp; moves = []<br>&nbsp;&nbsp;&nbsp; for square in range(NUM_SQUARES):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if board[square] == EMPTY:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; moves.append(square)<br>&nbsp;&nbsp;&nbsp; return moves<br><br># set winner<br>def winner(board):<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Determine the game winner&quot;&quot;&quot;
<br>&nbsp;&nbsp;&nbsp; WAYS_TO_WIN = ((0, 1, 2),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return winner<br><br>&nbsp;&nbsp;&nbsp; if EMPTY not in board:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return TIE<br>&nbsp;&nbsp;&nbsp; return None<br><br># set human move<br>def human_move(board, human):<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Get human move.&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; legal = legal_moves(board)<br>&nbsp;&nbsp;&nbsp; move = None<br>&nbsp;&nbsp;&nbsp; while move not in legal:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; move = ask_number(&quot;Where will you move? (0-8): &quot;, 0, NUM_SQUARES)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if move not in legal:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;\nThat square is already occupied. Please choose another.\n&quot;<br>&nbsp;&nbsp;&nbsp; print &quot;Fine...&quot;
<br>&nbsp;&nbsp;&nbsp; return move<br><br># set computer move<br>def computer_move(board, computer, human):<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Make computer move.&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; # Make a copy of the board to work with since the function will be changing the list
<br>&nbsp;&nbsp;&nbsp; board = board[:]<br>&nbsp;&nbsp;&nbsp; # The best positions to have, in order<br>&nbsp;&nbsp;&nbsp; BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)<br><br>&nbsp;&nbsp;&nbsp; print &quot;I shall take a square number&quot;,<br>&nbsp;&nbsp;&nbsp; # if computer can win, take that move
<br>&nbsp;&nbsp;&nbsp; for move in legal_moves(board):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; board[move] = computer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if winner(board) ==&nbsp; computer:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print move<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return move<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # done checking this move, undo it<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; board[move] = EMPTY
<br><br>&nbsp;&nbsp;&nbsp; #if human can win, block that move<br>&nbsp;&nbsp;&nbsp; for move in legal_moves(board):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; board[move] = human<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if winner(board) ==&nbsp; human:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print move<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return move<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # done checking this move, undo it
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; board[move] = EMPTY<br><br>&nbsp;&nbsp;&nbsp; # since no one can win on next move, pick best open square<br>&nbsp;&nbsp;&nbsp; for move in BEST_MOVES:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if move in legal_moves(board):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print move<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return move
<br><br># set next turn<br>def next_turn(turn):<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Switch turns.&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; if turn == X:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return O<br>&nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return X<br><br># congratulate winner<br>def congrat_winner(the_winner, computer, human):
<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Congratulate the winner&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; if the_winner != TIE:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print the_winner, &quot;won!\n&quot;<br>&nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Its a tie!\n&quot;<br><br>&nbsp;&nbsp;&nbsp; if the_winner == computer:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;As I predicted human, I am triumphant once more. \n&quot; \<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;Proof that computers are superior to humans in all regards.&quot;<br><br>&nbsp;&nbsp;&nbsp; elif the_winner == human:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;Nooooo! It cannot be! Somehow you tricked me human! \n&quot; \
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;But never again.......I will win next time!&quot;<br><br>&nbsp;&nbsp;&nbsp; elif the_winner == TIE:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;You were most lucky human, and somehow managed to tie me! \n&quot; \<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;Celebrate today....for this will not happen again!&quot;
<br><br># set main function<br>def main():<br>&nbsp;&nbsp;&nbsp; display_instruct()<br>&nbsp;&nbsp;&nbsp; computer, human = pieces()<br>&nbsp;&nbsp;&nbsp; turn = X<br>&nbsp;&nbsp;&nbsp; board = new_board()<br>&nbsp;&nbsp;&nbsp; display_board(board)<br><br>&nbsp;&nbsp;&nbsp; while not winner(board):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if turn == human:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; move = human_move(board, human)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; board[move] = human<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; move = computer_move(board, computer, human)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; board[move] = computer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; display_board(board)
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; turn = next_turn(turn)<br><br>&nbsp;&nbsp;&nbsp; the_winner =&nbsp; winner(board)<br>&nbsp;&nbsp;&nbsp; congrat_winner(the_winner, computer, human)<br><br># start the program<br>main()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br>raw_input(&quot;\n\nPress the enter key to exit.&quot;)
<br>-- <br>Best Regards<br><br>Jon Moore