Question: Python: Battleship Import the ship class from an earlier exercise. the code: horizontal = 0 vertical = 1 class Ship: def __init__(self, coords_x, coords_y, direction,
Python: Battleship
Import the ship class from an earlier exercise.
the code:
horizontal = 0 vertical = 1 class Ship: def __init__(self, coords_x, coords_y, direction, length): self.coords_x = coords_x self.coords_y = coords_y self.direction = direction self.length = length self.number_of_hits = 0 def __str__(self): return f"Ship with coordinates ({self.coords_x}, {self.coords_y}) with " \ f"length {self.length}, has taken {self.number_of_hits} hits." def hit(self): self.number_of_hits += 1 def is_sunk(self): if self.number_of_hits >= self.length: return True else: return False if __name__ == "__main__": battleship1 = Ship(1, 5, horizontal, 4) battleship2 = Ship(4, 1, vertical, 2) print(battleship1) print(battleship2) print() while not battleship1.is_sunk(): battleship1.hit() print(battleship1) while not battleship2.is_sunk(): battleship2.hit() print(battleship2) Make a class for a game board. This class should have the following properties:
A variable that counts the number of the shooter has used
An empty list of ships
A matrix or list of lists with boolean values that say whether this route has already been shot at or not. This matrix should have 10x10 elements where all start with the value False. For a list of lists you must first create the outer list, insert 10 inner lists, and for each inner list insert 10 False values. If you want to use a 10x10 numpy array, you can use 0 for False and 1 for True.
A matrix or list of references. This matrix should have 10x10 elements where all start with the value None. A list of lists is made as in the previous subtask. To create an array, you must specify the parameter "dtype = object" to np.zeros to create a numpy array that accepts objects and not just numbers.
A method to check if a ship's location is legal. A location is legal if the entire ship is within the game board and none of the routes the ship is going to cover already contain a ship. Use the matrix from the above to check if the routes already contain a ship. The method should take the coordinates, length and direction of the imagined ship as parameters. The method should return True or False.
A method that places a ship. It must first use the method from the previous subtask to check that the placement is legal. If the location is legal, it should create a new Ship object and put it in the list of ships. In the reference matrix, it should set all the routes that the ship covers to refer to the ship (instead of being None). The method should return True if it placed the ship and False if the placement is not allowed
A method that shoots on a route. The method should take the coordinates of the route as parameters. The method must first check whether the user has already shot at the route. If not, it should check if the route contains a ship. If it contains a ship, it shall call the hit () method of the ship. The default shell cell of the route the user has shot against in the matrix over hits is set equal to True
An method that prints the board
An method that checks if the user has won by going through the list of ships and checking that everyone has been sunk
a function or if __name__ == __main__ that plays the game. It shall place one ship of length 5, one ship of length 4, two ships of length 3 and one ship of length 2 in random places and in a random direction. it finds a legal place. Then it should let the user shoot by entering coordinates until the user has shot down all the ships. Then it should print the number of shots the user used.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
