Browse Source

has Ship.java combined into Battleship.java

combined
school 7 years ago
parent
commit
9e751528f6
  1. 199
      Battleship.java
  2. 198
      Ship.java

199
Battleship.java

@ -4,6 +4,7 @@
// Submarine = 3
// Pilot = 2
import java.util.Arrays;
public class Battleship{
public static final int X = 0;
public static final int Y = 0;
@ -163,3 +164,201 @@ public class Battleship{
}
}
}
class Ship{
public static final int X = 0;
public static final int Y = 1;
private static final boolean DEBUG = false;
private boolean vertical;
private int[] start;
private int[] end;
private int length;
public Ship(int[] start,int[] end){
this.start = start;
this.end = end;
this.correctSE();
int cord;
if (this.vertical) {
cord = Y;
}
else {
cord = X;
}
this.length = this.end[cord] - this.start[cord]+1;
}
public static int randint(int a, int b){
int rand_dist = (int) (Math.random() * (double) (b + 1 - a));
return rand_dist + a;
}
// creates new random ship
public static Ship randomShip(int[] boundTL, int[] boundBR,int length, boolean vert) {
int[] s_start = new int[2];
s_start[X] = randint(boundTL[X],boundBR[X]);
s_start[Y] = randint(boundTL[Y],boundBR[Y]);
int[] s_end = s_start.clone();
int cord;
// what direction do we want this pointing?
if (vert){
cord = Y;
}
else {
cord = X;
}
// check if out of bounds only one way, because if it doesn't work that way it SHOULD work the other way, unless the board cannot fit the ship (impossible with 10x10)
if (s_start[cord] + length-1 < boundBR[cord]) {
s_end[cord] += length-1;
}
else {
s_end[cord] -= length-1;
}
return new Ship(s_start,s_end);
}
public int getLength() {
return this.length;
}
public int[] getStart() {
return this.start;
}
public int[] getEnd() {
return this.end;
}
// ensures start and end are in the right place
private void correctSE(){
int cord;
this.vertical = this.start[X] == this.end[X];
if (this.vertical) {
cord = Y;
}
else {
cord = X;
}
int[] third_point;
if (start[cord] > end[cord]){
third_point = this.start;
this.start = this.end;
this.end = third_point;
}
}
private void rotate(int[] point){
// one point is tacked to the board and the other is moved around
int[] stationary_point;
int[] moving_point;
// either the start or end will be stationary
if (Arrays.equals(this.start,point)) {
moving_point = this.end;
stationary_point = this.start;
}
else {
moving_point = this.start;
stationary_point = this.end;
}
// get the new coordinates
int new_x = stationary_point[X] + stationary_point[Y] - moving_point[Y];
int new_y = moving_point[X] + stationary_point[Y] - stationary_point[X];
// set them
moving_point[X] = new_x;
moving_point[Y] = new_y;
}
public void rotate(int[] point, int n){
for (int i = 0; i < n; i++){
this.rotate(point);
}
this.correctSE();
}
public boolean isVertical() {
return this.vertical;
}
public boolean isInside(int[] boundTL, int[] boundBR){
return this.start[X] >= boundTL[X] && this.end[X] <= boundBR[X] &&
this.start[Y] >= boundTL[Y] && this.end[Y] <= boundBR[Y];
}
private static boolean inbetween (int a, int b, int c){
return a <= b && b <= c;
}
public boolean isIntersecting(Ship other){
boolean cond1;
boolean cond2;
boolean cond3;
int cord1;
int cord2;
// <necessary bit, ensures algorithm works both when other is oriented horizontally or vertically>
if (other.vertical) {
cord1 = X;
cord2 = Y;
}
else {
cord1 = Y;
cord2 = X;
}
// </necessary bit>
// lines are parallel
if (this.vertical == other.vertical) {
cond1 = this.start[cord1] == other.end[cord1];
cond2 = inbetween(this.start[cord2],other.start[cord2],this.end[cord2]);
cond3 = inbetween(other.start[cord2],this.start[cord2],other.end[cord2]);
return cond1 && (cond2 || cond3);
}
// lines are perpendicular
else{
cond1 = inbetween(other.start[cord2],this.start[cord2],other.end[cord2]);
cond2 = inbetween(this.start[cord1],other.start[cord1],this.end[cord1]);
return cond1 && cond2;
}
}
public void print() {
System.out.println("Start: " + "(" + this.start[X] + ", " + this.start[Y] + ")");
System.out.println("End: " + "(" + this.end[X] + ", " + this.end[Y] + ")");
}
public void placeOnBoard(int[][] board){
this.correctSE();
int x_col = this.start[X];
int y_col = this.start[Y];
int c;
if (this.vertical){
for (c = this.start[Y];c <= this.end[Y];c++){
if (DEBUG) {
board[c][x_col] = this.length;
}
else {
board[c][x_col] = 3;
}
}
}
else {
for (c = this.start[X];c <= this.end[X];c++){
if (DEBUG) {
board[y_col][c] = this.length;
}
else {
board[y_col][c] = 3;
}
}
}
}
}

