Browse Source

removed trailing whitespace

master
school 7 years ago
parent
commit
4804c8da76
  1. 28
      Battleship.java
  2. 70
      BattleshipMain.java
  3. 67
      Ship.java
  4. 6
      ShipTest.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};
@ -40,17 +40,17 @@ 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;
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,7 +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 ++){
@ -98,7 +98,7 @@ public class Battleship{
if (col < arr[row].length - 1){ if (col < arr[row].length - 1){
s += " "; s += " ";
} }
} }
System.out.println(s + "\n"); System.out.println(s + "\n");
} }
@ -109,15 +109,15 @@ public class Battleship{
int[] stationary = test.getStart().clone(); int[] stationary = test.getStart().clone();
test.print(); test.print();
test.placeOnBoard(testBoard); 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);
printBoard(testBoard); printBoard(testBoard);
test.rotate(stationary,1); test.rotate(stationary,1);
} }
//printBoard(testBoard); //printBoard(testBoard);
} }
} }

70
BattleshipMain.java

@ -15,45 +15,45 @@ import javafx.stage.Stage;
import javafx.util.Duration; import javafx.util.Duration;
import java.util.Scanner; import java.util.Scanner;
public class BattleshipMain extends Application
public class BattleshipMain extends Application
{ {
private GridPane windowScreen = new GridPane(); private GridPane windowScreen = new GridPane();
private Battleship battle; private Battleship battle;
@Override @Override
public void start(Stage primaryStage) public void start(Stage primaryStage)
{
{
this.battle = new Battleship(10, 10); this.battle = new Battleship(10, 10);
startingGame(); startingGame();
this.windowScreen.setAlignment(Pos.CENTER); this.windowScreen.setAlignment(Pos.CENTER);
Scene scene = new Scene(this.windowScreen, battle.getPlayerBoard().length * 70, battle.getPlayerBoard()[0].length * 60); Scene scene = new Scene(this.windowScreen, battle.getPlayerBoard().length * 70, battle.getPlayerBoard()[0].length * 60);
primaryStage.setTitle("Battleship"); primaryStage.setTitle("Battleship");
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.show(); primaryStage.show();
promptingPlayer(); promptingPlayer();
} }
public void startingGame() public void startingGame()
{ {
this.windowScreen.setPadding(new Insets(10, 10, 10, 10)); this.windowScreen.setPadding(new Insets(10, 10, 10, 10));
Text playerBoard = new Text(" Player Board"); Text playerBoard = new Text(" Player Board");
playerBoard.setFont(Font.font("Arial", 20)); playerBoard.setFont(Font.font("Arial", 20));
GridPane.setConstraints(playerBoard, 1, 1); GridPane.setConstraints(playerBoard, 1, 1);
setBoardValues(this.battle.getPlayerBoard(), 1); setBoardValues(this.battle.getPlayerBoard(), 1);
Text computerBoard = new Text(" Computer Board"); Text computerBoard = new Text(" Computer Board");
computerBoard.setFont(Font.font("Arial", 20)); computerBoard.setFont(Font.font("Arial", 20));
GridPane.setConstraints(computerBoard, 1, 12); GridPane.setConstraints(computerBoard, 1, 12);
setBoardValues(this.battle.getComputerBoardDisplay(), 12);
setBoardValues(this.battle.getComputerBoardDisplay(), 12);
addSpacing(); addSpacing();
this.windowScreen.getChildren().addAll(playerBoard, computerBoard);
this.windowScreen.getChildren().addAll(playerBoard, computerBoard);
} }
public void setBoardValues(int[][] p, int columnOffset) public void setBoardValues(int[][] p, int columnOffset)
{ {
for(int row = 0; row < p.length; row++) for(int row = 0; row < p.length; row++)
@ -69,11 +69,11 @@ public class BattleshipMain extends Application
player.setFill(Color.WHITE); player.setFill(Color.WHITE);
else else
player.setFill(Color.LIGHTGRAY); player.setFill(Color.LIGHTGRAY);
this.windowScreen.add(player, row + 4, col + columnOffset); this.windowScreen.add(player, row + 4, col + columnOffset);
} }
} }
public void addSpacing() public void addSpacing()
{ {
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
@ -83,7 +83,7 @@ public class BattleshipMain extends Application
Text blankSpace2 = new Text(" "); Text blankSpace2 = new Text(" ");
this.windowScreen.add(blankSpace2, i + 14, 1); this.windowScreen.add(blankSpace2, i + 14, 1);
} }
for (int i = 1; i < 11; i++) for (int i = 1; i < 11; i++)
{ {
Text number = new Text("" + i); Text number = new Text("" + i);
@ -94,16 +94,16 @@ public class BattleshipMain extends Application
this.windowScreen.add(numberHorizontal, 3 + i, 11); this.windowScreen.add(numberHorizontal, 3 + i, 11);
} }
} }
public void promptingPlayer() public void promptingPlayer()
{ {
TextField xInput = createInput("x", 10); TextField xInput = createInput("x", 10);
TextField yInput = createInput("y", 11); TextField yInput = createInput("y", 11);
Button sendCoordinates = new Button(" Send Coordinates "); Button sendCoordinates = new Button(" Send Coordinates ");
this.windowScreen.add(sendCoordinates, 16, 12); this.windowScreen.add(sendCoordinates, 16, 12);
sendCoordinates.setOnAction( e -> calculatingMove(xInput.getText(), yInput.getText()));
sendCoordinates.setOnAction( e -> calculatingMove(xInput.getText(), yInput.getText()));
} }
public TextField createInput(String coord, int colLoc) public TextField createInput(String coord, int colLoc)
{ {
TextField input = new TextField(); TextField input = new TextField();
@ -111,36 +111,36 @@ public class BattleshipMain extends Application
this.windowScreen.add(input, 16, colLoc); this.windowScreen.add(input, 16, colLoc);
return input; return input;
} }
public void calculatingMove(String xInput, String yInput) public void calculatingMove(String xInput, String yInput)
{ {
int[][] computerActual = this.battle.getComputerBoardHidden(); int[][] computerActual = this.battle.getComputerBoardHidden();
int[][] computerDisplay = this.battle.getComputerBoardDisplay(); int[][] computerDisplay = this.battle.getComputerBoardDisplay();
int[][] playerBoard = this.battle.getPlayerBoard(); int[][] playerBoard = this.battle.getPlayerBoard();
boolean playerWin = false, computerWin = false, checkingHit = false; boolean playerWin = false, computerWin = false, checkingHit = false;
int x, y = -1; int x, y = -1;
Text usedCoord = new Text(" Pick again"); Text usedCoord = new Text(" Pick again");
usedCoord.setFont(Font.font("Arial", 12)); usedCoord.setFont(Font.font("Arial", 12));
this.windowScreen.add(usedCoord, 16, 16); this.windowScreen.add(usedCoord, 16, 16);
usedCoord.setOpacity(0.0); usedCoord.setOpacity(0.0);
x = Integer.parseInt(xInput) - 1; x = Integer.parseInt(xInput) - 1;
y = Integer.parseInt(yInput) - 1; y = Integer.parseInt(yInput) - 1;
if ( x < 0 || x > 9 ) if ( x < 0 || x > 9 )
handleError("x", 14); handleError("x", 14);
if ( y < 0 || y > 9 ) if ( y < 0 || y > 9 )
handleError("y", 15); handleError("y", 15);
if ((x >= 0 && x <= 9) && (y >= 0 && y <= 9)) if ((x >= 0 && x <= 9) && (y >= 0 && y <= 9))
{ {
checkingHit = computerDisplay[x][y] == 0; checkingHit = computerDisplay[x][y] == 0;
} }
if (checkingHit) if (checkingHit)
{ {
this.battle.updateComputerBoards(x, y, computerActual, computerDisplay); this.battle.updateComputerBoards(x, y, computerActual, computerDisplay);
@ -154,21 +154,21 @@ public class BattleshipMain extends Application
FadeTransition fading = creatingFader(usedCoord); FadeTransition fading = creatingFader(usedCoord);
fading.play(); fading.play();
} }
if (playerWin) if (playerWin)
handleWin("You"); handleWin("You");
if (computerWin) if (computerWin)
handleWin("Computer");
handleWin("Computer");
} }
public void handleWin(String player) public void handleWin(String player)
{ {
Text winner = new Text(" " + player + " won!"); Text winner = new Text(" " + player + " won!");
winner.setFont(Font.font("Arial", 18)); winner.setFont(Font.font("Arial", 18));
this.windowScreen.add(winner, 16, 18); this.windowScreen.add(winner, 16, 18);
} }
public void handleError(String coord, int colLoc) public void handleError(String coord, int colLoc)
{ {
Text error = new Text(" Invalid " + coord + " coordinate."); Text error = new Text(" Invalid " + coord + " coordinate.");
@ -178,7 +178,7 @@ public class BattleshipMain extends Application
FadeTransition fading = creatingFader(error); FadeTransition fading = creatingFader(error);
fading.play(); fading.play();
} }
private FadeTransition creatingFader(Node node) private FadeTransition creatingFader(Node node)
{ {
FadeTransition fade = new FadeTransition(Duration.seconds(3), node); FadeTransition fade = new FadeTransition(Duration.seconds(3), node);
@ -186,7 +186,7 @@ public class BattleshipMain extends Application
fade.setToValue(0); fade.setToValue(0);
return fade; return fade;
} }
public static void main(String[] args) public static void main(String[] args)
{ {
Application.launch(args); Application.launch(args);

67
Ship.java

@ -6,7 +6,7 @@ public class Ship{
private int[] start; private int[] start;
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.start = start;
this.end = end; this.end = end;
@ -18,25 +18,25 @@ public class Ship{
else { else {
cord = X; cord = X;
} }
this.length = this.end[cord] - this.start[cord]+1; this.length = this.end[cord] - this.start[cord]+1;
} }
public static int randint(int a, int b){ public static int randint(int a, int b){
int rand_dist = (int) (Math.random() * (double) (b + 1 - a)); int rand_dist = (int) (Math.random() * (double) (b + 1 - a));
return rand_dist + a; return rand_dist + a;
} }
// creates new random ship // creates new random ship
public static Ship randomShip(int[] start, int[] end,int length, boolean vert) { public static Ship randomShip(int[] start, int[] end,int length, boolean vert) {
int[] s_start = new int[2]; int[] s_start = new int[2];
s_start[X] = randint(start[X],end[X]); s_start[X] = randint(start[X],end[X]);
s_start[Y] = randint(start[Y],end[Y]); s_start[Y] = randint(start[Y],end[Y]);
int[] s_end = s_start.clone(); int[] s_end = s_start.clone();
int cord; int cord;
// what direction do we want this pointing? // what direction do we want this pointing?
if (vert){ if (vert){
cord = Y; cord = Y;
@ -44,7 +44,7 @@ public class Ship{
else { else {
cord = X; cord = X;
} }
// check if out of bounds only one way, because if it doesn't work that way it SHOULD work the other way // check if out of bounds only one way, because if it doesn't work that way it SHOULD work the other way
if (s_start[cord] + length-1 < end[cord]) { if (s_start[cord] + length-1 < end[cord]) {
s_end[cord] += length-1; s_end[cord] += length-1;
@ -52,7 +52,7 @@ public class Ship{
else { else {
s_end[cord] -= length-1; s_end[cord] -= length-1;
} }
return new Ship(s_start,s_end); return new Ship(s_start,s_end);
} }
public int getLength() { public int getLength() {
@ -64,7 +64,7 @@ public class Ship{
public int[] getEnd() { public int[] getEnd() {
return this.end; return this.end;
} }
// ensures start and end are in the right place // ensures start and end are in the right place
private void correctSE(){ private void correctSE(){
int cord; int cord;
@ -75,59 +75,59 @@ public class Ship{
else { else {
cord = X; cord = X;
} }
int[] third_point; int[] third_point;
if (start[cord] > end[cord]){ if (start[cord] > end[cord]){
third_point = this.start; third_point = this.start;
this.start = this.end; this.start = this.end;
this.end = third_point; this.end = third_point;
} }
} }
private void rotate(int[] point){ private void rotate(int[] point){
// one point is tacked to the board and the other is moved around // one point is tacked to the board and the other is moved around
int[] stationary_point; int[] stationary_point;
int[] moving_point; int[] moving_point;
// either the start or end will be stationary // either the start or end will be stationary
if (Arrays.equals(this.start,point)) { if (Arrays.equals(this.start,point)) {
moving_point = this.end; moving_point = this.end;
stationary_point = this.start; stationary_point = this.start;
} }
else { else {
moving_point = this.start; moving_point = this.start;
stationary_point = this.end; stationary_point = this.end;
} }
// get the new coordinates // get the new coordinates
int new_x = stationary_point[X] + stationary_point[Y] - moving_point[Y]; int new_x = stationary_point[X] + stationary_point[Y] - moving_point[Y];
int new_y = moving_point[X] + stationary_point[Y] - stationary_point[X]; int new_y = moving_point[X] + stationary_point[Y] - stationary_point[X];
// set them // set them
moving_point[X] = new_x; moving_point[X] = new_x;
moving_point[Y] = new_y; moving_point[Y] = new_y;
} }
public void rotate(int[] point, int n){ public void rotate(int[] point, int n){
for (int i = 0; i < n; i++){ for (int i = 0; i < n; i++){
this.rotate(point); this.rotate(point);
} }
this.correctSE(); this.correctSE();
} }
public boolean isVertical() { public boolean isVertical() {
return this.vertical; return this.vertical;
} }
public boolean isInside(int[] start, int[] end){ public boolean isInside(int[] start, int[] end){
return this.start[X] >= start[X] && this.end[X] <= end[X] && return this.start[X] >= start[X] && this.end[X] <= end[X] &&
this.start[Y] >= start[Y] && this.end[Y] <= end[Y]; this.start[Y] >= start[Y] && this.end[Y] <= end[Y];
} }
public boolean isIntersecting(Ship other){ public boolean isIntersecting(Ship other){
boolean cond1; boolean cond1;
boolean cond2; boolean cond2;
@ -144,32 +144,32 @@ public class Ship{
cord2 = X; cord2 = X;
} }
// </necessary bit> // </necessary bit>
int t_s1 = this.start[cord1]; int t_s1 = this.start[cord1];
int t_e1 = this.end[cord1]; int t_e1 = this.end[cord1];
int o_s1 = other.start[cord1]; int o_s1 = other.start[cord1];
int o_e1 = other.end[cord1]; int o_e1 = other.end[cord1];
int t_s2 = this.start[cord2]; int t_s2 = this.start[cord2];
int t_e2 = this.end[cord2]; int t_e2 = this.end[cord2];
int o_s2 = other.start[cord2]; int o_s2 = other.start[cord2];
int o_e2 = other.end[cord2]; int o_e2 = other.end[cord2];
// lines are parallel // lines are parallel
if (this.vertical == other.vertical) { if (this.vertical == other.vertical) {
// overlaps // overlaps
cond1 = t_s2 == o_s2; cond1 = t_s2 == o_s2;
// intersects // intersects
cond2 = o_e1 >= t_s1; cond2 = o_e1 >= t_s1;
cond3 = t_e1 >= o_s1; cond3 = t_e1 >= o_s1;
return cond1 && (cond2 || cond3); return cond1 && (cond2 || cond3);
} }
// lines are perpendicular // lines are perpendicular
else{ else{
// other in range of this with respect to axis cord1 // other in range of this with respect to axis cord1
cond1 = (t_s1 >= o_s1) && (t_e1 <= o_e1); cond1 = (t_s1 >= o_s1) && (t_e1 <= o_e1);
// this in range of other with respect to axis cord2 // this in range of other with respect to axis cord2
@ -177,12 +177,12 @@ public class Ship{
return cond1 && cond2; return cond1 && cond2;
} }
} }
public void print() { public void print() {
System.out.println("Start: " + "(" + this.start[X] + ", " + this.start[Y] + ")"); System.out.println("Start: " + "(" + this.start[X] + ", " + this.start[Y] + ")");
System.out.println("End: " + "(" + this.end[X] + ", " + this.end[Y] + ")"); System.out.println("End: " + "(" + this.end[X] + ", " + this.end[Y] + ")");
} }
public void placeOnBoard(int[][] board){ public void placeOnBoard(int[][] board){
this.correctSE(); this.correctSE();
int x_col = this.start[X]; int x_col = this.start[X];
@ -200,5 +200,4 @@ public class Ship{
} }
} }
} }
}
}

6
ShipTest.java

@ -5,7 +5,7 @@ public class ShipTest{
fill[i] = Ship.randomShip(start,end,Ship.randint(2,4),true); fill[i] = Ship.randomShip(start,end,Ship.randint(2,4),true);
} }
} }
public static void main(String[] args){ public static void main(String[] args){
Ship[] test_ships = new Ship[5]; Ship[] test_ships = new Ship[5];
createTestShips(new int[] {0,0}, new int[] {5,5},test_ships); createTestShips(new int[] {0,0}, new int[] {5,5},test_ships);
@ -13,9 +13,9 @@ public class ShipTest{
test_ships[i].print(); test_ships[i].print();
System.out.println(); System.out.println();
test_ships[i+1].print(); test_ships[i+1].print();
System.out.println(test_ships[i].isIntersecting(test_ships[i+1])); System.out.println(test_ships[i].isIntersecting(test_ships[i+1]));
System.out.println(); System.out.println();
} }
} }
}
}
Loading…
Cancel
Save