java - Why does my programme runs sometimes, but take forever to run some other times? -


i trying make program generate battleship board. runs , generates board sometimes, @ other times keeps"run"ing. using netbeans on mac, if helpful. (you might notice, did not invoke methods throughout code, because programme not completed yet, it's halfway through) here programme:

class battlehipgame:

public class battleshipgame {      public static void main(string[] args) {      gamemaker.shipmaker();     }  } 

class gamemaker:

public class gamemaker { private static int numberofships;  public static void numberofshipssetter(int shipnumber)   { if (shipnumber<=5)     {         numberofships = shipnumber;         gamemaker.shipmaker();     }  }   public static void shipmaker()  {   ship aircraftcarrier = new ship();   ship battleship = new ship();   ship cruiser = new ship();   ship dest1 = new ship();   ship dest2 = new ship();    aircraftcarrier.sizesetter(5);  battleship.sizesetter(4);  cruiser.sizesetter(3);  dest1.sizesetter(2);  dest2.sizesetter(2);   aircraftcarrier.orientationsetter();  battleship.orientationsetter();  cruiser.orientationsetter();  dest1.orientationsetter();  dest2.orientationsetter();    aircraftcarrier.positionofship();  battleship.positionofship();  cruiser.positionofship();  dest1.positionofship();  dest2.positionofship();   for(int m=0; m<10; m++)     {         for(int g=0; g<10; g++)     {         system.out.print(position[g][m]+" ");     }     system.out.println("");     }       }   } 

class ship:

public class ship {   private int size;  private int shipid = 0;  private int orientationofship; public static final int[][] position = new int[10][10];  public void sizesetter(int shipsize)  {  if (shipsize<=5)  {     size= shipsize;     shipid=shipsize;  }  else  {      size =  0;  }  }   public void orientationsetter()//this method sets whether ship vertical or horizontal (horizontal 1, vertical 2) { int orientation =  1 + (int)(math.random()*2);  orientationofship = orientation; }  public void positionofship()//method gives ship position , updates array/board {     random randomno = new random();     if (orientationofship==1)//if orientation horizontal     {         int = 0;         int xposition = randomno.nextint(10-size);//this gives x position of leftmost block of ship           int yposition = randomno.nextint(9);//this gives y position of leftmost block of ship          boolean foo = false;                  {             while (foo==false&&i<size)             {                  if (position[xposition+i][yposition]!=0||xposition+size>10)//checks if ship hits ship or has parts outside "board"                     {                         foo = true;                     }                     i++;             }               if (foo)//if foo true (i.e if hits ship/spills) generate position ship                 {                          xposition = randomno.nextint(9-size)+1;                         yposition = randomno.nextint(9)+1;                  }          } while(foo);          (int bar = 0; bar<size; bar++)         {         position[xposition+bar][yposition] = shipid;         }     }       else if (orientationofship==2)//this vertical version of if       {         int = 0;         int ypos = randomno.nextint(10-size);           int xpos = randomno.nextint(9);          boolean isthere = false;                      {                 while (isthere==false&&i<size)                     {                         if (position[xpos][ypos+i]!=0||ypos+size>10)                             {                                 isthere = true;                             }                         i++;                     }                 if (isthere)                     {                                      ypos = randomno.nextint(9-size)+1;                         xpos = randomno.nextint(9)+1;                      }         } while(isthere);          (int incvar = 0; incvar<size; incvar++)         {         position[xpos][ypos+incvar] = shipid;         }      }   }  } 

in positionofship(), need set foo = false @ start of outer loop -- or if foo ever gets set true, outer loop run forever.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -