From 4804c8da76c4715040ca0ed2df857059e3389f16 Mon Sep 17 00:00:00 2001 From: school Date: Thu, 4 Oct 2018 17:31:16 -0500 Subject: [PATCH] removed trailing whitespace --- Battleship.java | 28 +++++++++--------- BattleshipMain.java | 70 ++++++++++++++++++++++----------------------- Ship.java | 67 +++++++++++++++++++++---------------------- ShipTest.java | 6 ++-- 4 files changed, 85 insertions(+), 86 deletions(-) diff --git a/Battleship.java b/Battleship.java index 1bb7f1f..7443484 100644 --- a/Battleship.java +++ b/Battleship.java @@ -6,7 +6,7 @@ public class Battleship{ public static final int X = 0; public static final int Y = 0; - + private int[][] playerBoard; private int[][] computerBoardDisplay; private int[][] computerBoardHidden; @@ -17,19 +17,19 @@ public class Battleship{ this.computerBoardDisplay = new int[x][y]; this.computerBoardHidden = new int[x][y]; } - + public int[][] getComputerBoardHidden(){ return this.computerBoardHidden; } - + public int[][] getComputerBoardDisplay(){ return this.computerBoardDisplay; } - + public int[][] getPlayerBoard(){ return this.playerBoard; } - + public static void randomizingBoard(int[][] board){ int[] boundTL = {0,0}; int[] boundBR = {board[0].length-1, board.length-1}; @@ -40,17 +40,17 @@ public class Battleship{ 3, 4, 5}; - + boolean placed; Ship[] ships = new Ship[5]; ships[0] = Ship.randomShip(boundTL,boundBR,lengths[0],true); for (int i = 1; i < 5; i++){ - + length = lengths[i]; placed = false; System.out.println(length); while (!placed){ - + Ship cur_ship = Ship.randomShip(boundTL,boundBR,length,i%2 == 0); cur_ship.print(); boolean intersects = false; @@ -79,7 +79,7 @@ public class Battleship{ rot += 1; } } - + } } } @@ -90,7 +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 ++){ @@ -98,7 +98,7 @@ public class Battleship{ if (col < arr[row].length - 1){ s += " "; } - + } System.out.println(s + "\n"); } @@ -109,15 +109,15 @@ public class Battleship{ int[] stationary = test.getStart().clone(); test.print(); test.placeOnBoard(testBoard); - + for (int i = 0; i < 4; i++){ test.print(); test.placeOnBoard(testBoard); printBoard(testBoard); test.rotate(stationary,1); } - + //printBoard(testBoard); - + } } diff --git a/BattleshipMain.java b/BattleshipMain.java index 6136a85..5bda322 100644 --- a/BattleshipMain.java +++ b/BattleshipMain.java @@ -15,45 +15,45 @@ import javafx.stage.Stage; import javafx.util.Duration; import java.util.Scanner; -public class BattleshipMain extends Application +public class BattleshipMain extends Application { private GridPane windowScreen = new GridPane(); private Battleship battle; - + @Override public void start(Stage primaryStage) - { + { this.battle = new Battleship(10, 10); startingGame(); this.windowScreen.setAlignment(Pos.CENTER); - + Scene scene = new Scene(this.windowScreen, battle.getPlayerBoard().length * 70, battle.getPlayerBoard()[0].length * 60); primaryStage.setTitle("Battleship"); primaryStage.setScene(scene); primaryStage.show(); - + promptingPlayer(); } - + public void startingGame() { this.windowScreen.setPadding(new Insets(10, 10, 10, 10)); - + Text playerBoard = new Text(" Player Board"); playerBoard.setFont(Font.font("Arial", 20)); GridPane.setConstraints(playerBoard, 1, 1); setBoardValues(this.battle.getPlayerBoard(), 1); - + Text computerBoard = new Text(" Computer Board"); computerBoard.setFont(Font.font("Arial", 20)); GridPane.setConstraints(computerBoard, 1, 12); - setBoardValues(this.battle.getComputerBoardDisplay(), 12); - + setBoardValues(this.battle.getComputerBoardDisplay(), 12); + addSpacing(); - - this.windowScreen.getChildren().addAll(playerBoard, computerBoard); + + this.windowScreen.getChildren().addAll(playerBoard, computerBoard); } - + public void setBoardValues(int[][] p, int columnOffset) { for(int row = 0; row < p.length; row++) @@ -69,11 +69,11 @@ public class BattleshipMain extends Application player.setFill(Color.WHITE); else player.setFill(Color.LIGHTGRAY); - + this.windowScreen.add(player, row + 4, col + columnOffset); } } - + public void addSpacing() { for (int i = 0; i < 2; i++) @@ -83,7 +83,7 @@ public class BattleshipMain extends Application Text blankSpace2 = new Text(" "); this.windowScreen.add(blankSpace2, i + 14, 1); } - + for (int i = 1; i < 11; i++) { Text number = new Text("" + i); @@ -94,16 +94,16 @@ public class BattleshipMain extends Application this.windowScreen.add(numberHorizontal, 3 + i, 11); } } - + public void promptingPlayer() { TextField xInput = createInput("x", 10); TextField yInput = createInput("y", 11); Button sendCoordinates = new Button(" Send Coordinates "); 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) { TextField input = new TextField(); @@ -111,36 +111,36 @@ public class BattleshipMain extends Application this.windowScreen.add(input, 16, colLoc); return input; } - + public void calculatingMove(String xInput, String yInput) { int[][] computerActual = this.battle.getComputerBoardHidden(); int[][] computerDisplay = this.battle.getComputerBoardDisplay(); int[][] playerBoard = this.battle.getPlayerBoard(); - + boolean playerWin = false, computerWin = false, checkingHit = false; - + int x, y = -1; - + Text usedCoord = new Text(" Pick again"); usedCoord.setFont(Font.font("Arial", 12)); this.windowScreen.add(usedCoord, 16, 16); usedCoord.setOpacity(0.0); - + x = Integer.parseInt(xInput) - 1; y = Integer.parseInt(yInput) - 1; - + if ( x < 0 || x > 9 ) handleError("x", 14); - + if ( y < 0 || y > 9 ) handleError("y", 15); - + if ((x >= 0 && x <= 9) && (y >= 0 && y <= 9)) { checkingHit = computerDisplay[x][y] == 0; } - + if (checkingHit) { this.battle.updateComputerBoards(x, y, computerActual, computerDisplay); @@ -154,21 +154,21 @@ public class BattleshipMain extends Application FadeTransition fading = creatingFader(usedCoord); fading.play(); } - + if (playerWin) handleWin("You"); - + if (computerWin) - handleWin("Computer"); + handleWin("Computer"); } - + public void handleWin(String player) { Text winner = new Text(" " + player + " won!"); winner.setFont(Font.font("Arial", 18)); this.windowScreen.add(winner, 16, 18); } - + public void handleError(String coord, int colLoc) { Text error = new Text(" Invalid " + coord + " coordinate."); @@ -178,7 +178,7 @@ public class BattleshipMain extends Application FadeTransition fading = creatingFader(error); fading.play(); } - + private FadeTransition creatingFader(Node node) { FadeTransition fade = new FadeTransition(Duration.seconds(3), node); @@ -186,7 +186,7 @@ public class BattleshipMain extends Application fade.setToValue(0); return fade; } - + public static void main(String[] args) { Application.launch(args); diff --git a/Ship.java b/Ship.java index 2759be5..f029e7e 100644 --- a/Ship.java +++ b/Ship.java @@ -6,7 +6,7 @@ public class Ship{ private int[] start; private int[] end; private int length; - + public Ship(int[] start,int[] end){ this.start = start; this.end = end; @@ -18,25 +18,25 @@ public class Ship{ 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[] start, int[] end,int length, boolean vert) { - + int[] s_start = new int[2]; s_start[X] = randint(start[X],end[X]); s_start[Y] = randint(start[Y],end[Y]); int[] s_end = s_start.clone(); - + int cord; - + // what direction do we want this pointing? if (vert){ cord = Y; @@ -44,7 +44,7 @@ public class Ship{ 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 if (s_start[cord] + length-1 < end[cord]) { s_end[cord] += length-1; @@ -52,7 +52,7 @@ public class Ship{ else { s_end[cord] -= length-1; } - + return new Ship(s_start,s_end); } public int getLength() { @@ -64,7 +64,7 @@ public class Ship{ public int[] getEnd() { return this.end; } - + // ensures start and end are in the right place private void correctSE(){ int cord; @@ -75,59 +75,59 @@ public class Ship{ 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[] start, int[] end){ return this.start[X] >= start[X] && this.end[X] <= end[X] && this.start[Y] >= start[Y] && this.end[Y] <= end[Y]; } - + public boolean isIntersecting(Ship other){ boolean cond1; boolean cond2; @@ -144,32 +144,32 @@ public class Ship{ cord2 = X; } // - + int t_s1 = this.start[cord1]; int t_e1 = this.end[cord1]; int o_s1 = other.start[cord1]; int o_e1 = other.end[cord1]; - + int t_s2 = this.start[cord2]; int t_e2 = this.end[cord2]; int o_s2 = other.start[cord2]; int o_e2 = other.end[cord2]; - + // lines are parallel if (this.vertical == other.vertical) { // overlaps cond1 = t_s2 == o_s2; - + // intersects cond2 = o_e1 >= t_s1; cond3 = t_e1 >= o_s1; - + return cond1 && (cond2 || cond3); } - + // lines are perpendicular else{ - + // other in range of this with respect to axis cord1 cond1 = (t_s1 >= o_s1) && (t_e1 <= o_e1); // this in range of other with respect to axis cord2 @@ -177,12 +177,12 @@ public class Ship{ 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]; @@ -200,5 +200,4 @@ public class Ship{ } } } - -} \ No newline at end of file +} diff --git a/ShipTest.java b/ShipTest.java index 970f061..504ae41 100644 --- a/ShipTest.java +++ b/ShipTest.java @@ -5,7 +5,7 @@ public class ShipTest{ fill[i] = Ship.randomShip(start,end,Ship.randint(2,4),true); } } - + public static void main(String[] args){ Ship[] test_ships = new Ship[5]; createTestShips(new int[] {0,0}, new int[] {5,5},test_ships); @@ -13,9 +13,9 @@ public class ShipTest{ test_ships[i].print(); System.out.println(); test_ships[i+1].print(); - + System.out.println(test_ships[i].isIntersecting(test_ships[i+1])); System.out.println(); } } -} \ No newline at end of file +} \ No newline at end of file