Ejemplo n.º 1
0
def check_lose(board, moves, play_sym):
    temp_board = Board()
    for move in moves:
        temp_board.board = [row[:] for row in board.board]
        temp_board.add_move(*move, play_sym)
        if temp_board.check_winner()[0]:
            return move
Ejemplo n.º 2
0
def get_best_move(board, comp_sym, play_sym):
    best_score, best_move = -10, None
    temp_board = Board()
    for move in get_open_squares(board):
        temp_board.board = [row[:] for row in board.board]
        temp_board.add_move(*move, comp_sym)
        score = minimax(temp_board, False, comp_sym, play_sym, 1)
        if score > best_score:
            best_score = score
            best_move = move
    return best_move
Ejemplo n.º 3
0
def minimax(board, maximizing, comp_sym, play_sym, depth):
    if board.check_winner() == (True, comp_sym): #computer wins
        return 1 / depth
    elif board.check_winner()[0]: #player wins
        return -1 / depth
    elif len(get_open_squares(board)) == 0: #draw
        return 0

    moves = get_open_squares(board)
    scores = []
    temp_board = Board()
    for move in moves:
        temp_board.board = [row[:] for row in board.board]
        if maximizing:
            player = comp_sym
        else:
            player = play_sym
        temp_board.add_move(*move, player)
        scores.append(minimax(temp_board, not maximizing, comp_sym, play_sym, depth + 1))
    if maximizing:
        return max(scores)
    return min(scores)
Ejemplo n.º 4
0
def check_over(board):
    print board
    state, winner = board.get_state()
    if state == 'WINNER':
        print state, winner
        return True
    elif state == 'TIE':
        print state
        return True
    return False


if __name__ == '__main__':
    board = Board()
    print board
    print '\n---'

    while True:
        i, j = map(int, raw_input().split(' '))
        board = board.add_move(Move(Piece(PLAYER1, i, j)))
        if check_over(board):
            break
        print '\n---'

        move = get_best_move(PLAYER2, PLAYER1, board, verbose=True)
        board = board.add_move(move)
        if check_over(board):
            break
        print '\n---'
Ejemplo n.º 5
0
        return True
    elif state == 'TIE':
        print state
        return True
    return False


if __name__ == '__main__':
    if len(argv) > 1 and argv[1] == '--nogui':
        board = Board()
        print board
        print '\n---'

        while True:
            i, j = map(int, raw_input().split(' '))
            board = board.add_move(Move(Piece(PLAYER1, col=i, row=j)))
            if check_over(board):
                break
            print '\n---'

            move = get_best_move(PLAYER2, PLAYER1, board)
            board = board.add_move(move)
            if check_over(board):
                break
            print '\n---'

    elif len(argv) > 1 and argv[1] == 'auto':
        iterations = int(argv[2])
        for _ in range(iterations):
            board = Board()
            smart = choice([PLAYER1, PLAYER2])