Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. """Simulation."""
  2.  
  3. from typing import Tuple
  4.  
  5.  
  6. def simulate(world_map: list, flight_plan: list) -> list:
  7. """
  8. Simulate a flying space ship fighting space pirates.
  9.  
  10. :param world_map: A list of strings indicating rows that make up the space map.
  11. The space map is always rectangular and the minimum given size is 1x1.
  12. Space pirate free zone is indicated by the symbol ('-'), low presence by ('w') and high presence by ('W').
  13. The ship position is indicated by the symbol ('X'). There is always one ship on the space map.
  14. Asteroid fields are indicated by the symbol ('#').
  15.  
  16. :param flight_plan: A list of moves.
  17. The moves are abbreviated N - north, E - east, S - south, W - west.
  18. Ignore moves that would put the ship out of bounds or crash it into an asteroid field.
  19.  
  20. :return: A list of strings indicating rows that make up the space map. Same format as the given wmap.
  21.  
  22. Pirates under Shippy's starting position are always eliminated ('-').
  23. If Shippy fights pirates in high presence area, it first turns into low presence ('w')
  24. and then from low presence into no presence area ('-').
  25. """
  26. row_num = 0
  27. world_map_dict = {}
  28. coast_dict = {}
  29.  
  30. coast_dict["S"] = (1, 0)
  31. coast_dict["N"] = (-1, 0)
  32. coast_dict["E"] = (0, 1)
  33. coast_dict["W"] = (0, -1)
  34. s_ship_loc = ()
  35. for row in world_map:
  36. map_width = len(row)
  37. map_height = len(world_map)
  38. elem_num = 0
  39. for element in row:
  40. world_map_dict[(row_num, elem_num)] = element
  41. if element == "X":
  42. s_ship_loc = (row_num, elem_num)
  43. elem_num += 1
  44. row_num += 1
  45. move_num = 0
  46. for move in flight_plan:
  47. unableToMove = False
  48. loc_after_move = (s_ship_loc[0] + coast_dict[move][0], s_ship_loc[1] + coast_dict[move][1])
  49. # Kas tahab kaardi pikkusest välja minna?
  50. if loc_after_move[0] > map_height or loc_after_move[0] < 0:
  51. unableToMove = True
  52. # Kas tahab kaardi laiusest välja minna?
  53. if loc_after_move[1] > map_width or loc_after_move[1] < 0:
  54. unableToMove = True
  55. # Kas tahab asteroidile otsa sõita?
  56. if world_map_dict[loc_after_move] == "#":
  57. unableToMove = True
  58. # Out of bounds
  59. if world_map_dict[loc_after_move] not in world_map_dict.values():
  60. unableToMove = True
  61.  
  62. move_num += 1
  63. if not unableToMove:
  64. if move_num == 1:
  65. world_map_dict[s_ship_loc] = "-"
  66. world_map_dict[loc_after_move] = "-"
  67. s_ship_loc = loc_after_move
  68. elif move_num > 1:
  69. s_ship_loc = loc_after_move
  70. if world_map_dict[loc_after_move] == "W":
  71. world_map_dict[loc_after_move] = "w"
  72. else:
  73. world_map_dict[loc_after_move] = "-"
  74.  
  75. if unableToMove:
  76. world_map_dict[s_ship_loc] = "X"
  77. else:
  78. world_map_dict[loc_after_move] = "X"
  79. world_map_values = list(world_map_dict.values())
  80. c = 0
  81. final_list = []
  82.  
  83. for x in world_map:
  84. c += map_width
  85. a = "".join(world_map_values[c-map_width:c])
  86. final_list.append(a)
  87.  
  88. return final_list
  89.  
  90.  
  91.  
  92. def list_to_dictionary_converter(world_map: list) -> Tuple[dict, int, int]:
  93. """
  94. Convert a list to dictionary using coordinates as keys.
  95.  
  96. :param world_map: list of strings.
  97. :return: dictionary of the space, shippy y position, shippy x position
  98.  
  99. Map tile under Shippy's location is marked as "-" or no presence area.
  100. Dictionaries key is a Tuple which has Y-position as the first value and X-position as
  101. the second value. If there is no Shippy (Marked as X in the list) in the list, the
  102. coordinates are marked as 0 and 0.
  103. """
  104. row = 0
  105. n_dict = {}
  106. s_loc = (0, 0)
  107. for x in world_map:
  108. ix = 0
  109. for char in x:
  110. n_dict[row, ix] = char
  111. if char == "X":
  112. s_loc = (row, ix)
  113. n_dict[row, ix] = "-"
  114. ix += 1
  115. row += 1
  116. dict_tuple = (n_dict, s_loc[0], s_loc[1])
  117. return dict_tuple
  118.  
  119. def dictionary_to_list_converter(space_map: dict, width: int, height: int) -> list:
  120. """
  121. Convert dictionary of coordinates to list of strings.
  122.  
  123. :param space_map: Dictionary of the space
  124. :param width: Width of the world.
  125. :param height: Height of the world.
  126. :return: List of strings
  127.  
  128. PS: You should add Shippy back the the dictionary before you call this method.
  129. """
  130. pass
  131.  
  132.  
  133. if __name__ == '__main__':
  134. space_list1 = [
  135. "#www-",
  136. "wXw#-",
  137. ]
  138.  
  139. flight_plan1 = ["N", "E", "E", "S", "E"]
  140. print("\n".join(simulate(space_list1, flight_plan1)))
  141. print(list_to_dictionary_converter(flight_plan1))
  142.  
  143. # #---X
  144. # w-w#-
  145.  
  146. assert simulate(space_list1, flight_plan1) == ["#---X", "w-w#-"]
  147.  
  148. print()
  149.  
  150. space_list2 = [
  151. "WWWW",
  152. "-wwW",
  153. "X-#W",
  154. ]
  155.  
  156. flight_plan2 = ["N", "N", "E", "E", "S", "W", "W", "S", "E", "E"]
  157. print("\n".join(simulate(space_list2, flight_plan2)))
  158.  
  159. # wwwW
  160. # ---W
  161. # -X#W
  162.  
  163. assert simulate(space_list2, flight_plan2) == ["wwwW", "---W", "-X#W"]
  164.  
  165. assert list_to_dictionary_converter(["-"]) == ({(0, 0): "-"}, 0, 0)
  166. assert list_to_dictionary_converter(['W#', '-X']) == ({(0, 0): 'W', (0, 1): '#', (1, 0): '-', (1, 1): '-'}, 1, 1)
  167.  
  168. assert list_to_dictionary_converter(
  169. world_map=space_list1
  170. ) == ({(0, 0): '#', (0, 1): 'w', (0, 2): 'w', (0, 3): 'w', (0, 4): '-', (1, 0): 'w', (1, 1): '-', (1, 2): 'w',
  171. (1, 3): '#', (1, 4): '-'}, 1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement