hey guys i got this new project for my java class and got the overall idea but my code still does not seem to work ...here are the directions:
- make a 5X5 boolean array assign 5 random elements for 5 ships
- user can have 15 guesses to find all 5 spots
- at end display remaining ships (if any) by location (ex. 2 1)

heres my code can u guys fix please i made the effort....

thanks

public static void battle()
 {
   EasyReader enter = new EasyReader();
   boolean [][] grid = new boolean [5][5]; 
   int guessRow, guessColumn, row, column, ships = 0, turns = 0;
   do{
       ships ++; 
       row = (int)Math.random() * grid.length;
       column = (int)Math.random() * grid[0].length; 
       grid[row][column] = true; 
     }while (ships < 5);
   
   do{
       turns ++;
       
       System.out.print("Enter the row ");
       guessRow = enter.readInt();
       System.out.print("Enter the column ");
       guessColumn = enter.readInt(); 
       if (grid[guessRow][guessColumn] == false)
        System.out.println("miss"); 
       else
       {
        ships --;       
        System.out.println("hit" + ships + " more left !");
       }
     }while(!(turns == 15 || ships == 0));
   if (turns == 15) 
     {
  for(int i = 0; true;i++) {
    for(int j = 0; true;j++){
      if (grid[row][column] == true)
        System.out.print("You lost the remaning ships are at " + row + column);
  } }
   } 
   else if (ships == 0);
   System.out.print("You won!");
 }

The problem is in the block of code which initialize the grid array.
It every time chose the same values for the column and row which are true and it is 0,0
You should first try another formula for chosing the true cells and second you should make sure that any value will not be repeated.
I hope this be a good hint.
Good luck.

Also
1. You should be worry about the array index
For example the first column in the first row in the array is indexed by (0,0) but the user will assume it (1,1).

2. Are you sure about the for loops you defined they seem to be infinite loops.

hey guys i got this new project for my java class and got the overall idea but my code still does not seem to work ...here are the directions:
- make a 5X5 boolean array assign 5 random elements for 5 ships
- user can have 15 guesses to find all 5 spots
- at end display remaining ships (if any) by location (ex. 2 1)

heres my code can u guys fix please i made the effort....

thanks

public static void battle()
 {
   EasyReader enter = new EasyReader();
   boolean [][] grid = new boolean [5][5]; 
   int guessRow, guessColumn, row, column, ships = 0, turns = 0;
   do{
       ships ++; 
       row = (int)Math.random() * grid.length;
       column = (int)Math.random() * grid[0].length; 
       grid[row][column] = true; 
     }while (ships < 5);
   
   do{
       turns ++;
       
       System.out.print("Enter the row ");
       guessRow = enter.readInt();
       System.out.print("Enter the column ");
       guessColumn = enter.readInt(); 
       if (grid[guessRow][guessColumn] == false)
        System.out.println("miss"); 
       else
       {
        ships --;       
        System.out.println("hit" + ships + " more left !");
       }
     }while(!(turns == 15 || ships == 0));
   if (turns == 15) 
     {
  for(int i = 0; true;i++) {
    for(int j = 0; true;j++){
      if (grid[row][column] == true)
        System.out.print("You lost the remaning ships are at " + row + column);
  } }
   } 
   else if (ships == 0);
   System.out.print("You won!");
 }

you have to close parenthesis The Math.random * grid.length, otherwise, it will cast the random number first into into int.;

this will produce the output that you want.

do{
       ships ++; 
       row = (int)Math.random() * grid.length;
  column = (int) (Math.random() * grid[0].length);// <----------------- see the difference?
       grid[row][column] = true; 
     }while (ships < 5);

hey guys i got this new project for my java class and got the overall idea but my code still does not seem to work ...here are the directions:
- make a 5X5 boolean array assign 5 random elements for 5 ships
- user can have 15 guesses to find all 5 spots
- at end display remaining ships (if any) by location (ex. 2 1)

heres my code can u guys fix please i made the effort....

thanks

public static void battle()
 {
   EasyReader enter = new EasyReader();
   boolean [][] grid = new boolean [5][5]; 
   int guessRow, guessColumn, row, column, ships = 0, turns = 0;
   do{
       ships ++; 
       row = (int)Math.random() * grid.length;
       column = (int)Math.random() * grid[0].length; 
       grid[row][column] = true; 
     }while (ships < 5);
   
   do{
       turns ++;
       
       System.out.print("Enter the row ");
       guessRow = enter.readInt();
       System.out.print("Enter the column ");
       guessColumn = enter.readInt(); 
       if (grid[guessRow][guessColumn] == false)
        System.out.println("miss"); 
       else
       {
        ships --;       
        System.out.println("hit" + ships + " more left !");
       }
     }while(!(turns == 15 || ships == 0));
   if (turns == 15) 
     {
  for(int i = 0; true;i++) {
    for(int j = 0; true;j++){
      if (grid[row][column] == true)
        System.out.print("You lost the remaning ships are at " + row + column);
  } }
   } 
   else if (ships == 0);
   System.out.print("You won!");
 }

