填充2d int数组int [] []一定次数

时间:2013-04-17 00:05:55

标签: java arrays

我正在进行战舰游戏,我根据特定尺寸放置某些船只。出于某种原因,我放置船只的方法并不起作用。

参考使用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;
    }


}

它有两个问题。

主要的问题是,在程序的某些运行中,它正在创建一个越界错误并试图在第10行放置一个不存在的船,因为只有int [10] [10]的数组因为他们从0等开始,所以最多可以达到9个。

第二个问题是我试图根据它们的尺寸放置船只,但它似乎放置了所有船只,并给它们所有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};

因此,基于它在放置方法上经历的周期,它应该每次都放置不同尺寸的船,而不是第3艘和第4艘船,它们都是deystroyers并且具有相同的尺寸。

我的大脑冻结了,无法弄清楚发生了什么,有人可以帮我一把吗?

1 个答案:

答案 0 :(得分: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实际上是进行碰撞测试,然后可能被放置。

其次,你的Ship()构造函数(不应该包含在注释中的外部链接!),有这个签名:

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作为方向,因此代码中较早的边界检查都不代表任何内容,并且可以轻松地发生超出范围的情况。