Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. import sys
  2. import re
  3.  
  4.  
  5. class Car:
  6. def __init__(self, x, y, heading):
  7. self.x = int(x)
  8. self.y = int(y)
  9. self.heading = heading
  10.  
  11. # Prints out information about the car.
  12. def getCarInfo(self):
  13. print("Car is currently at: " + str(self.x) +
  14. "X and " + str(self.y) + "Y." + " - heading: " + self.heading)
  15.  
  16. # Used to check the roomsize to see if the car hits a wall.
  17. def detectCollison(self):
  18. if (self.x >= room.room_x or self.y >= room.room_y or self.x <= 0 or self.y <= 0):
  19. sys.exit("Error! The car hit a wall. Ending position of the car is: " +
  20. str(self.x) + "X and " +
  21. str(self.y) + "Y.")
  22.  
  23. # Moves the car in the simulated room. detectCollsion is called after each move the check if the car has hit a wall.
  24. def moveCar(self):
  25. print("Car is starting at " + str(self.x) +
  26. "X and " + str(self.y) + "Y")
  27. for direction in input_directions:
  28. # Move forward
  29. if (direction == 'F' and self.heading == 'N'):
  30. self.y += 1
  31. self.detectCollison()
  32. elif (direction == 'F' and self.heading == 'S'):
  33. self.y -= 1
  34. self.detectCollison()
  35. elif (direction == 'F' and self.heading == 'W'):
  36. self.x -= 1
  37. self.detectCollison()
  38. elif (direction == 'F' and self.heading == 'E'):
  39. self.x += 1
  40. self.detectCollison()
  41.  
  42. # Move backwards
  43. elif (direction == 'B' and self.heading == 'N'):
  44. self.y -= 1
  45. self.detectCollison()
  46. elif (direction == 'B' and self.heading == 'S'):
  47. self.y += 1
  48. self.detectCollison()
  49. elif (direction == 'B' and self.heading == 'W'):
  50. self.x += 1
  51. self.detectCollison()
  52. elif (direction == 'B' and self.heading == 'E'):
  53. self.x -= 1
  54. self.detectCollison()
  55.  
  56. # Turn left
  57. elif (direction == 'L' and self.heading == 'N'):
  58. self.heading = 'W'
  59. self.detectCollison()
  60. elif (direction == 'L' and self.heading == 'S'):
  61. self.heading = 'E'
  62. self.detectCollison()
  63. elif (direction == 'L' and self.heading == 'W'):
  64. self.heading = 'S'
  65. self.detectCollison()
  66. elif (direction == 'L' and self.heading == 'E'):
  67. self.heading = 'N'
  68. self.detectCollison()
  69.  
  70. # Turn right
  71. elif (direction == 'R' and self.heading == 'N'):
  72. self.heading = 'E'
  73. self.detectCollison()
  74. elif (direction == 'R' and self.heading == 'S'):
  75. self.heading = 'W'
  76. self.detectCollison()
  77. elif (direction == 'R' and self.heading == 'W'):
  78. self.heading = 'N'
  79. self.detectCollison()
  80. elif (direction == 'R' and self.heading == 'E'):
  81. self.heading = 'S'
  82. self.detectCollison()
  83.  
  84. self.getCarInfo()
  85.  
  86. print("Success!")
  87. print("Ending position of the car is: " +
  88. str(self.x) + "X and " +
  89. str(self.y) + "Y.")
  90. print("The cars final heading is " + self.heading + ".")
  91.  
  92.  
  93. class Room:
  94. def __init__(self, room_x, room_y):
  95. self.room_x = int(room_x)
  96. self.room_y = int(room_y)
  97.  
  98. def getRoomInfo(self):
  99. print("Room X is: " + self.room_x)
  100. print("Room Y is: " + self.room_y)
  101.  
  102.  
  103. # Ask the user for the size of the room.
  104. while True:
  105. try:
  106. input_room_x, input_room_y = (input(
  107. "Enter two integers to determine the size of the room. Use space as a separator: ")).split()
  108. if (input_room_x.isdigit() and input_room_y.isdigit()):
  109. print("Roomsize: " + input_room_x + " " + input_room_y)
  110. break
  111. else:
  112. print("Use only integers please!")
  113. continue
  114. except ValueError:
  115. print("Please enter two digits followed by a space!")
  116. continue
  117. else:
  118. break
  119.  
  120. # Ask the user for the start coordinates for the car.
  121. # TODO check if user inputs start coordinates that are bigger than the room itself.
  122. while True:
  123. try:
  124. input_start_x, input_start_y, input_heading = input(
  125. "Enter the starting position using two integers and enter the letter 'N', 'S', 'W' or 'E' to determine heading of the car. Use capital letters. Use space as a separator: ").split()
  126. allowed = re.findall("[NSWE]", input_heading)
  127. if (input_start_x.isdigit() and input_start_y.isdigit and allowed):
  128. break
  129. else:
  130. print(
  131. "Use two integers for the starting location of the car and 1 letter for the heading! Use only 'N', 'S', 'W' or 'E'")
  132. continue
  133.  
  134. except (ValueError):
  135. print(
  136. "Please enter all the information!")
  137. continue
  138. else:
  139. break
  140.  
  141. # Ask the user for the directions for the car.
  142. while True:
  143. print("'F' to move the car forward" +
  144. "\n'B' to move the car backward" +
  145. "\n'L' to steer the car to the left" +
  146. "\n'R' to steer the car to the right")
  147. input_directions = []
  148. input_directions = input(
  149. "Enter one or more of the following characters above in a series to move the car in the room. Example: 'FFBFFLR' ")
  150. input_directions = input_directions.upper()
  151. # Verify that only 'FBLR' are used to enter directions.
  152. allowed = re.findall("[FBLR]", input_directions)
  153. if (allowed):
  154. break
  155. else:
  156. print("Please enter 'F' 'B' 'L' or 'R'. ")
  157. continue
  158.  
  159. # Instantiate objects from the classes.
  160. room = Room(input_room_x, input_room_y)
  161. car = Car(input_start_x, input_start_y, input_heading)
  162.  
  163. # Run the simulation
  164. car.moveCar()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement