Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ticTacToe;
- import javafx.application.Application;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.GridPane;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- public class TicTacToeApplication extends Application{
- private GameLogic game;
- @Override
- public void init() throws Exception{
- //create the game logic used by the app
- this.game = new GameLogic();
- }
- @Override
- public void start(Stage stage){
- Label gameInfo = new Label("Turn: X");
- GridPane grid = new GridPane();
- BorderPane layout = new BorderPane();
- Button[][] buttons = new Button[3][3];
- for(int row = 0; row < 3; row++){
- for(int col = 0; col < 3; col++){
- Button btn = new Button("");
- buttons[row][col] = btn;
- grid.add(btn, col, row);
- final int r = row;
- final int c = col;
- btn.setOnAction((event)->{
- if(game.makeMove(r, c)){
- btn.setText(game.getTurn().name());
- if(game.getState().name().equals("PLAYING")){
- game.changeTurn();
- gameInfo.setText(game.getTurn().name());
- }else{
- gameInfo.setText(game.getState().name());
- }
- }
- });
- }
- }
- layout.setTop(gameInfo);
- layout.setCenter(grid);
- Scene view = new Scene(layout);
- stage.setScene(view);
- stage.show();
- }
- public static void main(String[] args) {
- launch(TicTacToeApplication.class);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement