Recursion - Ship Battle
Posted
by
rgorrosini
on Stack Overflow
See other posts from Stack Overflow
or by rgorrosini
Published on 2012-06-04T16:22:35Z
Indexed on
2012/06/04
16:42 UTC
Read the original article
Hit count: 184
I'm trying to write a little ship battle game in java.
It is 100% academic, I made it to practice recursion, so... I want to use it instead of iteration, even if it's simpler and more efficient in most some cases.
Let's get down to business. These are the rules:
- Ships are 1, 2 or 3 cells wide and are placed horizontally only.
- Water is represented with 0, non-hit ship cells are 1, hit ship cells are 2 and sunken ships have all it's cells in 3.
With those rules set, I'm using the following array for testing:
int[][] board = new int[][]
{
{0, 1, 2, 0, 1, 0},
{0, 0, 1, 1, 1, 0},
{0, 3, 0, 0, 0, 0},
{0, 0, 2, 1, 2, 0},
{0, 0, 0, 1, 1, 1},
};
It works pretty good so far, and to make it more user-friendly I would like to add a couple of reports. these are the methods I need for them:
- Given the matrix, return the amount of ships in it.
- Same as a), but separating them by state (amount of non-hit ships, hit and sunken ones).
I will need a hand with those reports, and I would like to get some ideas.
Remember it must be done using recursion, I want to understand this, and the only way to go is practice!
Thanks a lot for your time and patience :).
© Stack Overflow or respective owner