Inifite Loop in Java Code
<p>I am having a problem in my program concerning an infinite loop as well as a incorrect response. In my program , I am trying to randomly set up ships for a battleship game and I am having problems with the placing the ships part. I have coded it , but I am running into 2 problems , one being that I have an infinite loop somewhere but i dont know where and the next being that the pieces dont properly set up on the grid . I have looked through this code for a  long time and I havent found a fix. Here it is : </p>

<pre><code>    public void placeAllShips() {
  int direction = (int) Math.random()*2 ; 
  int p1 = 0 ; 
  int p2 = 0 ; 
  for(int ships = 1 ; ships &lt; 6 ; ships ++ ) { 
   p1 = (int)(Math.random()*10); 
   p2 = (int)(Math.random()*10);  
   if ( p1 !=0 &amp;&amp; p2!= 0 &amp;&amp; direction == 0 /* Horizontal Direction*/ ){ 
    for(int i= 0; i &lt; ships ; i ++ ){ 
     while(board[p1][p2+i].hasShip() == true || p2 + i &gt; 10 &amp;&amp; p2 - i &lt; 0 ){  
      randomize(p1,p2) ;
     }
    }
    for(int j = 0 ; j &lt; ships ; j ++ ) {
     board[p1][p2+j].setHasShip(true) ; 
    }

   } 
   else if ( p1 !=0 &amp;&amp; p2!= 0 &amp;&amp; direction == 1 /*Vertical Direction*/ ){ 
    for(int i= 0; i &lt; ships ; i ++ ){ 
     while(board[p1+i][p2].hasShip() == true || p1 + i &gt; 10 &amp;&amp; p1 - i &lt; 0 ){  
      randomize(p1,p2) ;
     }
    }
    for(int j = 0 ; j &lt; ships ; j ++ ) {
     board[p1+j][p2].setHasShip(true) ; 
    }

   }
  }
 } 

 public void randomize( int x , int y ) { 
  //Generates random numbers.
  x = (int)Math.random()*10 ;
  y = (int)Math.random()*10 ;
 }
</code></pre>

<p>Thanks for the help ! </p>
