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; public void startGame() { setUpLabels(); g = new Game(); } public void setTop() { HBox hbox = new HBox(); hbox.setSpacing(10); hbox.setStyle("-fx-background-color: #336699;"); Button u = new Button("Undo"); u.resize(100, 20); Button buttonProjected = new Button("Projected"); Button r = new Button("Redo"); r.resize(100, 20); hbox.getChildren().add(buttonProjected); return; } private void setUpLabels() { labels = new Label[3][3]; GridPane grid = new GridPane(); 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); } } 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"); } } } }); } }