Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## 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("----------------------------------------");
}

}
{
Scanner sc = new Scanner(System.in);

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
{

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");

}
}```

• are all the ships aligned vertically, horizontally or both?

•

#### Posting Permissions

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