2013-04-17 109 views
0

我正在製作一個戰艦遊戲,我根據一定的尺寸放置某些船隻。出於某種原因,我放置船隻的方法不起作用。填充2d int數組int [] []一定次數

供參考用2種方法的代碼(想過我會貼全班因爲它只有我敢在約2種重要的方法)

public class Board { 

    public static final int ID_EMPTY = 0; 
public static final int ID_BATTLESHIP = 1; 
public static final int ID_AIRCRAFT_CARRIER = 2; 
public static final int ID_DESTORYER_1 = 3; 
public static final int ID_DESTORYER_2 = 4; 
public static final int ID_PT_BOAT = 5; 

private static final int ROW_COUNT = 10; 
private static final int COLUMN_COUNT = 10; 

private static final int SHIPS_PER_FLEET = 5; 

private Ship[] fleet; 
private int[][] gridCells; 

private Random randomizer = new Random(); 


public Board() { 

    this.fleet = new Ship[SHIPS_PER_FLEET]; 
    this.gridCells = new int[ROW_COUNT][COLUMN_COUNT]; 
    // Fill the grid cells with OPEN WATER -1s. 
     int i = 0; 
     while (i < ROW_COUNT) { 
      int j = 0; 
      while (j < COLUMN_COUNT) { 
       this.gridCells[i][j] = Ship.openWater; 
       j++; 
      } 

      i++; 
     } 

    } 

/* 
* add a ship to the grid. 
*/ 
    public void placeShips(Ship newShip){ 
      int row = newShip.getRow(); 
       int column = newShip.getColumn(); 
       int orientation = newShip.getOrientation(); 
       int i = 0; 

       // Add the ship to the fleet array. 
       this.fleet[newShip.getshipType()] = newShip; 

       if (orientation == Ship.orientationUp) { 
        while (i < newShip.getShipLenght()) { 
         this.gridCells[row - i][column] = newShip.getshipType(); 
         i++; 
        } 
       } 
       else if (orientation == Ship.orientationRight) { 
        while (i < newShip.getShipLenght()) { 
         this.gridCells[row][column + i] = newShip.getshipType(); 
         i++; 
        } 
       } 
       else if (orientation == Ship.orientationDown) { 
        while (i < newShip.getShipLenght()) { 
         this.gridCells[row + i][column] = newShip.getshipType(); 
         i++; 
        } 
       } 
       else { 
        // Orientation must be LEFT. Only one left =] 
        while (i < newShip.getShipLenght()) { 
         this.gridCells[row][column - i] = newShip.getshipType(); 
         i++; 
        } 
       } 

    } 
public void placeShipsRandomly(){ 


     int [] shipType = {Ship.aircraftCarrier, 
      Ship.battleship, 
      Ship.Destoryer_1, 
      Ship.Destoryer_2, 
      Ship.PtBoat}; 

     int[] shipLength = {5, 4, 3, 3, 2}; 

      int i = 0; 

       do { 
        int row; 
        int col; 
        int orientation; 

        // Randomly generate a row, column, and orientation. 

        row = randomizer.nextInt(ROW_COUNT); 
        col = randomizer.nextInt(COLUMN_COUNT); 
        orientation = randomizer.nextInt(4); 


        boolean bFitsOnBoard = false; 

        // Check to see if the ship fits on the board at the given row and column. 

        int testLength = shipLength[i] -1; 

        if (orientation == Ship.orientationUp) { 
         if (row >= testLength) { 
          bFitsOnBoard = true; 
         } 
        } 
        else if (orientation == Ship.orientationRight) { 
         if (COLUMN_COUNT - col > testLength) { 
          bFitsOnBoard = true; 
         } 
        } 
        else if (orientation == Ship.orientationDown) { 
         if (row - ROW_COUNT > testLength) { 
          bFitsOnBoard = true; 
         } 
        } 
        else if (orientation == Ship.orientationLeft) { 
         if (col >= testLength) { 
          bFitsOnBoard = true; 
         } 

        boolean bHitsOtherShips = false; 

        // Check to see if the ship hits any other ships on the board. 

        if (bFitsOnBoard == true) { 
         int j; 
         if (orientation == Ship.orientationUp) { 
          j = 0; 
          while (j < shipLength[i]) { 
           if (this.gridCells[row - j][col] != Ship.openWater) { 
            bHitsOtherShips = true; 
            break; 
           } 

           j++; 
          } 
         } 
         else if (orientation == Ship.orientationRight) { 
          j = 0; 
          while (j < shipLength[i]) { 
           if (this.gridCells[row][col + j] != Ship.openWater) { 
            bHitsOtherShips = true; 
            break; 
           } 

           j++; 
          } 
         } 
         else if (orientation == Ship.orientationDown) { 
          j = 0; 
          while (j < shipLength[i]) { 
           if (this.gridCells[row + j][col] != Ship.openWater) { 
            bHitsOtherShips = true; 
            break; 
           } 

           j++; 
          } 
         } 
         else if (orientation == Ship.orientationLeft) { 
          j = 0; 
          while (j < shipLength[i]) { 
           if (this.gridCells[row][col - j] != Ship.openWater) { 
            bHitsOtherShips = true; 
            break; 
           } 

           j++; 
          } 
         } 
        } 
        if ((bFitsOnBoard == true) && (bHitsOtherShips == false)) { 
          // Place this ship on the board. 
         Ship newShip = new Ship(shipType[i], orientation, row, col, shipLength[i]); 

         this.placeShips(newShip); 

         // Go on to the next ship. 
         i++; 
          } 
        } 
      } 
       while (i < SHIPS_PER_FLEET); 
} 
    /* 
    * returns the grid cell 
    */ 
    public int[][] getGridCell() 
    { 
    return this.gridCells; 
    } 


} 

