我正在製作一個戰艦遊戲,我根據一定的尺寸放置某些船隻。出於某種原因,我放置船隻的方法不起作用。填充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};
因此,根據它在地方法方法上所經歷的週期,它應該每次放置不同尺寸的船,除了第三艘和第四艘都是刨冰船並且尺寸相同的船。
我正在腦凍結,無法弄清楚發生了什麼事,有人可以幫我一把嗎?
粘貼你的代碼在這裏供將來參考。隨着時間的推移,鏈接將會消亡,使得這些內容對未來的讀者來說無用。此外,您只粘貼了一個關鍵方法 - 我們是否也可以看到第二個方法,以幫助調試問題? – torquestomp
我忘了在問題出現的地方包括整個班級,我編輯了問題以包含它。我在上面評論中粘貼的類是與@torquestomp鏈接的類。還編輯正確的問題,並根據以前的請求輸入代碼到問題中。 – JARRRRG