package gui; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.input.InputEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import tictactoeFX.Coordinate; import tictactoeFX.Game; import tictactoeFX.Game.Turn; public class TicTacToePane extends BorderPane { Label[][] labels; Game g; GridPane grid = new GridPane(); public void addButton() { Button undo = new Button("undo"); grid.getChildren().add(undo); Button redo = new Button("redo"); grid.getChildren().add(redo); } public void startGame() { setUpLabels(); g = new Game(); } private void setUpLabels() { labels = new Label[3][3]; for (int row = 0; row < 3; row++) for (int col = 0; col < 3; col++) { Label l = new Label(); setUpLabel(l, row, col); labels[row][col] = l; grid.add(l, col, row); } setCenter(grid); } private void setUpLabel(final Label l, final int row, final int col) { l.setPrefHeight(75); l.setPrefWidth(75); if (row < 2) { if (col > 0) l.getStyleClass().add("leftAndBottomBorder"); else l.getStyleClass().add("bottomBorder"); } else if (col > 0) l.getStyleClass().add("leftBorder"); l.setOnMouseClicked(new EventHandler() { @Override public void handle(InputEvent arg0) { if (!g.gameOver() && g.getTurn() == Turn.X) { if(g.makePlayerMove(row, col)) l.setText("X"); } if (!g.gameOver()&& g.getTurn() == Turn.O) { Coordinate c = g.makeAutoMove(); labels[c.getRow()][c.getCol()].setText("O"); } if (g.gameOver()) { if (g.getVictoryExists()) { Coordinate[] victoryCoords = g.getVictoryCoords(); for (Coordinate c : victoryCoords) labels[c.getRow()][c.getCol()].getStyleClass().add( "victory"); } } } }); } }