198
Ship.java

@ -1,198 +0,0 @@
import java.util.Arrays;
public class Ship{
public static final int X = 0;
public static final int Y = 1;
private static final boolean DEBUG = false;
private boolean vertical;
private int[] start;
private int[] end;
private int length;
public Ship(int[] start,int[] end){
this.start = start;
this.end = end;
this.correctSE();
int cord;
if (this.vertical) {
cord = Y;
}
else {
cord = X;
}
this.length = this.end[cord] - this.start[cord]+1;
}
public static int randint(int a, int b){
int rand_dist = (int) (Math.random() * (double) (b + 1 - a));
return rand_dist + a;
}
// creates new random ship
public static Ship randomShip(int[] boundTL, int[] boundBR,int length, boolean vert) {
int[] s_start = new int[2];
s_start[X] = randint(boundTL[X],boundBR[X]);
s_start[Y] = randint(boundTL[Y],boundBR[Y]);
int[] s_end = s_start.clone();
int cord;
// what direction do we want this pointing?
if (vert){
cord = Y;
}
else {
cord = X;
}
// check if out of bounds only one way, because if it doesn't work that way it SHOULD work the other way, unless the board cannot fit the ship (impossible with 10x10)
if (s_start[cord] + length-1 < boundBR[cord]) {
s_end[cord] += length-1;
}
else {
s_end[cord] -= length-1;
}
return new Ship(s_start,s_end);
}
public int getLength() {
return this.length;
}
public int[] getStart() {
return this.start;
}
public int[] getEnd() {
return this.end;
}
// ensures start and end are in the right place
private void correctSE(){
int cord;
this.vertical = this.start[X] == this.end[X];
if (this.vertical) {
cord = Y;
}
else {
cord = X;
}
int[] third_point;
if (start[cord] > end[cord]){
third_point = this.start;
this.start = this.end;
this.end = third_point;
}
}
private void rotate(int[] point){
// one point is tacked to the board and the other is moved around
int[] stationary_point;
int[] moving_point;
// either the start or end will be stationary
if (Arrays.equals(this.start,point)) {
moving_point = this.end;
stationary_point = this.start;
}
else {
moving_point = this.start;
stationary_point = this.end;
}
// get the new coordinates
int new_x = stationary_point[X] + stationary_point[Y] - moving_point[Y];
int new_y = moving_point[X] + stationary_point[Y] - stationary_point[X];
// set them
moving_point[X] = new_x;
moving_point[Y] = new_y;
}
public void rotate(int[] point, int n){
for (int i = 0; i < n; i++){
this.rotate(point);
}
this.correctSE();
}
public boolean isVertical() {
return this.vertical;
}
public boolean isInside(int[] boundTL, int[] boundBR){
return this.start[X] >= boundTL[X] && this.end[X] <= boundBR[X] &&
this.start[Y] >= boundTL[Y] && this.end[Y] <= boundBR[Y];
}
private static boolean inbetween (int a, int b, int c){
return a <= b && b <= c;
}
public boolean isIntersecting(Ship other){
boolean cond1;
boolean cond2;
boolean cond3;
int cord1;
int cord2;
// <necessary bit, ensures algorithm works both when other is oriented horizontally or vertically>
if (other.vertical) {
cord1 = X;
cord2 = Y;
}
else {
cord1 = Y;
cord2 = X;
}
// </necessary bit>
// lines are parallel
if (this.vertical == other.vertical) {
cond1 = this.start[cord1] == other.end[cord1];
cond2 = inbetween(this.start[cord2],other.start[cord2],this.end[cord2]);
cond3 = inbetween(other.start[cord2],this.start[cord2],other.end[cord2]);
return cond1 && (cond2 || cond3);
}
// lines are perpendicular
else{
cond1 = inbetween(other.start[cord2],this.start[cord2],other.end[cord2]);
cond2 = inbetween(this.start[cord1],other.start[cord1],this.end[cord1]);
return cond1 && cond2;
}
}
public void print() {
System.out.println("Start: " + "(" + this.start[X] + ", " + this.start[Y] + ")");
System.out.println("End: " + "(" + this.end[X] + ", " + this.end[Y] + ")");
}
public void placeOnBoard(int[][] board){
this.correctSE();
int x_col = this.start[X];
int y_col = this.start[Y];
int c;
if (this.vertical){
for (c = this.start[Y];c <= this.end[Y];c++){
if (DEBUG) {
board[c][x_col] = this.length;
}
else {
board[c][x_col] = 3;
}
}
}
else {
for (c = this.start[X];c <= this.end[X];c++){
if (DEBUG) {
board[y_col][c] = this.length;
}
else {
board[y_col][c] = 3;
}
}
}
}
}
Loading…
Cancel
Save