Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. """Simulation."""
  2. from typing import Tuple
  3.  
  4.  
  5. def simulate(world_map: list, flight_plan: list) -> list:
  6. """
  7. Simulate a flying space ship fighting space pirates.
  8.  
  9. :param world_map: A list of strings indicating rows that make up the space map.
  10. The space map is always rectangular and the minimum given size is 1x1.
  11. Space pirate free zone is indicated by the symbol ('-'), low presence by ('w') and high presence by
  12. ('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. coordinates_dict, shippy_y, shippy_x = list_to_dictionary_converter(world_map)
  27.  
  28. for i in range(len(flight_plan)):
  29. if flight_plan[i] == "N" and shippy_y - 1 >= 0 and coordinates_dict[(shippy_y - 1, shippy_x)] != "#":
  30. shippy_y -= 1
  31. switch_letters(coordinates_dict, shippy_y, shippy_x)
  32. if flight_plan[i] == "S" and shippy_y + 1 < len(world_map) \
  33. and coordinates_dict[(shippy_y + 1, shippy_x)] != "#":
  34. shippy_y += 1
  35. switch_letters(coordinates_dict, shippy_y, shippy_x)
  36. if flight_plan[i] == "E" and shippy_x + 1 < len(world_map[0]) \
  37. and coordinates_dict[(shippy_y, shippy_x + 1)] != "#":
  38. shippy_x += 1
  39. switch_letters(coordinates_dict, shippy_y, shippy_x)
  40. if flight_plan[i] == "W" and shippy_x - 1 >= 0 and coordinates_dict[(shippy_y, shippy_x - 1)] != "#":
  41. shippy_x -= 1
  42. switch_letters(coordinates_dict, shippy_y, shippy_x)
  43. coordinates_dict[(shippy_y, shippy_x)] = "X"
  44.  
  45. return dictionary_to_list_converter(coordinates_dict, len(world_map), len(world_map[0]))
  46.  
  47.  
  48. def list_to_dictionary_converter(world_map: list) -> Tuple[dict, int, int]:
  49. """
  50. Convert a list to dictionary using coordinates as keys.
  51.  
  52. :param world_map: list of strings.
  53. :return: dictionary of the space, shippy y position, shippy x position
  54.  
  55. Map tile under Shippy's location is marked as "-" or no presence area.
  56. Dictionaries key is a Tuple which has Y-position as the first value and X-position as
  57. the second value. If there is no Shippy (Marked as X in the list) in the list, the
  58. coordinates are marked as 0 and 0.
  59. """
  60. coordinates_dict = {}
  61. shippy_y = 0
  62. shippy_x = 0
  63.  
  64. for i in range(len(world_map)):
  65. for j in range(len(world_map[i])):
  66. if world_map[i][j] == "X":
  67. shippy_y = i
  68. shippy_x = j
  69. coordinates_dict[(i, j)] = "-"
  70. else:
  71. coordinates_dict[(i, j)] = world_map[i][j]
  72.  
  73. return coordinates_dict, shippy_y, shippy_x
  74.  
  75.  
  76. def dictionary_to_list_converter(space_map: dict, height: int, width: int) -> list:
  77. """
  78. Convert dictionary of coordinates to list of strings.
  79.  
  80. :param space_map: Dictionary of the space
  81. :param width: Width of the world.
  82. :param height: Height of the world.
  83. :return: List of strings.
  84.  
  85. PS: You should add Shippy back to the dictionary before you call this method.
  86. """
  87. shippy_map = []
  88. j = 0
  89.  
  90. for i in space_map:
  91. if j % width == 0:
  92. shippy_map.append(" ")
  93. shippy_map.append(space_map[i])
  94. else:
  95. shippy_map.append(space_map[i])
  96. j += 1
  97. return "".join(shippy_map).split()
  98.  
  99.  
  100. def switch_letters(coordinates_dict, shippy_y, shippy_x):
  101. """Switch letters in map."""
  102. if coordinates_dict[(shippy_y, shippy_x)] == "W":
  103. coordinates_dict[(shippy_y, shippy_x)] = "w"
  104. elif coordinates_dict[(shippy_y, shippy_x)] == "w":
  105. coordinates_dict[(shippy_y, shippy_x)] = "-"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement