Browse Source

Extensive changes

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

8
Battleship.java

@ -90,6 +90,7 @@ public class Battleship{
}
}
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 ++){
@ -104,8 +105,11 @@ public class Battleship{
}
public static void main(String[] args){
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();
test.print();
test.placeOnBoard(testBoard);
for (int i = 0; i < 4; i++){
test.print();
test.placeOnBoard(testBoard);
@ -113,5 +117,7 @@ public class Battleship{
test.rotate(stationary,1);
}
//printBoard(testBoard);
}
}

41
Ship.java

@ -7,25 +7,18 @@ public class Ship{
private int[] end;
private int length;
public Ship(int[] start,int[] end){
int cord;
boolean vertical = start[Y] == end[Y];
this.vertical = vertical;
if (vertical) {
cord = X;
}
else {
cord = Y;
}
if (start[cord] < end[cord]){
this.start = start;
this.end = end;
this.correctSE();
int cord;
if (this.vertical) {
cord = Y;
}
else {
this.start = end;
this.end = start;
cord = X;
}
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
private void correntSE(){
private void correctSE(){
int cord;
this.vertical = this.start[X] == this.end[X];
if (this.vertical) {
cord = X;
cord = Y;
}
else {
cord = Y;
cord = X;
}
int[] third_point;
@ -122,7 +116,7 @@ public class Ship{
for (int i = 0; i < n; i++){
this.rotate(point);
}
this.correntSE();
this.correctSE();
}
public boolean isVertical() {
@ -190,16 +184,19 @@ public class Ship{
}
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 c;
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 {
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