Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.47 KB | None | 0 0
  1. class GridPos:
  2. def __init__(self, x, y):
  3. """
  4. This class has the each position on the grid, it also tells whether
  5. a position has been guessed or whether a position has a boat on it
  6. or not.
  7.  
  8. Parameters:
  9. x is the x coordinate of the boat
  10. y is the y coordinate of the boat
  11.  
  12. Returns: The type of boat
  13.  
  14. Pre-condition: The given x, y variables are on the grid
  15.  
  16. Post-condition: The boats have been set in their proper positions
  17. and .'s have been placed in all other spots on the grid
  18. """
  19. self._cord = (x, y)
  20. self._boat = '.'
  21. self._guess = False
  22.  
  23. def set_boats(self, boat):
  24. """
  25. Sets the boat parameter equal to self._boat:
  26.  
  27. Parameters:
  28. boat is the type of boat either: A, B, D, S, or P
  29.  
  30. Returns: None
  31.  
  32. Pre-condition: boat is either A, B, D, S, or P
  33.  
  34. Post-condition: self._boat is equal to the boat parameter
  35. """
  36. self._boat = boat
  37.  
  38. def get_boat(self):
  39. '''boat getter'''
  40. return self._boat
  41.  
  42. def __str__(self):
  43. '''returns the boat'''
  44. return self._boat
  45.  
  46. def guess_upgrader(self):
  47. """
  48. Determines whether a position has been guessed or not
  49.  
  50. Parameters:
  51. None
  52.  
  53. Returns: True or False
  54.  
  55. Pre-condition: The guess has a value of either True or False
  56.  
  57. Post-condition: The guess gets changed to True, marking it has being
  58. guessed already
  59. """
  60. self._guess += True
  61.  
  62. def get_guess(self):
  63. '''Guess getter'''
  64. return self._guess
  65.  
  66. def get_position(self):
  67. '''Position getter'''
  68. return self._cord
  69.  
  70. class Board:
  71. def __init__(self):
  72. """
  73. This class creates the board, places the ships and manages the health
  74. of each ship on the board.
  75.  
  76. Parameters:
  77. self._grid is the entire grid
  78. self._health is the health of each ship at a given point
  79. self._sinks is how many ships have been sunk, once at 5
  80. the game ends
  81.  
  82. Returns: A string representation of the grid
  83.  
  84. Post-condition: The board has been created with all the ships placed
  85. in their appropriate position
  86. """
  87. self._grid = {}
  88. self._health = {'A':5,'B':4,'S':3,'D':3,'P':2}
  89. self._sinks = 0
  90.  
  91. def get_health(self):
  92. '''boat health getter'''
  93. return self._health
  94.  
  95. def __str__(self):
  96. '''returns the grid'''
  97. return str(self._grid)
  98.  
  99. def create_board(self):
  100. """
  101. This function creates a 10x10 board for the game
  102.  
  103. Parameters:
  104. None
  105.  
  106. Returns: None
  107.  
  108. Post-condition: A 10x10 grid has been created with a tuple as
  109. the values of the coordinates.
  110. """
  111. for i in range(9, -1, -1):
  112. for j in range(10):
  113. spot = (i, j)
  114. self._grid[spot] = GridPos(i, j)
  115.  
  116. def place_ships(self, line, error_line):
  117. """
  118. This function places the ships in their spots or calls an error while
  119. placing the boats
  120.  
  121. Parameters:
  122. Line consists of the ship and its coordinates.
  123. Error_line consists of the same values but in the proper form for
  124. exiting.
  125.  
  126. Returns: None
  127.  
  128. Pre-condition: All the ships are of valid length and there are only
  129. 5 ships, each being a different type.
  130.  
  131. Post-condition: The ships have been placed in their proper spots or
  132. an overlapping error occurs, ending the program
  133. """
  134. x1_y1 = line[1],line[2]
  135. x2_y2 = line[3],line[4]
  136. if x1_y1[0] == x2_y2[0]:
  137. for i in range(min(x1_y1[1], x2_y2[1]), max(x1_y1[1], x2_y2[1])+1):
  138. if self._grid[x1_y1[0], i].get_boat() == '.':
  139. self._grid[x1_y1[0], i].set_boats(line[0])
  140. else:
  141. print("ERROR: overlapping ship: " + error_line)
  142. exit(1)
  143. elif x1_y1[1] == x2_y2[1]:
  144. for i in range(min(x1_y1[0], x2_y2[0]), max(x1_y1[0], x2_y2[0])+1):
  145. if self._grid[i, x1_y1[1]].get_boat() == '.':
  146. self._grid[i, x1_y1[1]].set_boats(line[0])
  147. else:
  148. print("ERROR: overlapping ship: " + error_line)
  149. exit(1)
  150.  
  151. def process_guesses(self):
  152. """
  153. This function opens the guesses and displays whether they hit, missed,
  154. hit the same area again, or missed the area again
  155.  
  156. Parameters:
  157. None
  158.  
  159. Returns: None
  160.  
  161. Pre-condition: The board has been created and all the ships have been placed
  162. in their proper locations
  163.  
  164. Post-condition: All the ships have been sunk and the game is over
  165. """
  166. filee = input()
  167. try:
  168. file = open(filee)
  169. except:
  170. print("ERROR: Could not open file: " + filee)
  171. exit(1)
  172. for i in file:
  173. file_split = i.split()
  174. if len(file_split) == 0:
  175. pass
  176. else:
  177. guess = int(file_split[0]),int(file_split[1])
  178. if len(file_split) == 0:
  179. continue
  180. elif guess[0] > 9 or guess[0] < 0 or guess[1] > 9 or guess[1] < 0:
  181. print('illegal guess')
  182. else:
  183. if self._grid[guess].get_guess() == True:
  184. if str(self._grid[guess]) == '.':
  185. print('miss (again)')
  186. elif str(self._grid[guess]).isalpha():
  187. print('hit (again)')
  188. elif self._grid[guess].get_guess() == False:
  189. if str(self._grid[guess]) == '.':
  190. print('miss')
  191. self._grid[guess].guess_upgrader()
  192. elif str(self._grid[guess]) in self._health.keys():
  193. self._health[str(self._grid[guess])] -= 1
  194. if self._health[str(self._grid[guess])] == 0:
  195. print("{} sunk".format(str(self._grid[guess])))
  196. self._sinks += 1
  197. else:
  198. print('hit')
  199. if self._sinks == 5:
  200. print('all ships sunk: game over')
  201. self._grid[guess].guess_upgrader()
  202.  
  203. class Ship:
  204. def __init__(self):
  205. """
  206. Ship checks the sizes of the ships
  207.  
  208. Parameters:
  209. No parameters
  210.  
  211. Returns: None
  212.  
  213. Post-condition: The checked ship is the correct length
  214. """
  215. self._ship_sizes = {'A':5,'B':4,'S':3,'D':3,'P':2}
  216.  
  217. def check_ship_sizes(self, ship):
  218. '''Boat size getter'''
  219. return self._ship_sizes[ship]
  220.  
  221. def file(boardcall):
  222. """
  223. This function opens the file, checks for errors and places the ships
  224.  
  225. Parameters:
  226. boardcall is the initialer for the Board class
  227.  
  228. Returns: None
  229.  
  230. Post-condition: The ships are a legal size and direction
  231. """
  232. places = {}
  233. all_ships = []
  234. filee = input()
  235. try:
  236. file = open(filee)
  237. except:
  238. print("ERROR: Could not open file: " + filee)
  239. exit(1)
  240. for j in file:
  241. i_split = j.split()
  242. if i_split[0] in all_ships:
  243. print("ERROR: fleet composition incorrect")
  244. exit(1)
  245. else:
  246. all_ships.append(i_split[0])
  247. file.close()
  248. file = open(filee)
  249. if len(all_ships) != 5:
  250. print("ERROR: fleet composition incorrect")
  251. exit(1)
  252. for i in file:
  253. error_line = ''.join(i)
  254. file_split = i.split()
  255. check(file_split, error_line)
  256. boat = file_split[0]
  257. file_split[1] = int(file_split[1])
  258. file_split[2] = int(file_split[2])
  259. file_split[3] = int(file_split[3])
  260. file_split[4] = int(file_split[4])
  261. shipcall = Ship()
  262. if file_split[1] == file_split[3]:
  263. ssize = max(file_split[2], file_split[4]) - min(file_split[2], file_split[4]) + 1
  264. if ssize != shipcall.check_ship_sizes(boat):
  265. print('ERROR: incorrect ship size: ' + error_line)
  266. exit(1)
  267. else:
  268. ssize = max(file_split[1], file_split[3]) - min(file_split[1], file_split[3]) + 1
  269. if ssize != shipcall.check_ship_sizes(boat):
  270. print('ERROR: incorrect ship size: ' + error_line)
  271. exit(1)
  272. boardcall.place_ships(file_split, error_line)
  273.  
  274.  
  275.  
  276. def check(line, error_line):
  277. '''This function checks that the pieces are not placed diagonally
  278. Precondition: the line has 5 values
  279.  
  280. Parameters:
  281. Line consists of the ship and its coordinates.
  282. Error_line consists of the same values but in the proper form for
  283. exiting.
  284.  
  285. Returns: None
  286.  
  287. Post-condition: No errors occured or the program prints and error
  288. and the line that
  289. '''
  290. if (line[1] == line[3]) or (line[2] == line[4]) == True:
  291. pass
  292. else:
  293. print("ERROR: ship not horizontal or vertical: " + error_line)
  294. exit(1)
  295. i = 1
  296. while i < len(line):
  297. if int(line[i]) > 10:
  298. print("ERROR: ship out-of-bounds: " + error_line)
  299. exit(1)
  300. i += 1
  301.  
  302. def main():
  303. boardcall = Board()
  304. boardcall.create_board()
  305. file(boardcall)
  306. boardcall.process_guesses()
  307.  
  308. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement