title: creating a "dig out" game with a one dimensional array instead of two?
body: <p>I have a project in which I have to create a "dig out" game (A game in which you need to find coins,ect.There are so many games like this,like Dig Out and Mars Miner,ect.I want to create a simple version of them containing monsters.For more information you can look it up on Google.)without the multidimensional arrays.I have to create it with only a one-dimensional array.I draw the board and everything,but I have a problem and that is I don't want the player or the monsters to accidentally jump on a wrong place.</p>
  
  <p>For example,<a href="https://i.stack.imgur.com/iESSw.png" rel="nofollow noreferrer">in this picture</a>The player can go to the upper line's right if they intend to go to left.</p>
  
  <p>It's not that much of a problem for the player,but since I want the monster's movements to be automatic,I want to learn to control it.
  These are the variables:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> static String name;
   static int row,column;
   static int coins;
   static char[] gamePlace;
   static int i; //The most used variable for the loops.
   static int a=0;//Will be increased by one every time a new soil or stone is added.
   static int playerIndex;//The index of the player.
   static int go=0; //The index player wants to go to.
   static int dig=0; //The index player wants to dig out.
   static int monsterPlace;  //The index of the monster.</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is my board method:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> public static void printBoard() {  //Prints the board.
    int j,k=0;
    
    for(i=0;i&lt;column+2;i++)          //For the beauty of the game, a frame is drawn.
     System.out.print("-");
    System.out.println("");
    
    for(i=0;i&lt;row;i++) {
     
     System.out.print("|");
     for(j=0;j&lt;column;j++) {
      System.out.print(gamePlace[k]);
      k++;
      
    }
     System.out.println("|");
    }
    
    for(i=0;i&lt;column+2;i++)
     System.out.print("-");
    
   }</code></pre>
  </div>
  </div>
  </p>
  
  <p>So these were my monster methods:</p>
  
  <pre><code>    (I kept getting index out of bounds exception so I surrounded the "monster" method with try and catch,but now the monster doesn't move.What do you think I should do?)
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> public static void monster() {
    
    int monsterTurn = (int)(Math.random()*(4+1)-1); //Getting a number in between 1 and 4.
    try {
    if(monsterTurn==1 &amp;&amp; gamePlace[monsterPlace+1]=='.' || gamePlace[monsterPlace+1]=='!' ) {
     monsterMove(monsterPlace, monsterPlace+1); //If 1 is randomly chosen and the right is unoccupied the monster will go there.
              monsterPlace=monsterPlace+1;
    }
    }
    catch(Exception e) {
    }
    
    try {
    
    if(monsterTurn==2 &amp;&amp; gamePlace[monsterPlace-1]=='.' || gamePlace[monsterPlace-1]=='!') {
     monsterMove(monsterPlace, monsterPlace-1); //If 2 is randomly chosen and the left is unoccupied the monster will go there.
     monsterPlace=monsterPlace-1;
    }
    }
    catch(Exception e) {
    }
    
    
    try {
    
     if(monsterTurn==3 &amp;&amp; gamePlace[monsterTurn-column]=='.' || gamePlace[monsterPlace-column]=='!') {
     monsterMove(monsterPlace, monsterPlace-column);  //If 3 is randomly chosen and up is unoccupied the monster will go there.
     monsterPlace=monsterPlace-column;
    }
    }
    catch(Exception e) {
    }
    
    
    try {
    if(monsterTurn==4 &amp;&amp; gamePlace[monsterTurn+column]=='.' || gamePlace[monsterPlace+column]=='!') {
     monsterMove(monsterPlace, monsterPlace+column); //If 3 is randomly chosen and down is unoccupied the monster will go there.
        monsterPlace = monsterPlace+column;
    }
    }
    catch(Exception e) {
     
    }
  
     
    
   }
   
   public static void monsterMove(int place,int placeToGo) {
    char temp;
    int monsterColumn=0;
    int goToPlace=0;
    int columnNum=column;  //Created to make the comparison between the monster's column and the place it's going to go easier.
    
    for(i=0;i&lt;place;i++)  //Checks which column the monster is in and which column it is going to go,so it wouldn't accidently jump on the other side of the board.
     if(i==columnNum) {
      monsterColumn++;
      columnNum +=column;
      }
    
    for(i=0;i&lt;placeToGo;i++)
     if(i==columnNum) {
      goToPlace++;
      columnNum +=column;
     }
    
    
    if(monsterColumn==goToPlace) { //If the columns were the same,the monster will move.Else,the movement method will be recalled.
    temp = gamePlace[place];
    gamePlace[place] = gamePlace[placeToGo];
    gamePlace[placeToGo]=temp;
    }
    
    else
     monster();
    
   }
   </code></pre>
  </div>
  </div>
  </p>