I made an output print for the random columns and rows so that its easier to see where the true values are. It seems like the conditions for your user input doesnt work right, even if I typed the correct row and column for the true values, still it says " miss ".

import java.util.*;

public static void start()
 {
  
   Scanner sc= new Scanner(System.in); // 
   boolean [][] grid = new boolean [5][5];
   int guessRow, guessColumn, row, column, ships = 0, turns = 0;
   do{
       ships ++;
       row = (int)(Math.random() * grid.length);
       column = (int)Math.random() * grid[0].length;
       grid[row][column] = true;
     }while (ships < 5);

     for(int i = 0; i < grid.length; i++)
     {
		 for(int j=0; j < grid[i].length;j++)
		 {
			 System.out.print("+ "+grid[i][j]+" +");
		 }
		 System.out.println();
	 }

   do{
       turns ++;

       System.out.print("Enter the row ");
       guessRow = sc.nextInt();
       System.out.print("Enter the column ");
       guessColumn = sc.nextInt();
       if (grid[guessRow][guessColumn] == false)
        System.out.println("miss");
       else
       {
        ships --;
        System.out.println("hit" + ships + " more left !");
       }
     }while(!(turns == 15 || ships == 0));
   if (turns == 15)
     {
  for(int i = 0; true;i++) {
    for(int j = 0; true;j++){
      if (grid[row][column] == true)
        System.out.print("You lost the remaning ships are at " + row + column);
  } }
   }
   else if (ships == 0);
   System.out.print("You won!");
 }

I made an output print for the random columns and rows so that its easier to see where the true values are. It seems like the conditions for your user input doesnt work right, even if I typed the correct row and column for the true values, still it says " miss ".

import java.util.*;

public static void start()
 {
  
   Scanner sc= new Scanner(System.in); // 
   boolean [][] grid = new boolean [5][5];
   int guessRow, guessColumn, row, column, ships = 0, turns = 0;
   do{
       ships ++;
       row = (int)(Math.random() * grid.length);
       column = (int)Math.random() * grid[0].length;
       grid[row][column] = true;
     }while (ships < 5);

     for(int i = 0; i < grid.length; i++)
     {
		 for(int j=0; j < grid[i].length;j++)
		 {
			 System.out.print("+ "+grid[i][j]+" +");
		 }
		 System.out.println();
	 }

   do{
       turns ++;

       System.out.print("Enter the row ");
       guessRow = sc.nextInt();
       System.out.print("Enter the column ");
       guessColumn = sc.nextInt();
       if (grid[guessRow][guessColumn] == false)
        System.out.println("miss");
       else
       {
        ships --;
        System.out.println("hit" + ships + " more left !");
       }
     }while(!(turns == 15 || ships == 0));
   if (turns == 15)
     {
  for(int i = 0; true;i++) {
    for(int j = 0; true;j++){
      if (grid[row][column] == true)
        System.out.print("You lost the remaning ships are at " + row + column);
  } }
   }
   else if (ships == 0);
   System.out.print("You won!");
 }

Sorry,

My mistake. . it works fine now, I hope I fixed the prob.. fix the for loop in the last part. its infinite when its executed.

changed, but im not always getting 5 ships on the board, but no ships are psotioned on top of each other ...
fixed loop its not infinite but it always outputs

You lost the remaining ships are at points: 0123412345234563456745678You Win!

in the end ...

public static void start()
{
EasyReader find = new EasyReader(); //
boolean [][] grid = new boolean [5][5];
int guessRow, guessColumn, row, column, ships = 0, turns = 0;
do{
ships ++;
row = (int)(Math.random() * grid.length);
column = (int)Math.random() * grid[0].length;
if (grid[row][column] == false)
 grid[row][column] = true; 
}while (ships < 5);

for(int i = 0; i < grid.length; i++)
{
  for(int j=0; j < grid[i].length;j++)
  {
    System.out.print("+ "+grid[i][j]+" +");
  }
 System.out.println();
}

do{
  turns ++;
  System.out.print("Enter the row ");
  guessRow =  find.readInt();
  System.out.print("Enter the column ");
  guessColumn = find.readInt();
if (grid[guessRow][guessColumn] == false)
  System.out.println("miss");
else
{
  ships --;
  System.out.println("hit" + ships + " more left !");
}
}while(!(turns == 15 || ships == 0));
if (turns == 15)
{
 System.out.print("You lost the remaining ships are at points: "); 
 for(int i = 0; i < grid.length ;i++) {
   for(int j = 0; j < grid[0].length;j++){
     System.out.print(i + j);
 } }
}
else if (ships == 0);
System.out.print("You won!");
}
}

changed, but im not always getting 5 ships on the board, but no ships are psotioned on top of each other ...
fixed loop its not infinite but it always outputs

You lost the remaining ships are at points: 0123412345234563456745678You Win!

in the end ...