它有2個問題。

主要的問題是,在程序的某些運行過程中,它創建了一個超出界限的錯誤,並試圖放置一個不存在的行10,因爲int [10] [10]多達9 ofcourse,因爲他們從0開始等

第二個問題是我試圖根據自己的尺寸才能將船但是它似乎把所有的船舶並給他們大小的船隻3

因此,例如讓我們說這是數組輸出。

[0] [0] [3] [3] [3] [0] [0] [0] [0] [0] 
[0] [0] [0] [0] [0] [0] [0] [0] [0] [0] 
[0] [0] [0] [0] [0] [4] [0] [0] [0] [0] 
[0] [0] [0] [0] [0] [4] [0] [0] [0] [0] 
[0] [0] [0] [0] [0] [4] [0] [0] [0] [0] 
[0] [0] [0] [0] [0] [0] [0] [0] [0] [0] 
[0] [0] [0] [0] [1] [1] [1] [0] [0] [0] 
[0] [0] [0] [0] [0] [0] [0] [0] [2] [0] 
[0] [0] [0] [0] [0] [0] [0] [0] [2] [0] 
[0] [0] [0] [0] [0] [0] [0] [0] [2] [0] 

,因爲我已經取得船舶的長度是錯的INT [] shipLength = {5,4,3,3,2};

因此,根據它在地方法方法上所經歷的週期,它應該每次放置不同尺寸的船,除了第三艘和第四艘都是刨冰船並且尺寸相同的船。

我正在腦凍結,無法弄清楚發生了什麼事,有人可以幫我一把嗎?

+2

粘貼你的代碼在這裏供將來參考。隨着時間的推移,鏈接將會消亡,使得這些內容對未來的讀者來說無用。此外,您只粘貼了一個關鍵方法 - 我們是否也可以看到第二個方法,以幫助調試問題? – torquestomp

+0

我忘了在問題出現的地方包括整個班級,我編輯了問題以包含它。我在上面評論中粘貼的類是與@torquestomp鏈接的類。還編輯正確的問題,並根據以前的請求輸入代碼到問題中。 – JARRRRG

回答

2

你有兩個簡單的錯誤,我可以看到:

else if (orientation == Ship.orientationLeft) { 
    if (col >= testLength) { 
     bFitsOnBoard = true; 
    } 
// ED: Where's the '}'? 

boolean bHitsOtherShips = false; 

// Check to see if the ship hits any other ships on the board. 

這裏沒有括號關閉否則,如果 - 你的代碼是不是做你覺得它在做什麼。只有隨機滾動方向== Ship.orientationLeft的船隻實際上是進行碰撞測試,然後可能被放置。

其次,你的船()構造函數(!這不應該埋在評論一個外部鏈接),有這樣的簽名:

public Ship(int shipType, int shipLength, int row, int column, int orientation) { 

但是,當你宣佈你的新船放置:

Ship newShip = new Ship(shipType[i], orientation, row, col, shipLength[i]); 

方向和shipLength變量交換!由於只有具有方向類型Ship.orientationLeft的船舶正在實例化,並且因爲Ship.orientationLeft = 3,所有正在繪製的船隻的長度爲3.

至於索引越界,這很容易。由於您將shipLength作爲方向提交,因此代碼中的任何邊界檢查均不表示任何內容,並且可能會輕鬆地發生超出範圍的情況。

+0

我希望我在2小時前完成了這項工作。我嘗試了一些完全不同的方法來克服這一點。說實話,我真的很感謝你的幫助。 – JARRRRG