Created
August 24, 2021 10:10
-
-
Save Da9el00/ec257feb0269d1557fcc08ea74166858 to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Tic-Tac-toe Game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.Button; | |
import javafx.scene.text.Text; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
@FXML | |
private Button button1; | |
@FXML | |
private Button button2; | |
@FXML | |
private Button button3; | |
@FXML | |
private Button button4; | |
@FXML | |
private Button button5; | |
@FXML | |
private Button button6; | |
@FXML | |
private Button button7; | |
@FXML | |
private Button button8; | |
@FXML | |
private Button button9; | |
@FXML | |
private Text winnerText; | |
private int playerTurn = 0; | |
ArrayList<Button> buttons; | |
@Override | |
public void initialize(URL url, ResourceBundle resourceBundle) { | |
buttons = new ArrayList<>(Arrays.asList(button1,button2,button3,button4,button5,button6,button7,button8,button9)); | |
buttons.forEach(button ->{ | |
setupButton(button); | |
button.setFocusTraversable(false); | |
}); | |
} | |
@FXML | |
void restartGame(ActionEvent event) { | |
buttons.forEach(this::resetButton); | |
winnerText.setText("Tic-Tac-Toe"); | |
} | |
public void resetButton(Button button){ | |
button.setDisable(false); | |
button.setText(""); | |
} | |
private void setupButton(Button button) { | |
button.setOnMouseClicked(mouseEvent -> { | |
setPlayerSymbol(button); | |
button.setDisable(true); | |
checkIfGameIsOver(); | |
}); | |
} | |
public void setPlayerSymbol(Button button){ | |
if(playerTurn % 2 == 0){ | |
button.setText("X"); | |
playerTurn = 1; | |
} else{ | |
button.setText("O"); | |
playerTurn = 0; | |
} | |
} | |
public void checkIfGameIsOver(){ | |
for (int a = 0; a < 8; a++) { | |
String line = switch (a) { | |
case 0 -> button1.getText() + button2.getText() + button3.getText(); | |
case 1 -> button4.getText() + button5.getText() + button6.getText(); | |
case 2 -> button7.getText() + button8.getText() + button9.getText(); | |
case 3 -> button1.getText() + button5.getText() + button9.getText(); | |
case 4 -> button3.getText() + button5.getText() + button7.getText(); | |
case 5 -> button1.getText() + button4.getText() + button7.getText(); | |
case 6 -> button2.getText() + button5.getText() + button8.getText(); | |
case 7 -> button3.getText() + button6.getText() + button9.getText(); | |
default -> null; | |
}; | |
//X winner | |
if (line.equals("XXX")) { | |
winnerText.setText("X won!"); | |
} | |
//O winner | |
else if (line.equals("OOO")) { | |
winnerText.setText("O won!"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("Hello World"); | |
primaryStage.setScene(new Scene(root)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<?import javafx.scene.layout.FlowPane?> | |
<?import javafx.scene.text.Font?> | |
<?import javafx.scene.text.Text?> | |
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
<children> | |
<FlowPane layoutX="188.0" layoutY="115.0" prefHeight="225.0" prefWidth="225.0"> | |
<children> | |
<Button fx:id="button1" mnemonicParsing="false" prefHeight="75.0" prefWidth="75.0" /> | |
<Button fx:id="button2" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" prefHeight="75.0" prefWidth="75.0" /> | |
<Button fx:id="button3" layoutX="85.0" layoutY="10.0" mnemonicParsing="false" prefHeight="75.0" prefWidth="75.0" /> | |
<Button fx:id="button4" layoutX="160.0" layoutY="10.0" mnemonicParsing="false" prefHeight="75.0" prefWidth="75.0" /> | |
<Button fx:id="button5" layoutX="10.0" layoutY="85.0" mnemonicParsing="false" prefHeight="75.0" prefWidth="75.0" /> | |
<Button fx:id="button6" layoutX="85.0" layoutY="85.0" mnemonicParsing="false" prefHeight="75.0" prefWidth="75.0" /> | |
<Button fx:id="button7" layoutX="160.0" layoutY="85.0" mnemonicParsing="false" prefHeight="75.0" prefWidth="75.0" /> | |
<Button fx:id="button8" layoutX="10.0" layoutY="160.0" mnemonicParsing="false" prefHeight="75.0" prefWidth="75.0" /> | |
<Button fx:id="button9" layoutX="85.0" layoutY="160.0" mnemonicParsing="false" prefHeight="75.0" prefWidth="75.0" /> | |
</children> | |
</FlowPane> | |
<Text fx:id="winnerText" layoutX="182.0" layoutY="72.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Tic-Tac-Toe" textAlignment="CENTER" wrappingWidth="237.9443359375"> | |
<font> | |
<Font size="39.0" /> | |
</font> | |
</Text> | |
<Button layoutX="274.0" layoutY="361.0" mnemonicParsing="false" onAction="#restartGame" text="Restart" /> | |
</children> | |
</AnchorPane> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment