Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ticTacToe;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.layout.GridPane;
- import javafx.scene.text.Font;
- import javafx.geometry.Insets;
- public class TicTacToeApplication extends Application{
- Label gameState;
- String currentSymbol;
- public TicTacToeApplication(){
- this.gameState = new Label("Turn: X");
- this.gameState.setFont(Font.font("Monospaced", 40));
- this.gameState.setPadding(new Insets(10, 10, 10, 10));
- this.currentSymbol = "X";
- }
- public void start(Stage stage){
- BorderPane layout = new BorderPane();
- GridPane gameBoard = new GridPane();
- layout.setPrefSize(310, 350);
- gameBoard.setAlignment(Pos.CENTER);
- gameBoard.setVgap(10);
- gameBoard.setHgap(10);
- gameBoard.setPadding(new Insets(10, 10, 10, 10));
- int[][] blueprint = new int[3][3];
- for(int row = 0; row < blueprint.length; row++){
- for(int column = 0; column < blueprint[row].length; column++){
- gameBoard.add(createButton(), row, column);
- }
- }
- layout.setTop(gameState);
- layout.setCenter(gameBoard);
- Scene view = new Scene(layout);
- stage.setScene(view);
- stage.show();
- }
- public static void main(String[] args) {
- launch(TicTacToeApplication.class);
- }
- public Button createButton(){
- Button button = new Button();
- button.setFont(Font.font("Monospaced", 40));
- button.setOnAction((event) -> {
- if(button.getText().isEmpty()){
- button.setText(this.currentSymbol);
- changeSymbol();
- }
- });
- return button;
- }
- public void changeSymbol(){
- if(this.currentSymbol.equals("X")){
- this.currentSymbol = "O";
- }
- if(this.currentSymbol.equals("O")){
- this.currentSymbol = "X";
- }
- this.gameState.setText("Turn: " + this.currentSymbol);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment