Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import turtle
  2. t = turtle.Turtle()
  3.  
  4. def apply(find, replacement, insert):
  5. for i in range(len(insert)):
  6. if insert[i] == find:
  7. insert[i] = replacement
  8. #print(type(insert))
  9. return(insert)
  10.  
  11. #apply("F",["F","L","F"],["F"])
  12.  
  13. def flatten(insert):
  14. NewInsert = []
  15. for i in insert:
  16. if type(i) == list:
  17. for j in i:
  18. NewInsert.append(j)
  19. else:
  20. NewInsert.append(i)
  21. return(NewInsert)
  22.  
  23. def step(rules, insert):
  24. if type(rules) == list:
  25. for i in rules:
  26. insert = apply(i[0], i[1], insert)
  27. insert = flatten(insert)
  28. #print(type(insert))
  29. return insert
  30. else:
  31. print("Wrong input, need list")
  32.  
  33.  
  34. #print(step([["F",["F","L","F","R","F","L","F"]],["R",["F","R","F"]],["L",["S"]]],["F","R","F","L","R","F"]))
  35.  
  36. def compute(depth, rules, start):
  37. for i in range(depth):
  38. start = step(rules, start)
  39. #print(type(start))
  40. return start
  41.  
  42. #print(compute(2, [["F",["F","L","F","R","F","L","F"]]], ["F"]))
  43.  
  44. # dict args maps to argvalues and dict cmd F maps to fd etc.
  45. def execute(t, length, cmd, args):
  46. cmdarg = zip(cmd, args)
  47. for command, argument in cmdarg:
  48. if command == "fd":
  49. t.fd(length)
  50. elif command == "bk":
  51. t.bk(length)
  52. elif command == "lt":
  53. t.lt(float(argument))
  54. elif command == "rt":
  55. t.rt(float(argument))
  56. elif command == "nop":
  57. pass
  58. elif command == "scale":
  59. length = length * float(argument)
  60. return length
  61.  
  62. data = {}
  63.  
  64. def parse(fdl):
  65. data["commands"] = [] # A list in the dictionary where the commands are stored
  66. data["rules"] = [] # Here the rules are stored
  67. ruleset = open(fdl) # Save the file as variable
  68. templist = ruleset.readlines() # Split into lines
  69. # print(templist)
  70. content = []
  71. for lines in templist: # Here we remove new lines and white space and add to a new list 'content'
  72. line = lines.strip()
  73. content.append(line)
  74. # print(content)
  75. for cmd in content:
  76. if "start" in cmd:
  77. data["start"] = cmd.replace('start ', '') # Adds key called start and adds value after start by removing "start ".
  78. if "rule" in cmd:
  79. splitted = cmd.replace('rule ', '').split(' -> ')
  80. find = splitted[0]
  81. replacement = list(splitted[1].split(' '))
  82. data["rules"].append([find, replacement]) # Appends rule values to rules key so it's of the same form as in the step function
  83. if "length" in cmd:
  84. data["length"] = cmd.replace('length ', '') # Gives length number as value
  85. if "depth" in cmd:
  86. data["depth"] = cmd.replace('depth ', '') # Gives depth number as value
  87. if "cmd" in cmd:
  88. data["commands"].append(cmd.replace('cmd ', '')) # Appends cmd values to command key
  89. #print(data)
  90. #print(data["rules"])
  91.  
  92. #parse("dragon.txt")
  93.  
  94.  
  95. def run(t, fdl):
  96. parse(fdl)
  97. rules = data["rules"]
  98. depth = int(data["depth"])
  99. start = [data["start"]]
  100. length = float(data["length"])
  101. liste = compute(depth, rules, start)
  102. cmd_temp = data["commands"]
  103. #print(depth)
  104. #print(rules)
  105. print(liste)
  106. #print(cmd_temp)
  107. #args = what
  108. #execute(t, length, cmd, args)
  109.  
  110.  
  111. run(t, "dragon.txt")
  112. turtle.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement