Mmmag2

Untitled

Aug 29th, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. package ticTacToe;
  2.  
  3. import javafx.application.Application;
  4. import javafx.geometry.Pos;
  5. import javafx.stage.Stage;
  6. import javafx.scene.Scene;
  7. import javafx.scene.layout.BorderPane;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.layout.GridPane;
  11. import javafx.scene.text.Font;
  12. import javafx.geometry.Insets;
  13.  
  14.  
  15. public class TicTacToeApplication extends Application{
  16.  
  17. Label gameState;
  18. String currentSymbol;
  19.  
  20. public TicTacToeApplication(){
  21. this.gameState = new Label("Turn: X");
  22. this.gameState.setFont(Font.font("Monospaced", 40));
  23. this.gameState.setPadding(new Insets(10, 10, 10, 10));
  24. this.currentSymbol = "X";
  25. }
  26.  
  27. public void start(Stage stage){
  28. BorderPane layout = new BorderPane();
  29. GridPane gameBoard = new GridPane();
  30.  
  31. layout.setPrefSize(310, 350);
  32. gameBoard.setAlignment(Pos.CENTER);
  33. gameBoard.setVgap(10);
  34. gameBoard.setHgap(10);
  35. gameBoard.setPadding(new Insets(10, 10, 10, 10));
  36.  
  37.  
  38. int[][] blueprint = new int[3][3];
  39.  
  40. for(int row = 0; row < blueprint.length; row++){
  41. for(int column = 0; column < blueprint[row].length; column++){
  42. gameBoard.add(createButton(), row, column);
  43. }
  44. }
  45.  
  46. layout.setTop(gameState);
  47. layout.setCenter(gameBoard);
  48.  
  49. Scene view = new Scene(layout);
  50. stage.setScene(view);
  51. stage.show();
  52.  
  53.  
  54.  
  55.  
  56.  
  57. }
  58.  
  59. public static void main(String[] args) {
  60. launch(TicTacToeApplication.class);
  61. }
  62.  
  63. public Button createButton(){
  64. Button button = new Button();
  65. button.setFont(Font.font("Monospaced", 40));
  66. button.setOnAction((event) -> {
  67. if(button.getText().isEmpty()){
  68. button.setText(this.currentSymbol);
  69. changeSymbol();
  70.  
  71. }
  72. });
  73.  
  74. return button;
  75. }
  76.  
  77. public void changeSymbol(){
  78. if(this.currentSymbol.equals("X")){
  79. this.currentSymbol = "O";
  80. }
  81. if(this.currentSymbol.equals("O")){
  82. this.currentSymbol = "X";
  83. }
  84. this.gameState.setText("Turn: " + this.currentSymbol);
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment