Advertisement
werkstattdreid

TicTacToeApplication.java

Apr 7th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. package ticTacToe;
  2.  
  3. import javafx.application.Application;
  4. import javafx.stage.Stage;
  5. import javafx.scene.Scene;
  6. import javafx.scene.layout.BorderPane;
  7. import javafx.scene.layout.GridPane;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.control.Label;
  10.  
  11.  
  12.  
  13. public class TicTacToeApplication extends Application{
  14.  
  15.  
  16. private GameLogic game;
  17.  
  18. @Override
  19. public void init() throws Exception{
  20. //create the game logic used by the app
  21. this.game = new GameLogic();
  22. }
  23.  
  24. @Override
  25. public void start(Stage stage){
  26.  
  27. Label gameInfo = new Label("Turn: X");
  28. GridPane grid = new GridPane();
  29. BorderPane layout = new BorderPane();
  30. Button[][] buttons = new Button[3][3];
  31.  
  32. for(int row = 0; row < 3; row++){
  33. for(int col = 0; col < 3; col++){
  34. Button btn = new Button("");
  35. buttons[row][col] = btn;
  36. grid.add(btn, col, row);
  37.  
  38. final int r = row;
  39. final int c = col;
  40.  
  41. btn.setOnAction((event)->{
  42. if(game.makeMove(r, c)){
  43. btn.setText(game.getTurn().name());
  44. if(game.getState().name().equals("PLAYING")){
  45. game.changeTurn();
  46. gameInfo.setText(game.getTurn().name());
  47. }else{
  48. gameInfo.setText(game.getState().name());
  49. }
  50. }
  51. });
  52. }
  53. }
  54.  
  55. layout.setTop(gameInfo);
  56. layout.setCenter(grid);
  57.  
  58. Scene view = new Scene(layout);
  59.  
  60. stage.setScene(view);
  61. stage.show();
  62. }
  63.  
  64. public static void main(String[] args) {
  65. launch(TicTacToeApplication.class);
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement