Пример #1
0
def winning_moves(board, piece):
    """
    >>> from quarto import parse_board, parse_piece
    >>> board = parse_board('''
    ...     -086
    ...     cd-3
    ...     -a49
    ...     1b7-
    ... ''')
    >>> winning_moves(board, parse_piece('2'))
    [(0, 0)]
    >>> winning_moves(board, parse_piece('5'))
    [(3, 3)]
    >>> winning_moves(board, parse_piece('e'))
    [(0, 0)]
    >>> winning_moves(board, parse_piece('f'))
    [(3, 3)]
    """
    winning_squares = []
    for square in available_squares(board):
        test_board = copy(board)
        test_board[square] = piece
        if is_win(test_board):
            winning_squares.append(square)
    return winning_squares
Пример #2
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