I'm trying to create a console version of battleship but I am having trouble creating the grid. Any suggestions on where to start?
The grid is 8 x 8 and should look like this:
012345678
------------
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
Printable View
I'm trying to create a console version of battleship but I am having trouble creating the grid. Any suggestions on where to start?
The grid is 8 x 8 and should look like this:
012345678
------------
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
A nested loop.
The easiest way I can think of would be a two dimensional array.
That will just print the array out as a block of 8x8 0's. This could just as easily be an 8x8 array of objects be it battleships, cruisers etc.Code:public class Shipgrid
{
public static void main(String[] args){
int[][] shipGrid = new int[8][8];
for (int i = 0; i < shipGrid.length; i++){
for ( int j = 0; j < shipGrid[i].length; j++){
shipGrid[i][j] = 0;
}
}
for (int i = 0; i < shipGrid.length; i++){
System.out.println();
for ( int j = 0; j < shipGrid[i].length; j++){
System.out.print(shipGrid[i][j]);
}
}
}
}