Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- t = turtle.Turtle()
- def apply(find, replacement, insert):
- for i in range(len(insert)):
- if insert[i] == find:
- insert[i] = replacement
- #print(type(insert))
- return(insert)
- #apply("F",["F","L","F"],["F"])
- def flatten(insert):
- NewInsert = []
- for i in insert:
- if type(i) == list:
- for j in i:
- NewInsert.append(j)
- else:
- NewInsert.append(i)
- return(NewInsert)
- def step(rules, insert):
- if type(rules) == list:
- for i in rules:
- insert = apply(i[0], i[1], insert)
- insert = flatten(insert)
- #print(type(insert))
- return insert
- else:
- print("Wrong input, need list")
- #print(step([["F",["F","L","F","R","F","L","F"]],["R",["F","R","F"]],["L",["S"]]],["F","R","F","L","R","F"]))
- def compute(depth, rules, start):
- for i in range(depth):
- start = step(rules, start)
- #print(rules)
- return start
- #print(compute(2, [["F",["F","L","F","R","F","L","F"]], ["X",["J","E","G"]]], ["F","X"]))
- # dict args maps to argvalues and dict cmd F maps to fd etc.
- def execute(t, length, cmd, args):
- cmdarg = zip(cmd, args)
- for command, argument in cmdarg:
- if command == "fd":
- t.fd(length)
- elif command == "bk":
- t.bk(length)
- elif command == "lt":
- t.lt(float(argument))
- elif command == "rt":
- t.rt(float(argument))
- elif command == "nop":
- pass
- elif command == "scale":
- length = length * float(argument)
- return length
- data = {}
- def parse(fdl):
- data["commands"] = [] # A list in the dictionary where the commands are stored
- data["rules"] = [] # Here the rules are stored
- ruleset = open(fdl) # Save the file as variable
- templist = ruleset.readlines() # Split into lines
- # print(templist)
- content = []
- for lines in templist: # Here we remove new lines and white space and add to a new list 'content'
- line = lines.strip()
- content.append(line)
- # print(content)
- for cmd in content:
- if "start" in cmd:
- data["start"] = cmd.replace('start ', '').split(' ') # Adds key called start and adds value after start by removing "start ".
- if "rule" in cmd:
- splitted = cmd.replace('rule ', '').split(' -> ')
- find = splitted[0]
- replacement = list(splitted[1].split(' '))
- data["rules"].append([find, replacement]) # Appends rule values to rules key so it's of the same form as in the step function
- if "length" in cmd:
- data["length"] = cmd.replace('length ', '') # Gives length number as value
- if "depth" in cmd:
- data["depth"] = cmd.replace('depth ', '') # Gives depth number as value
- if "cmd" in cmd:
- data["commands"].append(cmd.replace('cmd ', '')) # Appends cmd values to command key
- #print(data)
- #print(data["rules"])
- #parse("dragon.txt")
- def run(t, fdl):
- parse(fdl)
- rules = data["rules"]
- depth = int(data["depth"])
- start = [data["start"]]
- length = float(data["length"])
- liste = compute(depth, rules, start)
- cmd_temp = data["commands"]
- #print(liste)
- #cmd = what
- #args = what
- #execute(t, length, cmd, args)
- run(t, "dragon.txt")
- turtle.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement