I have a project in which we are creating a Battle Boats gam
I have a project in which we are creating a Battle Boats game in Java (similar to Battleship) and I\'m unsure of how to approach. Code would be nice but of course I\'d like to understand the solution as well so an explanation would be great.
We are required to use a BattleboatsBoard class to represent the board with a 2D array, however the type of this 2D array is our choice and I\'m not sure what the best choice is. My first instinct was booleans but I\'m not sure that has enough information because of the other requirements of the project.
A boat is a line of 3 consecutive squares on the board. They may not overlap, extend outside, or be placed diagonally. The program should determine the direction and placement of boats randomly. I have no idea how to approach this in any efficient manner. Quantity of the boats is determined by the width and height of the board like so:
Using a scanner the player enters an x,y location to attack. If no boat, print miss. If they have already attacked that location or the location is out of bounds, penalty. If there is a penalty the user\'s next turn will be skipped. If there is a boat, print hit, if this attack sinks the boat, print sunk. I\'m unsure of how to keep track of all of this information in the program.
However, and this part confuses me as well, the player has an option for a \"drone\" mode to scan a small area of the board. If they use this, print \"recon\", 4 of their turns are skipped, and then the contents of an input location and the immediately surrounding squares are revealed to them. Game is over when all boats are sunk. Total attacks and total turns are printed, with lower scores being better. The board with what the user should know about it is printed after every turn.
Finally, there is a requirement to include a debug mode in which all of the game board\'s information is printed on the screen in any clearly understandable format before every turn. However, you must differentiate what would be visible to the player and what wouldn\'t be in normal play. Before play begins you are to get input to determine whether to run the game in \"normal\" mode or debug mode.
I don\'t have a firm grasp on 2D arrays so this project is hard to wrap my mind around so a full solution isn\'t necessary, but some advice on how to approach the parts of this project would be appreciated.
| Dimensions | Boats |
| width == 3 or height == 3 | 1 |
| 3 < width <= 5 or 3 < height <= 5 | 2 |
| 5 < width <= 7 or 5 < height <= 7 | 3 |
| 7 < width <= 9 or 7 < height <= 9 | 4 |
| 9 < width <= 12 or 9 < height <= 12 | 6 |
Solution
There can be 1 Dimensional array keeping track of players turns.
i.e. index 0 of array corresponds to Player 1, and the value there corresponds to number of turns.
1. Boats location on a matrix should be stored in 2 Dimensional boolean array.
Here (x,y) location would determine the location of boat.
If there is no Boat at particular location, then that entry can be FALSE.
If boat is there, the entry can be TRUE.
2. Now, create another 2 Dimensional integer array of same dimensions as the above
boolean array.
This array would have information about earlier Attacked information.
Each time Boat is attacked, the corresponding entry in the integer array is incremented.
If entry is ZERO, then either Boat is not attacked OR Boat is not present.
Only after we access Boolean array, we should scan this array.
3. When a player enters the (x,y) location, first scan the boolean array created in
Step 1, to find if the boat is present at that location. If that location, does not
have boat, the value will be FALSE and we can output MISS.
If there is boat, then the value will be TRUE, now check for same location in the
integer array created in Step 2, to find if the boat is already attacked.
If the entry in the integer array is greater than 1, then attack has already happened
and we can decide on Penalty.
If the entry in integer array is zero, then previous attack has not happened, and we can print
HIT to the screen and increment the count for that location in the integer array to keep track
of the HITS.
The turns array should be updated as well.
4. If the player goes for DRONE attack, print out the message \"recon\", add 4 to the number of
turns for this player in the turns array.
access the boolean array and display the location which he entered and surrounding array values.
5. If its debug mode, then display entire boolean array for each turn of the player.
To start the program, we can ask the player for the mode he wants and then act accordingly.

