<div dir="ltr">Dear all,<div><br></div><div>Has anyone solved the fourth challenge in Chapter 6 of Michael Dawson's book, 'Python Programming for the absolute beginner'?</div><div><br></div><div>The challenge: 'Write a new <i>computer_move()</i> function for the Tic-Tac-Toe game to plug the hole in the computer's strategy.  See if you can create an opponent that is unbeatable!'</div><div><br></div><div>The function is as follows: </div><div><br></div><div><div>def computer_move(board, computer, human):</div><div>    """Make computer move."""</div><div>    # make a copy to work with since function will be changing list</div><div>    board = board[:]</div><div><b>    # the best positions to have, in order</b></div><div><b>    BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)</b></div><div><br></div><div>    print("I shall take square number", end=" ")</div><div>    </div><div>    # if computer can win, take that move</div><div>    for move in legal_moves(board):</div><div>        board[move] = computer</div><div>        if winner(board) == computer:</div><div>            print(move)</div><div>            return move</div><div>        # done checking this move, undo it</div><div>        board[move] = EMPTY</div><div>    </div><div>    # if human can win, block that move</div><div>    for move in legal_moves(board):</div><div>        board[move] = human</div><div>        if winner(board) == human:</div><div>            print(move)</div><div>            return move</div><div>        # done checkin this move, undo it</div><div>        board[move] = EMPTY</div><div><br></div><div>    <b># since no one can win on next move, pick best open square</b></div><div><b>    for move in BEST_MOVES:</b></div><div><b>        if move in legal_moves(board):</b></div><div><b>            print(move)</b></div><div><b>            return move</b></div></div><div><br></div><div>I believe a solution lies in the final lines, which I put in bold, and in the BEST_MOVES tuple, which is also in bold.  As in, it looks through the list of best moves in order regardless of what move the opponent has made.  So it just dumbly selects the next item in the tuple instead of responding to the opponent's move.  So, for example, the following sequence shows the computer's weakness:</div><div><br></div><div>a. the opponent goes first in a corner, in Square 0</div><div>b. the computer picks the centre, in Square 5</div><div>c. the opponent picks the opposite corner, Square 8</div><div>d. the weakness - the computer picks the corner, Square 2, which will be easily countered by the opponent going in Square 6, instead of the smarter Square 1, which will result in a draw, and not a loss.</div><div><br></div><div>HAS ANYONE SOLVED THIS?  I'd much appreciate help here.</div></div>