特定のサイズに基づいて特定の船を配置する戦艦ゲームに取り組んでいます。何らかの理由で、船を配置する方法がうまくいきません。
参考までに、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つの問題があります。
主な問題は、プログラムの特定の実行で範囲外エラーが発生し、int[10][10] の配列がもちろん 9 までしか進まないため、存在しない行 10 に船を配置しようとしていることです。それらは0などから始まるため。
2 番目の問題は、サイズに基づいて船を配置しようとしているのですが、すべての船を配置し、サイズ 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 番目の船を除いて、毎回異なるサイズの船を配置する必要があります。ただし、両方とも駆逐艦であり、同じサイズです。
頭が固まって何が起こっているのか理解できません。誰か手を貸してくれませんか?