main.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. from karel.stanfordkarel import *
  2. """
  3. Inspired by John Conways "Game of Life" and "Biogenesis" by Joan Queralt Molina.
  4. The three phases of life start with Karel seeding a few fountains of youth, represented by beepers on black patches of "land".
  5. Than the circle of life commences, with Karel being the fickle hand of god that moves randomly over the board,
  6. at certain times deciding to bless or smite the life she passes.
  7. Being blessed means growth and changing colour, while smiting leads to permanent death or being composted.
  8. After several steps of growth it is possible for life to reproduce, and spread to other patches of land;
  9. possible in the process overwhelming neighboring life.
  10. After an ungodly amount of divine acts, in the final ascension phase, the fountains are collected by their creatress Karel;
  11. leaving only the result of the onslaught of the competition that is life.
  12. TIP: Run at top speed to spare your own life from wasting away...
  13. """
  14. ## Helper functions:
  15. # Turn left trice:
  16. def turn_right():
  17. for i in range(3):
  18. turn_left()
  19. # Keep moving while not blocked:
  20. def move_till_end():
  21. while front_is_clear():
  22. move()
  23. # 20% chance to move:
  24. def perhaps_move():
  25. if random(0.2):
  26. move()
  27. def pick_if_present():
  28. if beepers_present():
  29. pick_beeper()
  30. # Move to next row:
  31. def next_row():
  32. # Turn west:
  33. if facing_east():
  34. turn_left()
  35. if front_is_clear():
  36. move()
  37. turn_left()
  38. # Turn east
  39. else: # Facing west
  40. turn_right()
  41. if front_is_clear():
  42. move()
  43. turn_right()
  44. def wall_check():
  45. if front_is_blocked(): # End of row
  46. next_row()
  47. # Move to southeast corner:
  48. def genesis_position():
  49. # Move south:
  50. while not_facing_south():
  51. turn_left()
  52. move_till_end()
  53. # Move west:
  54. while not_facing_west():
  55. turn_left()
  56. move_till_end()
  57. # Turn:
  58. while not_facing_east():
  59. turn_left()
  60. # Do not face walls, and sometimes change direction:
  61. def orient():
  62. # Perhaps turn
  63. if random(0.1): # 10% chance to turn
  64. if random(0.5): # 50% chance to go right or left
  65. turn_left()
  66. else:
  67. turn_right()
  68. # Make sure not to face an obstruction
  69. while front_is_blocked():
  70. turn_left()
  71. ## Main functions:
  72. # According to biologists, the sole goal of life.
  73. def reproduce():
  74. move()
  75. if no_beepers_present(): # Unseeded tile
  76. # Seed life
  77. paint_corner("black")
  78. put_beeper()
  79. else: # Seeded tile
  80. if random(0.5): # 50% chance
  81. paint_corner("orange") # Accelerate life or overtake another life.
  82. else: # Remainder 50% chance
  83. die() # Compete, cancelling each other out.
  84. # Never leave Karel facign a wall:
  85. orient()
  86. def die():
  87. # Permanent death:
  88. if random(0.2): # 20% chance
  89. paint_corner("white")
  90. pick_beeper()
  91. # Seed for rebirth | Composting:
  92. else: # Remainder 80% chance
  93. paint_corner("black")
  94. # Grow, die or reproduce:
  95. def paint_life():
  96. # In reverse order, since "elif" is not available.
  97. #if corner_color_is("pink"): # End condition, but "quit()" is not available
  98. # quit()
  99. if corner_color_is("cyan"):
  100. if random(0.8): # 80% chance
  101. paint_corner("pink") # Evolve
  102. # Reproduce quadrice:
  103. for i in range(4):
  104. reproduce()
  105. else: # Remainder 20% chance
  106. if random(0.2): # 20% chance of death, else stagnation.
  107. die() # Death
  108. if corner_color_is("green"):
  109. if random(0.8): # 80% chance
  110. paint_corner("cyan") # Evolve
  111. # Reproduce thrice:
  112. for i in range(3):
  113. reproduce()
  114. else: # Remainder 20% chance
  115. if random(0.2): # 20% chance of death, else stagnation.
  116. die() # Death
  117. if corner_color_is("blue"):
  118. if random(0.8): # 80% chance
  119. paint_corner("green") # Evolve
  120. # Reproduce twice:
  121. for i in range(2):
  122. reproduce()
  123. else: # Remainder 20% chance
  124. if random(0.2): # 20% chance of death, else stagnation.
  125. die() # Death
  126. if corner_color_is("olive"):
  127. if random(0.6): # 60% chance
  128. paint_corner("blue") # Evolve
  129. reproduce()
  130. else: # Remainder 40% chance
  131. if random(0.2): # 20% chance of death, else stagnation.
  132. die() # Death
  133. if corner_color_is("orange"):
  134. if random(0.5): # 50% chance
  135. paint_corner("olive") # Evolve
  136. else: # Remainder 50% chance
  137. if random(0.2): # 20% chance of death, else stagnation.
  138. die() # Death
  139. if corner_color_is("red"): # Adolescence
  140. if random(0.4): # 40% chance
  141. paint_corner("orange") # Evolve
  142. else: # Remainder 60% chance
  143. if random(0.2): # 20% chance of death, else stagnation.
  144. die() # Death
  145. if corner_color_is("purple"): # Early stage
  146. if random(0.3): # 30% chance
  147. paint_corner("red") # Evolve
  148. else: # Remainder 70% chance
  149. if random(0.2): # 20% chance of death, else stagnation.
  150. die() # Death
  151. if corner_color_is("brown"): # Infancy
  152. if random(0.2): # 20% chance
  153. paint_corner("purple") # Evolve
  154. else: # Remainder 80% chance
  155. if random(0.2): # 20% chance of death, else stagnation.
  156. die() # Death
  157. if corner_color_is("gray"): # Prenatal
  158. if random(0.1): # 10% chance
  159. paint_corner("brown") # Evolve
  160. else: # Remainder 90% chance
  161. if random(0.2): # 20% chance of death, else stagnation.
  162. die() # Death
  163. if corner_color_is("black"): # Field is seeded
  164. paint_corner("gray") # Sprout life
  165. # Prepare the world with habitable patches.
  166. def seed():
  167. genesis_position()
  168. while front_is_clear():
  169. # Perhaps seed a fountain of life
  170. if random(0.3): # 30% chance
  171. paint_corner("black")
  172. put_beeper()
  173. move()
  174. wall_check()
  175. # Face away from the wall
  176. orient()
  177. # The circle of life
  178. def life_loop():
  179. for i in range(9999): # Incubation period
  180. # Path of god, played by karel:
  181. for step in range(9): # Travel period
  182. perhaps_move()
  183. orient()
  184. paint_life()
  185. # End the game neatly:
  186. def ascension():
  187. genesis_position()
  188. # Remove seeds
  189. while front_is_clear():
  190. pick_if_present()
  191. move()
  192. pick_if_present()
  193. wall_check()
  194. genesis_position()
  195. # The three phases of life:
  196. def main():
  197. seed()
  198. life_loop()
  199. ascension() # The script should not run forever, making some unintended biblical implications...
  200. # don't change this code
  201. if __name__ == '__main__':
  202. main()