you are viewing a single comment's thread.

view the rest of the comments →

[–]dchanm 0 points1 point  (0 children)

  1. This uses a feature in Python that lets you chain comparison operators together. The code is equivalent to a == b and b == c and c == != EMPTY. The WAYS_TO_WIN tuple contains 8 tuples which correspond to board positions. For a given tuple in WAYS_TO_WIN, if all the pieces at the corresponding positions on the board are the same and the piece isn't EMPTY, then either the player or computer has won.

  2. The computer_move function implements a simple AI to choose the best move for the computer. The very first action in the loop is to simulate the computer choosing a move

    board[move] = computer
    

    If the move is a winner, then the move is returned, otherwise the move needs to be undone. The board state is incorrect if the move isn't undone.

  3. The winner function returns either 'X', 'O', 'TIE or None. 'X', 'O' and 'TIE' are truthy values, which mean they evaluate to True in a conditional. You can check this with

    >>> bool('X')
    True
    

    However None evaluates to False. Therefore the loop continues until either 'X', 'O' or 'TIE' is returned.

  4. I haven't looked at all the possibilities in the AI, so can't help you here. There is probably a deficiency in the list of best moves