Tic tac toe

 avatar
unknown
java
4 years ago
3.3 kB
5
Indexable
package ticTacToe;

import java.lang.reflect.Array;
import java.util.HashSet;
import java.util.Set;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventTarget;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.Parent;
import javafx.scene.text.Font;



public class TicTacToeApplication extends Application implements EventHandler<ActionEvent> {

    private Label textTurn;
    private Button[][] matrix;
    private PlayerOne playerOne = new PlayerOne();
    private PlayerTwo playerTwo = new PlayerTwo();
    
    @Override
    public void start(Stage window) {
        
        this.matrix = new Button[3][3];
        
        BorderPane layout = new BorderPane();
        
        this.textTurn = new Label("Turn: X");
        this.textTurn.setFont(Font.font("Monospaced", 40));
        
        layout.setTop(this.textTurn);
        
        GridPane board = new GridPane();
        board.setHgap(10);
        board.setVgap(10);
        board.setPadding(new Insets(10, 10, 10, 10));
        
        
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                Button btn = new Button(" ");
                    btn.setFont(Font.font("Monospaced", 40));
                    String id = String.format("%d %d", i, j);
                    btn.setId(id);
                    btn.setOnAction(this);
                    this.matrix[i][j] = btn;
                board.add(btn, i, j);
            }
        }
        
        
        
        layout.setCenter(board);
        
        Scene scene = new Scene(layout);
        window.setScene(scene);
        window.show();
    }
    
  
    

    public static void main(String[] args) {
        launch(TicTacToeApplication.class);
        System.out.println("Hello world!");
    }

    @Override
    public void handle(ActionEvent actionEvent) {
        Button sourceButton = (Button) actionEvent.getSource();
        String[] parts = sourceButton.getId().split(" ");
        int x = Integer.valueOf(parts[0]);
        int y = Integer.valueOf(parts[1]);
       
        
        if (this.textTurn.getText().contains("X") && this.matrix[x][y].getText().isBlank()) {
            
            sourceButton.setText(this.playerOne.getTurn());
            
            this.textTurn.setText("Turn: O");
            matrix[x][y].setText(this.playerOne.getTurn());
        } else if (this.textTurn.getText().contains("O") && this.matrix[x][y].getText().isBlank()) {
           
            sourceButton.setText(this.playerTwo.getTurn());
            this.textTurn.setText("Turn: X");
            matrix[x][y].setText(this.playerTwo.getTurn());
        }
        System.out.printf("Button pressed: %s \n", sourceButton.getId());
        
    }
    
    public boolean checkForWin(int row, int column) {
        
   
        return true;
    }

}
Editor is loading...