Practice/Amazon/Design a Chess Game
Object Oriented DesignMust
Design an object-oriented system for a Battleship game. Your system should support the classic two-player naval warfare game where players place ships on a grid and take turns attacking coordinates on their opponent's board. The system must handle ship placement, validate moves, track hits and misses, detect when ships are sunk, and determine when the game is won.
Ship class that represents individual ships with a name, size, and orientationBoard class that represents a player's game grid (typically 10x10)BattleshipGame class that manages the overall game state for multiple playersExample 1: Basic Gameplay
` game = BattleshipGame() board1 = game.setup_player_board(1) board2 = game.setup_player_board(2)
destroyer = Ship("Destroyer", 2) board1.place_ship(destroyer, 0, 0, True) # horizontal at (0,0)
result = game.make_attack(2, 1, 0, 0) # Returns "HIT" result = game.make_attack(2, 1, 0, 1) # Returns "SUNK" result = game.make_attack(2, 1, 0, 2) # Returns "MISS" `
Example 2: Invalid Placement
` board = Board(10) battleship = Ship("Battleship", 4)
valid = board.place_ship(battleship, 0, 8, True) # Returns False
valid = board.place_ship(battleship, 0, 6, True) # Returns True
`
Example 3: Game Over Detection
` game = BattleshipGame() board = game.setup_player_board(1)
patrol = Ship("Patrol", 2) board.place_ship(patrol, 5, 5, False) # vertical
game.make_attack(2, 1, 5, 5) # "HIT" game.make_attack(2, 1, 6, 5) # "SUNK"
game.is_game_over() # Returns True, all ships sunk `