Exemple #1
0
def evolve_playfield(playfield, new_pf, alpaca, verbose=False):
    if playfield.min_y is None:
        return
    bb = BoundingBox(0, 0, 0, 0)
    fit_bounding_box(alpaca, bb)
    y = playfield.min_y - bb.max_dy
    while y <= playfield.max_y - bb.min_dy:
        x = playfield.min_x - bb.max_dx
        while x <= playfield.max_x - bb.min_dx:
            state_id = playfield.get(x, y)
            if verbose:
                print "state at (%d,%d): %s" % (x, y, state_id)
            state_ast = find_state_defn(alpaca, state_id)
            if verbose:
                print " => %r" % state_ast
            classes = state_ast.classes
            rules = state_ast.rules
            new_state_id = apply_rules(alpaca, playfield, x, y, rules, classes, verbose=verbose)
            if new_state_id is None:
                new_state_id = state_id
            if verbose:
                print "new state: %s" % new_state_id
            new_pf.set(x, y, new_state_id)
            x += 1
        y += 1
Exemple #2
0
def evolve_playfield(playfield, new_pf, alpaca):
    # XXX TODO + 1, - 1's in here should reflect the maximum
    # neighbourhood used by any rule
    if playfield.min_y is None:
        return
    y = playfield.min_y - 1
    while y <= playfield.max_y + 1:
        x = playfield.min_x - 1
        while x <= playfield.max_x + 1:
            state_id = playfield.get(x, y)
            #print "state at (%d,%d): %s" % (x, y, state_id)
            state_ast = find_state_defn(alpaca, state_id)
            #print " => %r" % state_ast
            new_state_id = eval_rules(alpaca, playfield, x, y, state_ast.children[3])
            class_decls = state_ast.children[2]
            assert class_decls.type == 'MembershipDecls'
            for class_decl in class_decls.children:
                assert class_decl.type == 'ClassDecl'
                if new_state_id is not None:
                    break
                class_id = class_decl.value
                class_ast = find_class_defn(alpaca, class_id)
                new_state_id = eval_rules(alpaca, playfield, x, y, class_ast.children[0])
            if new_state_id is None:
                new_state_id = playfield.get(x, y)
            #print "new state: %s" % new_state_id
            new_pf.set(x, y, new_state_id)
            x += 1
        y += 1
Exemple #3
0
def evolve_playfield(playfield, new_pf, alpaca, verbose=False):
    if playfield.min_y is None:
        return
    bb = BoundingBox(0, 0, 0, 0)
    fit_bounding_box(alpaca, bb)
    y = playfield.min_y - bb.max_dy
    while y <= playfield.max_y - bb.min_dy:
        x = playfield.min_x - bb.max_dx
        while x <= playfield.max_x - bb.min_dx:
            state_id = playfield.get(x, y)
            if verbose:
                print "state at (%d,%d): %s" % (x, y, state_id)
            state_ast = find_state_defn(alpaca, state_id)
            if verbose:
                print " => %r" % state_ast
            classes = state_ast.classes
            rules = state_ast.rules
            new_state_id = apply_rules(alpaca,
                                       playfield,
                                       x,
                                       y,
                                       rules,
                                       classes,
                                       verbose=verbose)
            if new_state_id is None:
                new_state_id = state_id
            if verbose:
                print "new state: %s" % new_state_id
            new_pf.set(x, y, new_state_id)
            x += 1
        y += 1
Exemple #4
0
def eval_relation(alpaca, playfield, x, y, state_id, ast):
    """state_id is the ID of a state (possibly from the playfield)
    that we want to check.  ast is either a StateRef or a ClassDecl.
    Returns true iff the state_id satisfies the StateRef or ClassDecl.

    """
    if ast.type == "ClassDecl":
        class_id = ast.value
        state_ast = find_state_defn(alpaca, state_id)
        return state_defn_is_a(alpaca, state_ast, class_id)
    elif ast.type in ("StateRefEq", "StateRefRel"):
        pf_state_id = eval_state_ref(playfield, x, y, ast)
        return state_id == pf_state_id
Exemple #5
0
def eval_relation(alpaca, playfield, x, y, state_id, ast, verbose=False):
    """state_id is the ID of a state (possibly from the playfield)
    that we want to check.  ast is either a StateRef or a ClassDecl.
    Returns true iff the state_id satisfies the StateRef or ClassDecl.

    """
    if ast.type == 'ClassDecl':
        class_id = ast.id
        state_ast = find_state_defn(alpaca, state_id)
        result = state_defn_is_a(alpaca, state_ast, class_id, verbose=verbose)
        if verbose:
            print " => checking if {} is_a {}: {}".format(state_id, class_id, result)
        return result
    elif ast.type in ('StateRefEq', 'StateRefRel'):
        pf_state_id = eval_state_ref(playfield, x, y, ast)
        return state_id == pf_state_id
Exemple #6
0
def eval_relation(alpaca, playfield, x, y, state_id, ast, verbose=False):
    """state_id is the ID of a state (possibly from the playfield)
    that we want to check.  ast is either a StateRef or a ClassDecl.
    Returns true iff the state_id satisfies the StateRef or ClassDecl.

    """
    if ast.type == 'ClassDecl':
        class_id = ast.id
        state_ast = find_state_defn(alpaca, state_id)
        result = state_defn_is_a(alpaca, state_ast, class_id, verbose=verbose)
        if verbose:
            print " => checking if {} is_a {}: {}".format(
                state_id, class_id, result)
        return result
    elif ast.type in ('StateRefEq', 'StateRefRel'):
        pf_state_id = eval_state_ref(playfield, x, y, ast)
        return state_id == pf_state_id