Results 1 to 5 of 5
  1. #1
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Question battle Ship game help!!

    Hi!

    i am working on this text version battleship game, and i can not get the
    computer to place the ships randomly.

    i get arrayoutof bounds exeption error, but i dont understand why. i posted my code below, so can somebody point out were the problem is?

    Java Code:
    /*
     *
     *	A Battleship game
     */
     
     import java.util.Random;
     
     public class BattleShip
     {
     	public String[][] grid;
     	private int length;
     	private int width;
    	private boolean placed = false;
     	
     	public BattleShip(int l, int w)
     	{
     		grid = new String[l][w];
     		length = l;
     		width = w;
     		initGrid();
      	}
     	
     	public void initGrid()
     	{
     		for (int r = 0; r < grid.length; r++)
     			for (int c = 0; c < grid[r].length; c++)
     			{
     				grid[r][c] = "O";   
     			}
     	}
     	
     	public void printGrid()
     	{
     		String g = "";
     		g += "    ";
     		for (int i = 1; i <= (grid.length); i++)
     		{
     			g += (i + " ");
     		}
     		
     		g += "\n\n";
     		
     		for (int r = 0; r < grid.length; r++)
     		{
    			if (r >= 9)
    				g += ((r + 1) + "  ");
    			else
    				g += ((r + 1) + "   ");
     			for (int c = 0; c < grid[r].length; c++)
     			{
     				g += ((grid[r][c]) + " ");
     			}
     			g += "\n";
     		}
     		System.out.println(g);
     	}
    	 
    	public boolean isValidLocation(int l, int h)
    	{
    		if ((grid[l][h]).equals("O"))
    			return false;
    		else
    			return true;
    	}
    	
    	public boolean isValidShipLocation(int l, int h, int leng, int dir)
    	{
    		int countValid = 0;
    		if (dir == 1)
    		{
    			for (int i = 0; i <= leng; i++)
    			{
    				if ((grid[l][h + i]).equals("#"));
    					countValid++;
    			}
    		}
    		else if (dir == 2)
    		{
    			for (int j = 0; j <= leng; j++)
    			{
    				if ((grid[l + j][h]).equals("#"));
    					countValid++;
    			}
    		}
    		else if (dir == 3)
    		{
    			for (int k = 0; k <= leng; k++)
    			{
    				if ((grid[l - k][h]).equals("#"));
    					countValid++;
    			}
    		}
    		else 
    		{
    			for (int z = 0; z <= leng; z++)
    			{
    				if ((grid[l][h - z]).equals("#"));
    					countValid++;
    			}
    		}
    		if (countValid == 0)
    			return true;
    		else
    			return false;
    	}
    	
     	public void genShip(int len)
    	{
    		if (placed == false)
    		{
    			Random rand = new Random();
    			int x = rand.nextInt(10);
    			int y = rand.nextInt(10);
    			int dir = rand.nextInt(4) + 1;
    			
    			if (isValidShipLocation(x,y,len,dir) == true)
    			{
    				for (int i = 0; i <= len; i++)
    				{
    					if (dir == 1)
    						grid[x][y + i] = "#";
    					else if (dir == 2)
    						grid[x + i][y] = "#";
    					else if (dir == 3)
    						grid[x - i][y] = "#";
    					else
    						grid[x][y - i] = "#";
    				}
    				placed = true;
    			}
    			else
    			{
    				genShip(len);
    			}
    		}
        }
     	
     	public static void main(String[]args)
     	{
     		BattleShip battleship = new BattleShip(10,10);
     		battleship.genShip(5);
     		battleship.genShip(4);
     		battleship.genShip(3);
     		battleship.genShip(3);
     		battleship.genShip(2);
     		battleship.printGrid();
     	}	
    
     }

    :eek: Any help would be appreciated !
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Put some System.out.println(...) statements in there, and you'll see that you're going off the grid. Or you can use System.out.printf, which I find easier to use in this situation:
    i.e.,
    Java Code:
            System.out.printf("l = %d; h = %d, j = %d%n", l, h, j);
            if ((grid[l + j][h]).equals("#"))
              ;
            countValid++;

  3. #3
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Default

    so is there a quick fix for this problem?
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    hm, fix the logic, which is a bit of busy work that I wish I had time for but don't. I've 30 charts to dictate. But I've seen your posts, and I know that you've got the smarts. I'll bet that with a bit of concentration and lots of scratch paper you'll solve it quicker than any of us could.

  5. #5
    linux1man's Avatar
    linux1man is offline Member
    Join Date
    Nov 2008
    Posts
    39
    Rep Power
    0

    Default

    Ok i will try
    (is it something in the for loop tho?)
    -- Ubuntu 8.04 (Linux) O.P.S. 512mb RAM
    -- 2.66 Ghz Pentium Geforce NVidia 440 64mb

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. Game 21
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 01:33 AM
  3. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM
  4. Virtual Battle Programmer
    By ALATECjobs in forum Jobs Offered
    Replies: 2
    Last Post: 08-28-2008, 05:52 PM
  5. error with traverse a relations ship
    By darkbalder in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-11-2007, 06:25 PM

Posting Permissions

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