You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import javafx.application.Application;
  2. import javafx.animation.FadeTransition;
  3. import javafx.geometry.Pos;
  4. import javafx.geometry.Insets;
  5. import javafx.scene.Node;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Button;
  8. import javafx.scene.control.TextField;
  9. import javafx.scene.layout.GridPane;
  10. import javafx.scene.paint.Color;
  11. import javafx.scene.shape.Rectangle;
  12. import javafx.scene.text.Text;
  13. import javafx.scene.text.Font;
  14. import javafx.stage.Stage;
  15. import javafx.util.Duration;
  16. import java.util.Scanner;
  17. public class BattleshipMain extends Application
  18. {
  19. private GridPane windowScreen = new GridPane();
  20. private Battleship battle;
  21. @Override
  22. public void start(Stage primaryStage)
  23. {
  24. this.battle = new Battleship(10, 10);
  25. startingGame();
  26. this.windowScreen.setAlignment(Pos.CENTER);
  27. Scene scene = new Scene(this.windowScreen, battle.getPlayerBoard().length * 70, battle.getPlayerBoard()[0].length * 60);
  28. primaryStage.setTitle("Battleship");
  29. primaryStage.setScene(scene);
  30. primaryStage.show();
  31. promptingPlayer();
  32. }
  33. public void startingGame()
  34. {
  35. this.windowScreen.setPadding(new Insets(10, 10, 10, 10));
  36. Text playerBoard = new Text(" Player Board");
  37. playerBoard.setFont(Font.font("Arial", 20));
  38. GridPane.setConstraints(playerBoard, 1, 1);
  39. setBoardValues(this.battle.getPlayerBoard(), 1);
  40. Text computerBoard = new Text(" Computer Board");
  41. computerBoard.setFont(Font.font("Arial", 20));
  42. GridPane.setConstraints(computerBoard, 1, 12);
  43. setBoardValues(this.battle.getComputerBoardDisplay(), 12);
  44. addSpacing();
  45. this.windowScreen.getChildren().addAll(playerBoard, computerBoard);
  46. }
  47. public void setBoardValues(int[][] p, int columnOffset)
  48. {
  49. for(int row = 0; row < p.length; row++)
  50. for(int col = 0; col < p[row].length; col++)
  51. {
  52. Rectangle player = new Rectangle(0, 0, 20, 20);
  53. player.setStroke(Color.BLACK);
  54. if(p[row][col] == 3)
  55. player.setFill(Color.BLACK);
  56. else if(p[row][col] == 2)
  57. player.setFill(Color.RED);
  58. else if(p[row][col] == 1)
  59. player.setFill(Color.WHITE);
  60. else
  61. player.setFill(Color.LIGHTGRAY);
  62. this.windowScreen.add(player, row + 4, col + columnOffset);
  63. }
  64. }
  65. public void addSpacing()
  66. {
  67. for (int i = 0; i < 2; i++)
  68. {
  69. Text blankSpace = new Text(" ");
  70. this.windowScreen.add(blankSpace, i + 2, 1);
  71. Text blankSpace2 = new Text(" ");
  72. this.windowScreen.add(blankSpace2, i + 14, 1);
  73. }
  74. for (int i = 1; i < 11; i++)
  75. {
  76. Text number = new Text("" + i);
  77. number.setFont(Font.font("Arial", 20));
  78. this.windowScreen.add(number, 3, 11 + i);
  79. Text numberHorizontal = new Text("" + i);
  80. numberHorizontal.setFont(Font.font("Arial", 20));
  81. this.windowScreen.add(numberHorizontal, 3 + i, 11);
  82. }
  83. }
  84. public void promptingPlayer()
  85. {
  86. TextField xInput = createInput("x", 10);
  87. TextField yInput = createInput("y", 11);
  88. Button sendCoordinates = new Button(" Send Coordinates ");
  89. this.windowScreen.add(sendCoordinates, 16, 12);
  90. sendCoordinates.setOnAction( e -> calculatingMove(xInput.getText(), yInput.getText()));
  91. }
  92. public TextField createInput(String coord, int colLoc)
  93. {
  94. TextField input = new TextField();
  95. input.setPromptText("Enter " + coord + " coordinate");
  96. this.windowScreen.add(input, 16, colLoc);
  97. return input;
  98. }
  99. public void calculatingMove(String xInput, String yInput)
  100. {
  101. int[][] computerActual = this.battle.getComputerBoardHidden();
  102. int[][] computerDisplay = this.battle.getComputerBoardDisplay();
  103. int[][] playerBoard = this.battle.getPlayerBoard();
  104. boolean playerWin = false, computerWin = false, checkingHit = false;
  105. int x, y = -1;
  106. Text usedCoord = new Text(" Pick again");
  107. usedCoord.setFont(Font.font("Arial", 12));
  108. this.windowScreen.add(usedCoord, 16, 16);
  109. usedCoord.setOpacity(0.0);
  110. x = Integer.parseInt(xInput) - 1;
  111. y = Integer.parseInt(yInput) - 1;
  112. if ( x < 0 || x > 9 )
  113. handleError("x", 14);
  114. if ( y < 0 || y > 9 )
  115. handleError("y", 15);
  116. if ((x >= 0 && x <= 9) && (y >= 0 && y <= 9))
  117. {
  118. checkingHit = computerDisplay[x][y] == 0;
  119. }
  120. if (checkingHit)
  121. {
  122. this.battle.updateComputerBoards(x, y, computerActual, computerDisplay);
  123. playerWin = this.battle.checkPlayerWin();
  124. this.battle.computerTurn();
  125. computerWin = this.battle.checkComputerWin();
  126. setBoardValues(this.battle.getPlayerBoard(), 1);
  127. setBoardValues(this.battle.getComputerBoardDisplay(), 12);
  128. } else {
  129. usedCoord.setOpacity(1.0);
  130. FadeTransition fading = creatingFader(usedCoord);
  131. fading.play();
  132. }
  133. if (playerWin)
  134. handleWin("You");
  135. if (computerWin)
  136. handleWin("Computer");
  137. }
  138. public void handleWin(String player)
  139. {
  140. Text winner = new Text(" " + player + " won!");
  141. winner.setFont(Font.font("Arial", 18));
  142. this.windowScreen.add(winner, 16, 18);
  143. }
  144. public void handleError(String coord, int colLoc)
  145. {
  146. Text error = new Text(" Invalid " + coord + " coordinate.");
  147. error.setFont(Font.font("Arial", 12));
  148. this.windowScreen.add(error, 16, colLoc);
  149. error.setOpacity(1.0);
  150. FadeTransition fading = creatingFader(error);
  151. fading.play();
  152. }
  153. private FadeTransition creatingFader(Node node)
  154. {
  155. FadeTransition fade = new FadeTransition(Duration.seconds(3), node);
  156. fade.setFromValue(1);
  157. fade.setToValue(0);
  158. return fade;
  159. }
  160. public static void main(String[] args)
  161. {
  162. Application.launch(args);
  163. }
  164. }