我正在开发一款战舰游戏,在该游戏中,我根据特定尺寸放置某些船只。出于某种原因,我放置船只的方法不起作用。
用于参考具有 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因为它们从 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};
因此,根据它在 placeships 方法中经历的循环,它应该每次都放置不同大小的船,除了第 3 艘和第 4 艘船,它们都是驱逐舰并且具有相同的尺寸。
我的大脑冻结了,无法弄清楚发生了什么,有人可以帮我吗?