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

    Question BattleShip game ship on top of ship

    Hi!

    i am working on a text version battleship game, but my problem is the ships that are generated by the computer are placed one on top of another.
    Does anybody have any solutions on how this can be fixed? :confused:

    My code:

    Java Code:
    import java.util.Random;
     
     public class BattleShip
     {
     	public String[][] grid;
    	private int[] takenX = new int[100];
    	private int[] takenY = new int[100];
     	private int length;
     	private int width;
     	
     	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 void placeShip(int l)
    	{
    		Random rand = new Random();
    		int x = rand.nextInt(10 - (l - 1)); 
    		int y = rand.nextInt(10 - (l - 1));
    		int dir = rand.nextInt(2) + 1;
    		if (dir == 1)
    		{
    			for (int i = 0; i < l; i++)
    			{
    				grid[x + i][y] = "#";
    			}
    		}
    		else
    		{
    			for (int j = 0; j < l; j++)
    			{
    				grid[x][y - 1] = "#";
    			}
    		}
    	}
     	
     	public static void main(String[]args)
     	{
     		BattleShip battleship = new BattleShip(10,10);
     		battleship.placeShip(5);
     		battleship.printGrid();
     	}
     }
    Any suggestions would be very helpful !!!!
    -- 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,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Some suggestions:

    1) Use either an enum for horizontal / vertical or a boolean such as "placeHorizontal" since there are only two possibilities, and a boolean or enum would be much more readable than dir == 1 or dir ==2.

    2) Select horizontal vs. vertical placement first as it will effect the possible range of positions available for the ship on both axi. For instance, if the ship is of length 5 and you randomly place it horizontally, then there are 6 possible starting x locations, and 10 possible starting y locations. Conversely if the ship is to be placed vertically, then swap the previous statements.

    3) Easiest I think is a placeShip method that repeatedly selects a random location until one is found that doesn't overlap. A do-while loop could work nicely here.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    And whichever way you choose (random placement is probably the easiest as said) make sure you deploy the larger ships first or you could find the code trying to deploy a battleship when there's no valid space left on the board.

Similar Threads

  1. battle Ship game help!!
    By linux1man in forum New To Java
    Replies: 4
    Last Post: 06-03-2009, 01:47 AM
  2. Battleship game
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 09:42 PM
  3. Java Battleship Game Help PLEASE
    By mars_red in forum New To Java
    Replies: 0
    Last Post: 02-12-2008, 01:09 AM
  4. Java BattleShip game help
    By mars_red in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 12:58 AM
  5. error with traverse a relations ship
    By darkbalder in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-11-2007, 05: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
  •