2015-11-30 71 views
0

我正在創建一個遊戲,其中有一個玩家對象Hek某些敵對物體,遊戲世界是沼澤。基本上沼澤可以被認爲是一個4x4網格,但沼澤的大小可以通過用戶輸入變化(即5x5,6x6,7x7,8x8)。爲什麼我的方法不能正常工作?

在規範中,我正在努力,詳細說明Hek如何移動。 Hek只能移動到相鄰的正方形,所以他可以基於最小4x4網格從一次選擇的最正方形是8.他移動到的正方形是隨機計算的,使用Random()

下面是我寫的模擬此代碼,但它似乎並沒有真正工作。我不知道我的while循環是否被錯誤地使用?

public void moveHek(Hek hek) { 




    boolean moveMade = false; 

    while (moveMade != true) { 
     Random rand = new Random(); 
     int newMove = rand.nextInt(8) + 1; 
     hek.setMove(newMove); 
     int x = hek.getMove(); 
     int prevXPos = hek.getXPosition(); 
     int prevYPos = hek.getYPosition(); 

     if (x == 1) { 
      while(prevXPos > 0 && prevYPos > 0) { //this if while checks that Hek's position isn't located within the top row 
       hek.setPosition(prevXPos - 1, prevYPos - 1); //and he isn't positioned within the first column of the map 
       int newX = hek.getXPosition(); //and then moves Hek into the position that is diagonally left upwards to him. 
       int newY = hek.getYPosition(); 
       this.addHek(hek, newX, newY); 
       moveMade = true; 
      } 
     } else if(x == 2) { 
      while (prevXPos > 0) { //this if while checks that Hek's position isn't located within the top row and then 
       hek.setPosition(prevXPos - 1, prevYPos); //moves Hek into the position that is directly above him. 
       int newX = hek.getXPosition(); 
       int newY = hek.getYPosition(); 
       this.addHek(hek, newX, newY); 
       moveMade = true; 
      } 
     } else if(x == 3) { 
      while (prevXPos > 0 && prevYPos < gameMap.length) { //this if while checks that Hek's position isn't located within the top row 
       hek.setPosition(prevXPos - 1, prevYPos + 1); //and he isn't positioned in the last column of the map, then moves him into the 
       int newX = hek.getXPosition(); //position that is diagonally right upwards to him. 
       int newY = hek.getYPosition(); 
       this.addHek(hek, newX, newY); 
       moveMade = true; 
      } 
     } else if(x == 4) { 
      while (prevYPos > 0) { //this if while checks that Hek's position isn't located within the first column of the map and then 
       hek.setPosition(prevXPos, prevYPos + 1); //moves him into the position that is directly left. 
       int newX = hek.getXPosition(); 
       int newY = hek.getYPosition(); 
       this.addHek(hek, newX, newY); 
       moveMade = true; 
      } 
     } else if(x == 5) { 
      while (prevYPos < gameMap.length) { //this if while checks that Hek's position isn't located within the last column of the map and then 
       hek.setPosition(prevXPos, prevYPos - 1); //moves him into the position that is directly right. 
       int newX = hek.getXPosition(); 
       int newY = hek.getYPosition(); 
       this.addHek(hek, newX, newY); 
       moveMade = true; 
      } 
     } else if(x == 6) { 
      while (prevXPos < gameMap.length && prevYPos > 0) { //this if while checks that Hek's position isn't located within the last row of the map and 
       hek.setPosition(prevXPos + 1, prevYPos - 1); //that his position isn't located in the first column, then moves him into the position 
       int newX = hek.getXPosition(); //that is diagonally left downwards to him. 
       int newY = hek.getYPosition(); 
       this.addHek(hek, newX, newY); 
       moveMade = true; 
      } 
     } else if(x == 7) { 
      while (prevXPos < gameMap.length) { //this if while checks that Hek's position isn't located within the last row of the map and then moves 
       hek.setPosition(prevXPos + 1, prevYPos); //him into the position directly below him. 
       int newX = hek.getXPosition(); 
       int newY = hek.getYPosition(); 
       this.addHek(hek, newX, newY); 
       moveMade = true; 
      } 
     } else if(x == 8) { 
      while (prevXPos < gameMap.length && prevYPos < gameMap.length) { //this if while checks that Hek's position isn't located within the last row 
       hek.setPosition(prevXPos + 1, prevYPos + 1); //and the last column of the map, then it moves him into the position that is diagonally 
       int newX = hek.getXPosition(); //right downwards to him. 
       int newY = hek.getYPosition(); 
       this.addHek(hek, newX, newY); 
       moveMade = true; 
      } 
     } 
    }   
} 

任何幫助將不勝感激,正在發生的錯誤是java.lang.ArrayIndexOutOfBoundsException: -1,有時內內的代碼,而循環甚至不執行呢?謝謝!

+0

請註明至極線您的錯誤是。該錯誤是由該區域外的newX或newY引起的 – JFPicard

回答

1

此塊:

while (prevYPos < gameMap.length) { //this if while checks that Hek's position isn't located within the last column of the map and then 
      hek.setPosition(prevXPos, prevYPos - 1); //moves him into the position that is directly right. 
      int newX = hek.getXPosition(); 
      int newY = hek.getYPosition(); 
      this.addHek(hek, newX, newY); 
      moveMade = true; 
    } 

y位置設置爲prevYPos-1,而不檢查prevYPos是大於零。

另外,如果你想避免去一個數組區間的頂部,你需要檢查(prevYPos + 1 < gameMap.length)而不是(prevYPos < gameMap.length)

相關問題