Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: How to create a battleship grid?

  1. #1
    cupOJava is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Question 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. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to create a battleship grid?

    A nested loop.

  3. #3
    Jingoism is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default 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 10:13 AM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to create a battleship grid?

    Quote Originally Posted by cupOJava View Post
    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
    Jingoism likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Open Grid Scheduler/Grid Engine
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-27-2012, 08:00 PM
  2. Battleship UML help
    By Lucid15 in forum New To Java
    Replies: 0
    Last Post: 03-12-2012, 05:15 AM
  3. battleship in java
    By matthewandalex5 in forum Advanced Java
    Replies: 2
    Last Post: 03-01-2012, 04:13 PM
  4. How Can I Create a Simple Grid with a GridLayout?
    By geeeeky.girl in forum AWT / Swing
    Replies: 7
    Last Post: 01-07-2009, 04:31 AM
  5. [SOLVED] How to create a Grid?
    By Nakira in forum New To Java
    Replies: 11
    Last Post: 11-12-2008, 11:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •