Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.01 KB | None | 0 0
  1. # Skeleton file for BVP Practicum.
  2. #
  3. # You have to implement all of the functions in this file.
  4. #
  5. # If you implement them correctly, you can run this file to play the Slither game.
  6. # For that to work, this file has to be in the same directory as
  7. # the `slitherlib.py` file we provided.
  8. from slitherlib import Direction, Slither
  9.  
  10. def create_new_world(game_size_x, game_size_y):
  11. """
  12. Creates a new world with dimensions game_size_x by game_size_y.
  13. The player's snake has length one (no body, just a head) and is positioned in the middle of the world.
  14. In case of an even game dimension, you may choose whether to round up or down.
  15. The player's snake starts out facing down.
  16. The world also includes food and other snakes at random locations.
  17. The value of the food is a random natural number between 1 and 9 (including 1 and 9).
  18. Every (empty) location then has a 1% probability of containing an enemy snake head.
  19. Every (still empty) location has a 2.5% probability of containing food.
  20.  
  21. Note that the world description as you create it here will be passed on to the other functions that you will
  22. implement, such as game_to_string and forward. It should contain all the information that you need to implement
  23. these functions; you are free to choose your own representation.
  24.  
  25. You may *optionally* give enemy snakes a body in addition to a head.
  26. If you want do to this, you can do so as follows:
  27. 75% of enemy snakes are of length 2 or higher, (75%)^2 of snakes are of length 3 or higher and so on.
  28. (so with 75% probability, you increase the length of the Snake by one and try increasing again)
  29. With every move they made, enemy snakes moved forward with a probability of 75%.
  30. When enemy snakes turn, they do so with equal probability in either direction.
  31. (so never to the direction opposite to their current one).
  32. Enemy snakes may overlap.
  33.  
  34. :param game_size_x: Horizontal size of the game world.
  35. :param game_size_y: Verical size of the game world.
  36. :returns A data structure that holds all information about a new world.
  37. """
  38. world={}
  39. grid=[]
  40. length=0
  41. MaxScore=0
  42. food=[1,2,3,4,5,6,7,8,9]
  43. direction=Direction.DOWN
  44. for i in range (game_size_y):
  45. grid.append([])
  46. for j in range (game_size_x):
  47. grid[i].append(".")
  48.  
  49. for i in range (game_size_y):
  50. for j in range (game_size_x):
  51. if grid[i][j]==".":
  52. p=randint(0,99)
  53. if p==7:
  54. grid[i][j]="X"
  55. else:
  56. g=randint(0,39)
  57. if g==6:
  58. r=randint(1,9)
  59. grid[i][j]=r
  60. MaxScore+=r
  61. world["grid"]=grid
  62. world["length"]=length
  63. world["location"]=(game_size_x//2, game_size_y//2)
  64. world["direction"]=direction
  65. world["MaxScore"]=MaxScore
  66. world["food"]=food
  67.  
  68. return world
  69.  
  70. def game_to_string(world):
  71. """
  72. Returns a string representation of the given world description.
  73. Use the following characters to represent game elements:
  74. * A "·" character for empty space
  75. * A number for a piece of food, where the number corresponds to the value of the food (1, 2, ... or 9).
  76. * A "X" character for a piece of enemy snake
  77. * A "O" character for the player's snake's head
  78. * A "o" character for a piece of the player's snake's body
  79.  
  80. Example output:
  81. ························X··o··
  82. ························Oooo··
  83. ························X··o··
  84. ························X··o··
  85. ························X··o··
  86. ······················9·X··o··
  87. ························X··o3·
  88.  
  89.  
  90. :param world: Data structure holding information about the world as returned by create_new_world.
  91. :returns A multi-line string representing the given world description.
  92. """
  93. string=""
  94. for i in range (len(world["grid"])):
  95. for j in range(len(world["grid"][i])):
  96. if world["grid"][i][j]==".":
  97. string+="."
  98. elif world["grid"][i][j]=="X":
  99. string+="X"
  100. elif world["grid"][i][j] in world["food"]:
  101. string+=world["grid"][i][j]
  102. elif world["grid"][i][j]=="body":
  103. string+="o"
  104. elif world["grid"][i][j]=="head":
  105. string+="O"
  106. string+="\n"
  107. return string
  108.  
  109. def forward(world):
  110. """
  111. If the game is over, this function does nothing.
  112. If the game is ongoing, this function modifies the given world description by moving the player's snake one step
  113. along its current direction.
  114.  
  115. When moving over a piece of food, we say the snake eats the food.
  116. When eating a piece of food of value X, the player's snake should increase in length by X.
  117. The player's score should also increase by X.
  118. A snake's length is increased by moving its head while leaving the tail in place. It therefore takes
  119. some turns for food to be "processed". Snakes can eat more food while still processing old food.
  120.  
  121. :param world: Data structure holding information about the world as returned by create_new_world.
  122. """
  123. if not is_game_over(world):
  124. if world["direction"]==Direction.Down:
  125. world["location"][1]-=1
  126. elif world["direction"]==Direction.Up:
  127. world["location"][1]+=1
  128. elif world["direction"]==Direction.Left:
  129. world["location"][2]-=1
  130. elif world["direction"]==Direction.Right:
  131. world["location"][2]+=1
  132. if world["grid"]["location"[1]]["location"[2]] in world["food"]:
  133. world["length"]+=world["grid"]["location"[1]]["location"[2]]
  134. for i in range(world["length"]):
  135. world["grid"]["location"[1]]["location"[2]]="body"
  136. forward(world)
  137.  
  138. return world
  139.  
  140.  
  141. def turn_and_forward(world, direction):
  142. """
  143. If the game is over, this function does nothing.
  144. If the game is ongoing, this function modifies the given world description by moving the player's snake one step
  145. along the given direction. It also changes the player's snake's current direction to the given direction.
  146. If the given direction points "into" the snake, ignore the given direction and just move the players' snake along
  147. its current direction. Do not change the direction of the snake.
  148.  
  149. For other things that happen when moving forward, see the documentation of the function forward.
  150.  
  151. :param world: Data structure holding information about the world as returned by create_new_world.
  152. :param direction: Direction the player's snake should turn towards.
  153. direction is equal to either Direction.UP, Direction.RIGHT, Direction.DOWN or Direction.LEFT.
  154. """
  155. if not game_to_string(world):
  156. if direction==Direction.DOWN and world["direction"]!=Direction.UP:
  157. world["location"][1]-=1
  158. elif direction==Direction.UP and world["direction"]!=Direction.DOWN:
  159. world["location"][1]+=1
  160. elif direction==Direction.LEFT and world["direction"]!=Direction.RIGHT:
  161. world["location"][2]-=1
  162. elif direction==Direction.RIGHT and world["direction"]!=Direction.LEFT:
  163. world["location"][2]+=1
  164. else:
  165. forward(world)
  166. world["direction"]=direction
  167. return world
  168.  
  169. def is_game_over(world):
  170. """
  171. Returns true iff this game is over and false iff the game is ongoing.
  172. The game is over when the snake has bumped into a square that contains another snake or when all the food is gone.
  173. Note that snakes are allowed to move "through" themselves; doing so does not end the game!
  174.  
  175. :param world: Data structure holding information about the world as returned by create_new_world.
  176. """
  177. is_game_over=False
  178. if world["grid"]["location"[1]]["location"[2]]=="X" or world["length"]==world["MaxScore"]:
  179. is_game_over=True
  180.  
  181. return is_game_over
  182.  
  183.  
  184. def get_score(world):
  185. """
  186. Returns the player's score in the given world.
  187. In a new world, player's scores are equal to zero. They are increased when eating food as described in forward.
  188.  
  189. :param world: Data structure holding information about the world as returned by create_new_world.
  190. :return: The player's score as an integer.
  191. """
  192. return world["length"]
  193.  
  194. # We put our "main" function here.
  195. if __name__ == "__main__":
  196. slither = Slither(create_new_world, game_to_string, forward, turn_and_forward, is_game_over, get_score)
  197. # Optionally set the speed at which the snake moves (expressed in milliseconds between ticks)
  198. # slither.set_waiting_time(1000)
  199. # We then start the GUI and start moving the snake; start will call back on your functions.
  200. slither.start(30, 10)
  201. # Functionally, the start function behaves as follows:
  202. #
  203. # def start(game_size_x, game_size_y):
  204. # world = create_new_world(game_size_x, game_size_y)
  205. # new_direction = None
  206. # print("Score: %i" % get_score(world))
  207. # print(game_to_string(world))
  208. # time.sleep(waiting_time/1000)
  209. # while not is_game_over(world):
  210. # if new_direction:
  211. # turn_and_forward(world, new_direction)
  212. # new_direction = None
  213. # else:
  214. # forward(world)
  215. # print("Score: %i" % get_score(world))
  216. # print(game_to_string(world))
  217. # time.sleep(waiting_time/1000)
  218. # print("Game over :(")
  219. # print("Final score: %i" % get_score(world))
  220. # print(game_to_string(world))
  221. #
  222. # new_direction is set whenever the user presses an arrow key, to the direction corresponding to the key press.
  223. # waiting_time is controlled by set_waiting_time as shown above; it has a default value of 500.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement