Пример #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
Пример #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
Пример #3
0
def game(player_a, player_b):
    """
    Play a game between two player functions.
    """
    pieces = list(piece_descriptions.keys())

    # Board mapping positions to pieces currently occupying that position.
    board = {}
    for x in range(SIZE):
        for y in range(SIZE):
            board[(x, y)] = None

    current_piece = None
    for score, current_player in itertools.cycle([(+1, player_a),
                                                  (-1, player_b)]):
        # Remove the current piece from available pieces, and give it to the current player.
        if current_piece:
            pieces.remove(current_piece)
        # Current player places the piece at `pos`, and chooses a piece for the opponent.
        pos, next_piece = current_player(copy(board), copy(pieces),
                                         current_piece)
        #print('player moved at %s, and chose %s' % (pos, next_piece))
        if pos:
            assert board[
                pos] is None  # Don't allow playing on occupied positions.
            board = make_move(board, current_piece, pos)
        current_piece = next_piece

        #print(board_string(board))

        if is_win(board):
            #print('%s wins!' % current_player.__name__)
            return score

        if not pieces:
            # Game over, draw.
            #print('draw')
            return 0