Example #1
0
def bot_wholeply(board, pieces, current_piece):
    from players import available_squares, winning_moves
    squares = available_squares(board)

    # Play a winning move if one exists.
    if current_piece:
        for pos in winning_moves(board, current_piece):
            return pos, None

    # Try to find a combination of a move and piece that doesn't lose on the
    # opponent's next turn.
    for pos in squares:
        test_board = make_move(board, current_piece, pos)
        losing_pieces = set()
        for next_piece in pieces:
            if list(winning_moves(test_board, next_piece)):
                losing_pieces.add(next_piece)
        not_losing_pieces = list(set(pieces) - losing_pieces)
        if not_losing_pieces:
            return pos, random.choice(not_losing_pieces)

    # Nothing intelligent left to do, give up and play randomly.
    pos = None
    if squares:
        pos = random.choice(squares)

    next_piece = None
    if pieces:
        next_piece = random.choice(pieces)

    return pos, next_piece
Example #2
0
def bot_wholeply(board, pieces, current_piece):
    from players import available_squares, winning_moves
    squares = available_squares(board)

    # Play a winning move if one exists.
    if current_piece:
        for pos in winning_moves(board, current_piece):
            return pos, None

    # Try to find a combination of a move and piece that doesn't lose on the
    # opponent's next turn.
    for pos in squares:
        test_board = make_move(board, current_piece, pos)
        losing_pieces = set()
        for next_piece in pieces:
            if list(winning_moves(test_board, next_piece)):
                losing_pieces.add(next_piece)
        not_losing_pieces = list(set(pieces) - losing_pieces)
        if not_losing_pieces:
            return pos, random.choice(not_losing_pieces)

    # Nothing intelligent left to do, give up and play randomly.
    pos = None
    if squares:
        pos = random.choice(squares)

    next_piece = None
    if pieces:
        next_piece = random.choice(pieces)

    return pos, next_piece
Example #3
0
def bot_twoply(board, pieces, current_piece):
    from players import available_squares, winning_moves
    squares = available_squares(board)

    pos = None
    if current_piece:
        winning_squares = winning_moves(board, current_piece)
        if winning_squares:
            pos = random.choice(winning_squares)
            return pos, None
        else:
            pos = random.choice(squares)
        board[pos] = current_piece

    next_piece = None
    if pieces:
        losing_pieces = set()
        for piece in pieces:
            winning_squares = winning_moves(board, piece)
            if winning_squares:
                losing_pieces.add(piece)
        not_losing_pieces = [p for p in pieces if p not in losing_pieces]
        if not_losing_pieces:
            next_piece = random.choice(not_losing_pieces)
        else:
            next_piece = random.choice(pieces)

    return pos, next_piece
Example #4
0
def bot_twoply(board, pieces, current_piece):
    from players import available_squares, winning_moves
    squares = available_squares(board)

    pos = None
    if current_piece:
        winning_squares = winning_moves(board, current_piece)
        if winning_squares:
            pos = random.choice(winning_squares)
            return pos, None
        else:
            pos = random.choice(squares)
        board[pos] = current_piece

    next_piece = None
    if pieces:
        losing_pieces = set()
        for piece in pieces:
            winning_squares = winning_moves(board, piece)
            if winning_squares:
                losing_pieces.add(piece)
        not_losing_pieces = [p for p in pieces if p not in losing_pieces]
        if not_losing_pieces:
            next_piece = random.choice(not_losing_pieces)
        else:
            next_piece = random.choice(pieces)

    return pos, next_piece
Example #5
0
def bot_oneply(board, pieces, current_piece):
    from players import available_squares, winning_moves
    squares = available_squares(board)

    pos = None
    if current_piece:
        winning_squares = winning_moves(board, current_piece)
        if winning_squares:
            pos = random.choice(winning_squares)
            return pos, None
        else:
            pos = random.choice(squares)
        board[pos] = current_piece

    next_piece = None
    if pieces:
        next_piece = random.choice(pieces)

    return pos, next_piece
Example #6
0
def bot_oneply(board, pieces, current_piece):
    from players import available_squares, winning_moves

    squares = available_squares(board)

    pos = None
    if current_piece:
        winning_squares = winning_moves(board, current_piece)
        if winning_squares:
            pos = random.choice(winning_squares)
            return pos, None
        else:
            pos = random.choice(squares)
        board[pos] = current_piece

    next_piece = None
    if pieces:
        next_piece = random.choice(pieces)

    return pos, next_piece