# (See accompanying file LICENSE_1_0.txt or copy at # http://www.boost.org/LICENSE_1_0.txt) """Tic-tac-toe game where the computer player goes first.""" import ai import rules player_char = 'o' comp_char = 'x' board = [' '] * 9 winner = None first_move = True legend = "123456789" rules.printBoard(legend) print "\nComputer goes first\n" while not winner and not rules.isFull(board): # On the first turn, skip the human player to let the computer go first. if not first_move: # Player's turn board[ai.humanPlayer(board)] = player_char winner = rules.getWinner(board) # Computer's turn if not winner and not rules.isFull(board): if first_move: board[ai.firstMove()] = comp_char else: # Don't care what the score was, just take the best move.
first_move = True while not winner and not rules.isFull(board): # Skip the naive player's first turn if the smart player goes first. if not first_move or naive_goes_first: # Naive player's turn. if first_move: board[ai.firstMove()] = naive_char else: board[ai.tryToBeSmart(board, naive_char)] = naive_char winner = rules.getWinner(board) first_move = False if not winner and not rules.isFull(board): # Smart player's turn if first_move: board[ai.firstMove()] = smart_char else: # Don't care what the score was, just take the best move. _, move = ai.negamax(board, smart_char) assert move is not None board[move] = smart_char winner = rules.getWinner(board) first_move = False # Either the game just ended or we're on to the next round. rules.printBoard(board) print rules.printEndGame(winner)