# Thread: How to create a battleship grid?

1. Member
Join Date
Nov 2012
Posts
5
Rep Power
0

## How to create a battleship grid?

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 |

2. ## Re: How to create a battleship grid?

A nested loop.

3. Member
Join Date
Nov 2012
Posts
14
Rep Power
0

## Re: How to create a battleship grid?

The easiest way I can think of would be a two dimensional array.

Java 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]);
}
}
}
}```
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.
Last edited by Jingoism; 11-16-2012 at 09:13 AM.

4. ## Re: How to create a battleship grid?

Originally Posted by cupOJava
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 |
That grid is a (deformed) 9x9 grid ...

kind regards,

Jos