Browse Source

Extensive changes

Ships is basically fully tested.
experiment
school 7 years ago
parent
commit
6c4f335d31
  1. 28
      Battleship.java
  2. 43
      Ship.java

28
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,19 +17,19 @@ 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-1, board.length-1}; int[] boundBR = {board[0].length-1, board.length-1};
@ -39,18 +39,18 @@ public class Battleship{
3, 3,
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;
System.out.println(length); System.out.println(length);
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);
cur_ship.print(); cur_ship.print();
boolean intersects = false; boolean intersects = false;
@ -79,7 +79,7 @@ public class Battleship{
rot += 1; rot += 1;
} }
} }
} }
} }
} }
@ -90,6 +90,7 @@ public class Battleship{
} }
} }
public static void printBoard(int[][] arr){ public static void printBoard(int[][] arr){
for (int row = 0; row < arr.length; row++){ for (int row = 0; row < arr.length; row++){
String s = ""; String s = "";
for (int col = 0; col < arr[row].length; col ++){ for (int col = 0; col < arr[row].length; col ++){
@ -104,8 +105,11 @@ public class Battleship{
} }
public static void main(String[] args){ public static void main(String[] args){
int[][] testBoard = new int[10][10]; int[][] testBoard = new int[10][10];
Ship test = new Ship(new int[] {5,5}, new int[] {9,5});
Ship test = new Ship(new int[] {5,5}, new int[] {5,9});
int[] stationary = test.getStart().clone(); int[] stationary = test.getStart().clone();
test.print();
test.placeOnBoard(testBoard);
for (int i = 0; i < 4; i++){ for (int i = 0; i < 4; i++){
test.print(); test.print();
test.placeOnBoard(testBoard); test.placeOnBoard(testBoard);
@ -113,5 +117,7 @@ public class Battleship{
test.rotate(stationary,1); test.rotate(stationary,1);
} }
//printBoard(testBoard);
} }
} }

43
Ship.java

@ -7,25 +7,18 @@ public class Ship{
private int[] end; private int[] end;
private int length; private int length;
public Ship(int[] start,int[] end){ public Ship(int[] start,int[] end){
this.start = start;
this.end = end;
this.correctSE();
int cord; int cord;
boolean vertical = start[Y] == end[Y];
this.vertical = vertical;
if (vertical) {
cord = X;
}
else {
if (this.vertical) {
cord = Y; cord = Y;
} }
if (start[cord] < end[cord]){
this.start = start;
this.end = end;
}
else { else {
this.start = end;
this.end = start;
cord = X;
} }
this.length = this.end[cord] - this.start[cord]+1; this.length = this.end[cord] - this.start[cord]+1;
} }
@ -73,13 +66,14 @@ public class Ship{
} }
// ensures start and end are in the right place // ensures start and end are in the right place
private void correntSE(){
private void correctSE(){
int cord; int cord;
this.vertical = this.start[X] == this.end[X];
if (this.vertical) { if (this.vertical) {
cord = X;
cord = Y;
} }
else { else {
cord = Y;
cord = X;
} }
int[] third_point; int[] third_point;
@ -122,7 +116,7 @@ public class Ship{
for (int i = 0; i < n; i++){ for (int i = 0; i < n; i++){
this.rotate(point); this.rotate(point);
} }
this.correntSE();
this.correctSE();
} }
public boolean isVertical() { public boolean isVertical() {
@ -190,18 +184,21 @@ public class Ship{
} }
public void placeOnBoard(int[][] board){ public void placeOnBoard(int[][] board){
int x_col = this.start[X]
this.correctSE();
int x_col = this.start[X];
int y_col = this.start[Y]; int y_col = this.start[Y];
int c;
if (this.vertical){ if (this.vertical){
for (int c = this.start[Y];c <= this.end[Y];c++){
board[x_col][c] = this.length;
for (c = this.start[Y];c <= this.end[Y];c++){
System.out.println(c);
board[c][x_col] = this.length;
} }
} }
else { else {
for (int c = this.start[X];c <= this.end[X];c++){
board[c][y_col] = this.lenth;
for (c = this.start[X];c <= this.end[X];c++){
board[y_col][c] = this.length;
} }
} }
} }
}
}
Loading…
Cancel
Save