public static void start()
{
EasyReader find = new EasyReader(); //
boolean [][] grid = new boolean [5][5];
int guessRow, guessColumn, row, column, ships = 0, turns = 0;
do{
ships ++;
row = (int)(Math.random() * grid.length);
column = (int)Math.random() * grid[0].length;
if (grid[row][column] == false)
 grid[row][column] = true; 
}while (ships < 5);

for(int i = 0; i < grid.length; i++)
{
  for(int j=0; j < grid[i].length;j++)
  {
    System.out.print("+ "+grid[i][j]+" +");
  }
 System.out.println();
}

do{
  turns ++;
  System.out.print("Enter the row ");
  guessRow =  find.readInt();
  System.out.print("Enter the column ");
  guessColumn = find.readInt();
if (grid[guessRow][guessColumn] == false)
  System.out.println("miss");
else
{
  ships --;
  System.out.println("hit" + ships + " more left !");
}
}while(!(turns == 15 || ships == 0));
if (turns == 15)
{
 System.out.print("You lost the remaining ships are at points: "); 
 for(int i = 0; i < grid.length ;i++) {
   for(int j = 0; j < grid[0].length;j++){
     System.out.print(i + j);
 } }
}
else if (ships == 0);
System.out.print("You won!");
}
}

You only changed the assignment statment for int row ,You didnt change the other assignment statement for int column. Sorry its My fault, I didnt change it either.

it has to be like

column = (int) (Math.random() * grid[0].length);


fixed loop its not infinite but it always outputs

You lost the remaining ships are at points: 0123412345234563456745678You Win!

in the end ...

Why would you want to output the int "i" and int "j" value? just relax, your confusing your self, you can do this.

take the " ; " semicolon off of the "else if" statement too.

if (turns == 15)
{
 System.out.print("You lost the remaining ships are at points: "); 
 for(int i = 0; i < grid.length ;i++) {
   for(int j = 0; j < grid[0].length;j++){
     System.out.print(i + j);
 } }
}
else if (ships == 0);  // <-------------------see it? take that off
System.out.print("You won!"); //   
}
}

hey guys i fixed the program the game part works but in the end i dont output the right result (all the remaining points) can you fix the loop for me ?
thanks

{
 public static void start()
{
EasyReader find = new EasyReader(); //
boolean [][] grid = new boolean [5][5];
int guessRow, guessColumn, row, column, ships = 0, turns = 0;
do{

row = (int)(Math.random() * grid.length);
column = (int)(Math.random() * grid[0].length);
if (grid[row][column] == false)
{
 grid[row][column] = true; 
 ships ++;
}
}while (ships < 5);

for(int i = 0; i < grid.length; i++)
{
  for(int j=0; j < grid[0].length;j++)
  {
    System.out.print("+ "+grid[i][j]+" +");
  }
 System.out.println();
}

do{
  turns ++;
  System.out.print("Enter the row ");
  guessRow =  find.readInt();
  System.out.print("Enter the column ");
  guessColumn = find.readInt();
if (grid[guessRow][guessColumn] == false)
  System.out.println("miss");
else
{
  ships --;
  grid[guessRow][guessColumn] = false;
  System.out.println("hit - " + ships + " more left !");
}
}while(!(turns == 15 || ships == 0));
if (turns == 15)
{
 System.out.print("FAIL You only sunk " + (5 - ships) +" ships! The rest are at points: "); 
 for(int i = 0; i < grid.length ;i++) {
   for(int j = 0; j < grid[0].length;j++){  //<-------------this part doesnt work
     System.out.print("" + i + "" + j);
 } }
}
else if (ships == 0)
  System.out.print("You won in " + turns + " turns!");
}
}

hey guys i fixed the program the game part works but in the end i dont output the right result (all the remaining points) can you fix the loop for me ?
thanks

{
 public static void start()
{
EasyReader find = new EasyReader(); //
boolean [][] grid = new boolean [5][5];
int guessRow, guessColumn, row, column, ships = 0, turns = 0;
do{

row = (int)(Math.random() * grid.length);
column = (int)(Math.random() * grid[0].length);
if (grid[row][column] == false)
{
 grid[row][column] = true; 
 ships ++;
}
}while (ships < 5);

for(int i = 0; i < grid.length; i++)
{
  for(int j=0; j < grid[0].length;j++)
  {
    System.out.print("+ "+grid[i][j]+" +");
  }
 System.out.println();
}

do{
  turns ++;
  System.out.print("Enter the row ");
  guessRow =  find.readInt();
  System.out.print("Enter the column ");
  guessColumn = find.readInt();
if (grid[guessRow][guessColumn] == false)
  System.out.println("miss");
else
{
  ships --;
  grid[guessRow][guessColumn] = false;
  System.out.println("hit - " + ships + " more left !");
}
}while(!(turns == 15 || ships == 0));
if (turns == 15)
{
 System.out.print("FAIL You only sunk " + (5 - ships) +" ships! The rest are at points: "); 
 for(int i = 0; i < grid.length ;i++) {
   for(int j = 0; j < grid[0].length;j++){  //<-------------this part doesnt work
     System.out.print("" + i + "" + j);
 } }
}
else if (ships == 0)
  System.out.print("You won in " + turns + " turns!");
}
}

I helped you with your code and you owned it like you figured it out all by your self. No one will help you here man if you dont show any effort. What your asking us to do is write it for you, we are here to advice, it is your job to write it. I

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.