2015-11-07 59 views
2

所以ia玩家在2D陣列上,當我做一個動作時,我希望玩家移動到他周圍的8個可用塊之一,下面的代碼隨機移動他,但是做了兩次隨機移動到一個二維數組中的新實例

地圖移動

GrassGrassGrassGrass

草REK GrassGrass

GrassGrassGrassGrass

重啓前assGrassGrassGrass

隨機運動

0 0 //這不應該發生

地圖移動

GrassGrassGrassGrass

GrassGrassGrassGrass

GrassGrassGr後assGrass

GrassGrassGrass REK

import java.util.Random; 

public class command_Movment implements command_Move { 


inSwamp map = new inSwamp(); 
inSwamp rek = new Rek(); 
Random random = new Random(); 


int row = random.nextInt(3); 
int col = random.nextInt(3); 

@Override 
public Command move() { 

    for (int i = 0; i < map.grid.length; i++) { 
     for (int j = 0; j < map.grid[i].length; j++) { 

      if (map.grid[i][j] == rek.getName()) { 

       try { 
        map.grid[i][j] = "Grass"; 
        if (row == 0) { 
         i++; 
        } 
        if (row == 1) { 
         i--; 
        } 

        if (col == 0) { 
         j++; 
        } 
        if (col == 1) { 
         j--; 
        } 

        map.grid[i][j] = rek.getName(); 
        System.out.println(col + "  " + row); 

        break; 

       } catch (ArrayIndexOutOfBoundsException exception) { 
        if (row == 0) { 
         i--; 
        } 
        if (row == 1) { 
         i++; 
        } 

        if (col == 0) { 
         j--; 
        } 
        if (col == 1) { 
         j++; 
        } 
        map.grid[i][j] = rek.getName(); 
        System.out.println("Error"); 

        break; 
       } 



     } 
    } 


} 

    return null; 

} }

回答

2

首先,你不應該使用==比較字符串,你應該使用equals方法。所以用if (map.grid[i][j].equals(rek.getName()))代替if (map.grid[i][j] == rek.getName())

編輯請不要使用標籤打破程序的模塊化! 請不要使用捕獲ArrayIndexOutofBound異常來確定數組索引是否正確。 例外應該不是發生。你應該首先檢查索引。

我更新了我的隨機移動程序:基本上我瘦你想: 1)隨機向上或向下移動從原始位置2)如果向上移動或向下移動超過矩陣的邊界,不要朝這個方向移動。

以下程序應該將rek隨機移動到其8個鄰居之一,而不會導致任何ArrayIndexOutOfBoundException。

public Command move() { 
    // randomly determine the moving direction 
    // -1 means move left, 1 means move right 
    int horizontal_direction = Math.random() > 0.5 ? -1 : 1; 
    // -1 means move up, 1 mean move down 
    int vertical_direction = Math.random() > 0.5 ? -1 : 1; 

    for (int i = 0; i < map.grid.length; i++) { 
     for (int j = 0; j < map.grid[i].length; j++) { 

      if (map.grid[i][j].equals(rek.getName())) { 
       map.grid[i][j] = "Grass"; // replace rek's current position with Grass\ 

       // if the newRow exceeds the boundaries, don't move in that direction 
       int newRow = i + horizontal_direction; 
       if (newRow < 0 || newRow == map.grid.length) 
        newRow = i; 

       // if the newCol exceeds the boundaries, don't move in that direction 
       int newCol = j + vertical_direction; 
       if (newCol < 0 || newCol == map.grid[i].length) 
        newCol = j; 

       map.grid[newRow][newCol] = rek.getName(); // move rek to the new position 

       System.out.println(newRow + "  " + newCol); 
       break; 
      } 
     } 
    } 
    return null; 
} 
+0

謝謝,我已經將它們更改爲.equals,並且代碼不在網格中的任何位置,只有rek周圍的8個可用塊。 – Help

+0

@MichaelAlexanderDelaney更新了代碼,這應該符合你的要求。 – Bon

0

這樣的標籤添加到您的外環:

outer: 
for (int i = 0; i < map.grid.length; i++) { 
    ...... 
} 

而在嘗試塊,打破循環這方式:

map.grid[i][j] = rek.getName(); 
System.out.println(col + "  " + row); 

break outer; 
+0

謝謝,這真是棒極了,從來不知道 – Help

+1

之前,可以以其他方式解決這個問題該函數,你知道的。但是這種「黑客」在這種情況下似乎是有益的。受到裝配體的GOTO和JUMP TO指令的啓發。 –

+0

@JDev我從來不知道這存在於Java中。我拒絕了,因爲我同意你的評論 - 這是非常hacky並打破了計劃的模塊化。 – Bon