Monty374

JFX 7.8

Dec 7th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. package toetictac;
  2.  
  3. import javafx.application.Application;
  4. import javafx.stage.Stage;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.control.Label;
  8. import javafx.scene.layout.BorderPane;
  9. import javafx.scene.layout.GridPane;
  10. import javafx.scene.layout.Pane;
  11. import javafx.scene.paint.Color;
  12. import javafx.scene.shape.Line;
  13. import javafx.scene.shape.Ellipse;
  14.  
  15. public class ToeTicTac extends Application {
  16. // Indicate which player has a turn, initially it is the X player
  17. private char whoseTurn = 'X';
  18.  
  19. // Create and initialize cell
  20. private Cell[][] cell = new Cell[3][3];
  21.  
  22. // Create and initialize a status label
  23. private Label lblStatus = new Label("X's turn to play");
  24.  
  25. @Override // Override the start method in the Application class
  26. public void start(Stage primaryStage) {
  27. // Pane to hold cell
  28. GridPane pane = new GridPane();
  29. for (int i = 0; i < 3; i++)
  30. for (int j = 0; j < 3; j++)
  31. pane.add(cell[i][j] = new Cell(), j, i);
  32. ;
  33.  
  34. BorderPane borderPane = new BorderPane();
  35. borderPane.setCenter(pane);
  36. borderPane.setBottom(lblStatus);
  37.  
  38.  
  39.  
  40. // Create a scene and place it in the stage
  41. Scene scene = new Scene(borderPane, 450, 450);
  42. primaryStage.setTitle("TicTacToe"); // Set the stage title
  43. primaryStage.setScene(scene); // Place the scene in the stage
  44. primaryStage.show(); // Display the stage
  45. }
  46.  
  47. /** Determine if the cell are all occupied */
  48. public boolean isFull() {
  49. for (int i = 0; i < 3; i++)
  50. for (int j = 0; j < 3; j++)
  51. if (cell[i][j].getToken() == ' ')
  52. return false;
  53.  
  54. return true;
  55. }
  56.  
  57. /** Determine if the player with the specified token wins */
  58. public boolean isWon(char token) {
  59. for (int i = 0; i < 3; i++)
  60. if (cell[i][0].getToken() == token
  61. && cell[i][1].getToken() == token
  62. && cell[i][2].getToken() == token) {
  63. return true;
  64. }
  65.  
  66. for (int j = 0; j < 3; j++)
  67. if (cell[0][j].getToken() == token
  68. && cell[1][j].getToken() == token
  69. && cell[2][j].getToken() == token) {
  70. return true;
  71. }
  72.  
  73. if (cell[0][0].getToken() == token
  74. && cell[1][1].getToken() == token
  75. && cell[2][2].getToken() == token) {
  76. return true;
  77. }
  78.  
  79. if (cell[0][2].getToken() == token
  80. && cell[1][1].getToken() == token
  81. && cell[2][0].getToken() == token) {
  82. return true;
  83. }
  84.  
  85. return false;
  86. }
  87.  
  88. // An inner class for a cell
  89. public class Cell extends Pane {
  90. // Token used for this cell
  91. private char token = ' ';
  92.  
  93. public Cell() {
  94. setStyle("-fx-border-color: black");
  95. this.setPrefSize(2000, 2000);
  96. this.setOnMouseClicked(e -> handleMouseClick());
  97. }
  98.  
  99. /** Return token */
  100. public char getToken() {
  101. return token;
  102. }
  103.  
  104. /** Set a new token */
  105. public void setToken(char c) {
  106. token = c;
  107.  
  108. if (token == 'X') {
  109. Line line1 = new Line(10, 10,
  110. this.getWidth() - 10, this.getHeight() - 10);
  111. line1.endXProperty().bind(this.widthProperty().subtract(10));
  112. line1.endYProperty().bind(this.heightProperty().subtract(10));
  113. Line line2 = new Line(10, this.getHeight() - 10,
  114. this.getWidth() - 10, 10);
  115. line2.startYProperty().bind(
  116. this.heightProperty().subtract(10));
  117. line2.endXProperty().bind(this.widthProperty().subtract(10));
  118.  
  119. // Add the lines to the pane
  120. this.getChildren().addAll(line1, line2);
  121. }
  122. else if (token == 'O') {
  123. Ellipse ellipse = new Ellipse(this.getWidth() / 2,
  124. this.getHeight() / 2, this.getWidth() / 2 - 10,
  125. this.getHeight() / 2 - 10);
  126. ellipse.centerXProperty().bind(
  127. this.widthProperty().divide(2));
  128. ellipse.centerYProperty().bind(
  129. this.heightProperty().divide(2));
  130. ellipse.radiusXProperty().bind(
  131. this.widthProperty().divide(2).subtract(10));
  132. ellipse.radiusYProperty().bind(
  133. this.heightProperty().divide(2).subtract(10));
  134. ellipse.setStroke(Color.BLACK);
  135. ellipse.setFill(Color.WHITE);
  136.  
  137. getChildren().add(ellipse); // Add the ellipse to the pane
  138. }
  139. }
  140.  
  141. /* Handle a mouse click event */
  142. private void handleMouseClick() {
  143. // If cell is empty and game is not over
  144. if (token == ' ' && whoseTurn != ' ') {
  145. setToken(whoseTurn); // Set token in the cell
  146.  
  147. // Check game status
  148. if (isWon(whoseTurn)) {
  149. lblStatus.setText(whoseTurn + " won! The game is over");
  150. whoseTurn = ' '; // Game is over
  151. }
  152. else if (isFull()) {
  153. lblStatus.setText("Draw! The game is over");
  154. whoseTurn = ' '; // Game is over
  155. }
  156. else {
  157. // Change the turn
  158. whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
  159. // Display whose turn
  160. lblStatus.setText(whoseTurn + "'s turn");
  161. }
  162. }
  163. }
  164. }
  165.  
  166.  
  167.  
  168. public static void main(String[] args) {
  169. launch(args);
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment