package gui; import javafx.event.EventHandler; import java.awt.Insets; import javafx.scene.layout.BorderPane; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.input.InputEvent; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import tictactoeFX.Coordinate; import tictactoeFX.Game; import tictactoeFX.Game.Turn; public class TicTacToePane extends BorderPane { Label[][] labels; Game g; GridPane grid = new GridPane(); Button undo = new Button("undo"); Button redo = new Button("redo"); public void addButton() { grid.getChildren().add(undo); grid.getChildren().add(redo); } public void startGame() { setUpLabels(); addButton(); setCenter(grid); g = new Game(); } public void setTop() { HBox box = new HBox(); box.setSpacing(10); box.setStyle("-fx-background-color: #ff69b4;"); Button undo = new Button("Undo"); undo.setPrefSize(50.0,30.0); Button redo = new Button("Redo"); redo.setPrefSize(50.0,90.0); box.getChildren().addAll(undo, redo); setTop(box); } 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 < 3) { 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"); } } } }); } }