2011-08-22 16 views
4

我見過它做這樣的:如何從java中的數組製作基於圖塊的地圖?

int[] map = 
    { 
     1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 

    }; 

在其中會有在該位置的所有1的是 對象如果我做一堵牆通過聲明這一點:

level.add(new Wall(x, y)); 

其中X和Y代表瓷磚位置,因此level.add(new Wall(0, 0));將在左上角形成一面牆,level.add(new Wall(20, 15));將在右下角形成一面牆。

我該如何做到這一點,讓地圖上的第一個值等於1,它會在左上角做一堵牆,如果地圖上的最後一個值是1,那麼它將使牆右下角的 ?

編輯:對於有有同樣的問題的人,這裏是我的解決方案,我,我終於用:

intro = getAudioClip(getCodeBase(), "intro.wav"); 
    int[][] map = { 
      { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 
      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } 

    }; 

    for (int x = 0; x < map.length; x++) { 
     for (int y = 0; y < map[x].length; y++) { 
      if(map[x][y] == 1) 
      level.add(new Wall(y, x)); 
     } 
    } 
+0

隨着你思考的方式,你需要一個偏移爲每一行確定設置了哪一列。如果你想採用基於數組的方式來做到這一點,你應該使用多維數組,如int [] [] map = {{0,0,0},{1,1,1}} ; – jluzwick

+0

但是,爲什麼即使使用1維數組來處理二維數組也很簡單? – Jems

+0

如果你在Wall和Empty之外添加更多tile類型,我會強烈建議使用enum。 – thedaian

回答

3

你應該用一個二維數組。

int[][] map = 
{ 
    {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
}; 

然後你可以訪問數組的位置來改變值。

map[0][0] = 1; 
map[15][20] = 1; 
+0

完美工作,謝謝。 – Derek

1
final int width = 20; 
final int height = 15; 
for(int i = 0; i < width * height; i++) { 
    if (map[i] == 1) 
     level.add(new Wall(i % width, i/height)); 
} 
+0

這只是填補了牆壁的地圖... – Derek

+0

@Deza糾正。 – Marcelo

3

我看到幾個像樣的解決方案

最簡單的辦法:

int[][] map = new int[20][15]; 

// set all to empty 
for(int i = 0; i < map.length; i++) { 
    for(int j = 0; j < map[0].length; j++) { 
     map[i][j] = 0; 
    } 
} 
// make those 2 walls 
map[0][0] = 1; 
map[1][0] = 1; 

更好的解決方案

class Coordinate { 
    final int x; 
    final int y; 
    Coordinate(int x, int y) { this.x = x ; this.y = y; } 
    public int hashCode() { return x >> 16 & y; } 
    public boolean equals(Object o) { if (o instanceof Coordinate) { Coordinate c = (Coordinate)o; return c.x==x && c.y==y;} return false; } 
} 
enum Tile { 
    EMPTY, 
    WALL, 
    TRAP 
} 

Map<Coordinate,Tile> map = new HashMap<Coordinate,Tile>(); 
for(int x = 0; x < width; x++) { 
    for(int y = 0; y < height; y++) { 
     map.put(new Coordinate(x,y),Tile.EMPTY); 
    } 
} 

map.put(new Coordinate(0,0), Tile.WALL); 
map.put(new Coordinate(0,1), Tile.WALL); 
+0

+1我喜歡你的OO解決方案。 – Marcelo

+0

初始化寬度*高度初始化HashMap會更好嗎?還有一個需要考慮的負載因素,地圖的大小永遠不會改變? – Jan

+0

您可能可以從中擠出一些微型優化。性能優勢可能不值得這段時間,除非它是1)在代碼的關鍵部分(緊密環,受互斥鎖保護的或類似的東西),2)每個構造對象的讀取數量非常少。 – corsiKa

相關問題