Battleship
Asked at Citadel
Reported by the problem's creator, not verified
You are building the game logic for two-player Battleship as the class Battleship, created with the board size n. Each player, "A" or "B", has their own n by n board. Rows and columns are numbered from 0, with (0, 0) at the top left.
Examples
Example 1
Input: init =
[5], operations =[["place_ship",["A",0,0,3,"H"]],["place_ship",["A",1,2,3,"V"]],["place_ship",["A",2,0,3,"H"]],["place_ship",["A",3,3,3,"H"]],["place_ship",["B",0,0,3,"H"]],["ship_cells",["A"]],["ship_cells",["B"]]]Output:
[true,true,false,false,true,[[0,0],[0,1],[0,2],[1,2],[2,2],[3,2]],[[0,0],[0,1],[0,2]]]Explanation: A's first ship covers (0,0), (0,1), (0,2). The vertical ship from (1,2) covers (1,2), (2,2), (3,2): it touches the first ship but shares no cell, so it is placed. The horizontal ship from (2,0) would cover (2,2), which is taken, and the one from (3,3) would need columns 3, 4 and 5 on a 5-wide board, so both fail. B can use the cells A has, because each player has a board of their own.