def play_game()

in simulation/decai/simulation/simulate_ttt_dt.py [0:0]


def play_game(classifier, tic_tac_toe):
    board = np.zeros((tic_tac_toe.width, tic_tac_toe.length), dtype=np.int8)

    if random.random() < 0.5:
        # Machine is playing.
        pos = classifier.predict(board.flatten())
        board[_map_pos(tic_tac_toe, board, pos)] = 1
    m = {0: '#', 1: 'O', -1: 'X'}
    map_symbols = np.vectorize(lambda x: m[x])

    def print_board(b):
        print(np.array2string(map_symbols(b), formatter={'str_kind': lambda x: x}))

    print(f"The machine is O. You are X.\nPositions:\n{np.arange(board.size).reshape(board.shape)}")
    while True:
        if np.count_nonzero(board) == board.size:
            print("TIE")
            break
        # Person's turn.
        print_board(board)
        while True:
            pos = input("Where would you like to go?")
            pos = _map_pos(tic_tac_toe, board, int(pos.strip()))
            if board[pos] == 0:
                board[pos] = -1
                break
            else:
                print("There is already a value there.")

        winner = tic_tac_toe.get_winner(board)
        if winner is not None:
            print("You WIN!")
            break

        # Machine's turn.
        original_pos = classifier.predict(board.flatten())
        pos = _map_pos(tic_tac_toe, board, original_pos)
        if board[pos] != 0:
            print(f"Machine picked a spot that already has a marker ({original_pos}). This probably means a draw.")
            print_board(board)
            break
        board[pos] = 1

        winner = tic_tac_toe.get_winner(board)
        if winner is not None:
            print("You lose :(")
            break
    print_board(board)