Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array length problem

    At the moment I am primarily concerned with making the lengths on the ships connect to each other. Can anyone give me some sort of guidance on how to place ships lengths of 2 3 3 4 5 on the board, and without bumping with each other.

    this is what I have so far
    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Battleship 
    {
    	int grid[][];
    	int row;
    	int col;
    	
    	public Battleship()
    	{//random locations for ships
    		grid = new int[10][10];
    		
    		Random rnd = new Random();
    		//patrol
    		
    		int[] shipsLength = {2, 3, 3, 4, 5};
    		int[] ship = {1, 2, 3, 4, 5};
    		
    		//test for 2x1
    		for(int x = 0; x < 5; x++)
    		{
    			
    			
    			row = rnd.nextInt(10);
    			col = rnd.nextInt(10);
    			
    			grid[row][col] = ship[x];
    		}
    	}
    	
    	public void display()
    	{
    		//horizontal lines
    		
    		System.out.println("----------------------------------------");
    		
    		
    		for (int line[] : grid)
    		{
    			for (int num : line)
    			{				// determine what to print for this position
    				if (num == -1)
    					System.out.printf("| &#37;2s", "m " );
    				else if (num == 0 || num == 10)
    					System.out.printf("|   ");        // don't print 0 or 10
    				else
      				    System.out.printf("| %2d", num);  // this displays the 10
    			}
    			
    		
    		// print the final bar of this row
    		System.out.println("|");
    
    		// print horizontal line
    		System.out.println("----------------------------------------");
    		}
    		
    		
    	}
    	public int menu()
    	{
    		Scanner sc = new Scanner(System.in);
    		
    		// display menu
    		System.out.println("1. Enter a guess (row column): ");
    		System.out.println("2. Display grid");
    		System.out.println("3. End game");
    		
    		// get user's choice
    		System.out.print("Enter choice: ");
    		int choice = sc.nextInt();
    		
    		return choice;		
    	}
    	/////public boolean processGuess(int r, int c) GUESS PROCESSOR IS HERE
    	{
    		//CHECK POSITIONS
    	}
    	
    	public int getRow()
    	{
    		return row;
    	}
    	
    	public int getCol()
    	{
    		return col;
    	}
    	
    	
    	
    	public static void main(String args[])
    	{
    		Scanner keyboard = new Scanner(System.in);
    		Battleship gs = new Battleship();
    		
    		int counter = 0;
    		int choice;
    		
    		do
    		{
    			choice = gs.menu();
    			
    			if (choice == 1) //guess
    			{
    				System.out.println("Enter your guess (row and column i.e. 1 9): ");
    				int row = keyboard.nextInt();
    				int col = keyboard.nextInt();
    				
    				
    				
    			}
    		else if (choice == 2) // display
    		{
    			gs.display();
    		}
    		}	
    		while(choice == 3);
    		
    
    		System.out.println("End of Battleship game");
    		
    	}
    }
    Last edited by oracleguy; 10-04-2008 at 05:19 PM. Reason: Please use code tags

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    are all the ships aligned vertically, horizontally or both?
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

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