Browse Source

randomzingBoard doesn't throw any errors

Still buggy and needs to be tested thoroughly
experiment
school 7 years ago
parent
commit
982b043447
  1. 47
      Battleship.java

47
Battleship.java

@ -6,7 +6,7 @@
public class Battleship{ public class Battleship{
public static final int X = 0; public static final int X = 0;
public static final int Y = 0; public static final int Y = 0;
private int[][] playerBoard; private int[][] playerBoard;
private int[][] computerBoardDisplay; private int[][] computerBoardDisplay;
private int[][] computerBoardHidden; private int[][] computerBoardHidden;
@ -17,22 +17,22 @@ public class Battleship{
this.computerBoardDisplay = new int[x][y]; this.computerBoardDisplay = new int[x][y];
this.computerBoardHidden = new int[x][y]; this.computerBoardHidden = new int[x][y];
} }
public int[][] getComputerBoardHidden(){ public int[][] getComputerBoardHidden(){
return this.computerBoardHidden; return this.computerBoardHidden;
} }
public int[][] getComputerBoardDisplay(){ public int[][] getComputerBoardDisplay(){
return this.computerBoardDisplay; return this.computerBoardDisplay;
} }
public int[][] getPlayerBoard(){ public int[][] getPlayerBoard(){
return this.playerBoard; return this.playerBoard;
} }
public static void randomizingBoard(int[][] board){ public static void randomizingBoard(int[][] board){
int[] boundTL = {0,0}; int[] boundTL = {0,0};
int[] boundBR = {board[0].length, board.length};
int[] boundBR = {board[0].length-1, board.length-1};
int length; int length;
int[] lengths = { int[] lengths = {
2, 2,
@ -40,30 +40,30 @@ public class Battleship{
3, 3,
4, 4,
5}; 5};
boolean placed; boolean placed;
Ship[] ships = new Ship[5]; Ship[] ships = new Ship[5];
ships[0] = Ship.randomShip(boundTL,boundBR,lengths[0],true); ships[0] = Ship.randomShip(boundTL,boundBR,lengths[0],true);
for (int i = 1; i < 5; i++){ for (int i = 1; i < 5; i++){
length = lengths[i]; length = lengths[i];
placed = false; placed = false;
while (!placed){ while (!placed){
Ship cur_ship = Ship.randomShip(boundTL,boundBR,length,i%2 == 0); Ship cur_ship = Ship.randomShip(boundTL,boundBR,length,i%2 == 0);
boolean intersects = false; boolean intersects = false;
int rot = 0; int rot = 0;
int[] stationary = cur_ship.getStart().clone(); int[] stationary = cur_ship.getStart().clone();
while ((rot < 4) && !placed) { while ((rot < 4) && !placed) {
// ensures ship doesn't intect with any that were previously placed // ensures ship doesn't intect with any that were previously placed
for (int j = 0; j < i + 1; j++){
for (int j = 0; j < i; j++){
intersects = cur_ship.isIntersecting(ships[j]); intersects = cur_ship.isIntersecting(ships[j]);
if (intersects){ if (intersects){
cur_ship.rotate(stationary,1); cur_ship.rotate(stationary,1);
rot += 1; rot += 1;
break; break;
} }
} }
// ensures ship is still in bounds after rotation // ensures ship is still in bounds after rotation
if (!cur_ship.isInside(boundTL,boundBR)) { if (!cur_ship.isInside(boundTL,boundBR)) {
@ -82,5 +82,28 @@ public class Battleship{
for (int i = 0; i < 5; i++){ for (int i = 0; i < 5; i++){
ships[i].placeOnBoard(board); ships[i].placeOnBoard(board);
} }
}
}
public static void printBoard(int[][] arr){
for (int row = 0; row < arr.length; row++){
String s = "";
for (int col = 0; col < arr[row].length; col ++){
if (arr[row][col] == 4){
s += "+";
}
else {
s += " ";
}
if (col < arr[row].length - 1){
s += " ";
}
}
System.out.println(s + "\n");
}
}
public static void main(String[] args){
int[][] testBoard = new int[10][10];
randomizingBoard(testBoard);
printBoard(testBoard);
}
} }
Loading…
Cancel
Save