Skip to content

Commit

Permalink
Code refactored lil bit
Browse files Browse the repository at this point in the history
  • Loading branch information
akgarg0472 committed Mar 8, 2023
1 parent df9ec46 commit f0b18f6
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 285 deletions.
28 changes: 16 additions & 12 deletions src/TicTacToeJavaFX/GameBoard.fxml
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:controller="TicTacToeJavaFX.GameBoardController"
xmlns:fx="http://javafx.com/fxml">
<BorderPane xmlns:fx="http://javafx.com/fxml"
fx:controller="TicTacToeJavaFX.GameBoardController">
<center>
<VBox>
<HBox>
<Button fx:id="btn1"></Button>
<Button fx:id="btn2"></Button>
<Button fx:id="btn3"></Button>
<Button fx:id="btn1"/>
<Button fx:id="btn2"/>
<Button fx:id="btn3"/>
</HBox>

<HBox>
<Button fx:id="btn4"></Button>
<Button fx:id="btn5"></Button>
<Button fx:id="btn6"></Button>
<Button fx:id="btn4"/>
<Button fx:id="btn5"/>
<Button fx:id="btn6"/>
</HBox>

<HBox>
<Button fx:id="btn7"></Button>
<Button fx:id="btn8"></Button>
<Button fx:id="btn9"></Button>
<Button fx:id="btn7"/>
<Button fx:id="btn8"/>
<Button fx:id="btn9"/>
</HBox>
</VBox>
</center>

<bottom>
<Label fx:id="bottomCopyrightLabel" text="©Akhilesh Garg" BorderPane.alignment="BOTTOM_CENTER"></Label>
<Label fx:id="bottomCopyrightLabel" text="©Akhilesh Garg" BorderPane.alignment="BOTTOM_CENTER"/>
</bottom>

</BorderPane>
327 changes: 104 additions & 223 deletions src/TicTacToeJavaFX/GameBoardController.java

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/TicTacToeJavaFX/GameResult.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<BorderPane fx:id="myid" xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:id="myid"
xmlns="http://javafx.com/javafx"
fx:controller="TicTacToeJavaFX.GameResultController"
prefHeight="400.0" prefWidth="600.0">

Expand Down
49 changes: 27 additions & 22 deletions src/TicTacToeJavaFX/GameResultController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import javafx.scene.text.FontWeight;

import java.io.IOException;
import java.net.URL;
import java.util.Objects;

@SuppressWarnings("UnusedDeclaration")
public class GameResultController {

@FXML
Expand All @@ -27,8 +30,30 @@ public class GameResultController {
@FXML
private Button exitGameButton;

public void initialize() {
this.playAgainButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
try {
final URL welcomeScreenResource = getClass().getResource("WelcomeScreen.fxml");
Objects.requireNonNull(welcomeScreenResource, "Error loading game welcome screen");

Main.getPrimaryStage().setScene(new Scene(
FXMLLoader.load(welcomeScreenResource),
Main.STAGE_DEFAULT_WIDTH,
Main.STAGE_DEFAULT_HEIGHT
));
} catch (IOException e) {
e.printStackTrace();
}
}
});

exitGameButton.setOnMouseClicked(event -> Main.getPrimaryStage().close());
}

// this method dynamically update the content of the final result scene of the game according to the game result
void setGameResultText(String gameResultText1, String winner) {
void setGameResultText(final String gameResultText1, final String winner) {
if (gameResultText1.equals("Draw")) {
gameOverLabel.setFont(Font.font("Algerian", FontWeight.BOLD, 45));
congratsLabel.setFont(new Font(35));
Expand All @@ -53,24 +78,4 @@ void setGameResultText(String gameResultText1, String winner) {
}
}

public void initialize() {
playAgainButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
try {
Main.getPrimaryStage().setScene(new Scene
(FXMLLoader.load(getClass().getResource("WelcomeScreen.fxml")), Main.STAGE_DEFAULT_WIDTH, Main.STAGE_DEFAULT_HEIGHT));
} catch (IOException exception) {
exception.printStackTrace();
}
}
});

exitGameButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Main.getPrimaryStage().close();
}
});
}
}
}
14 changes: 10 additions & 4 deletions src/TicTacToeJavaFX/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.net.URL;
import java.util.Objects;

public class Main extends Application {

static final int STAGE_DEFAULT_WIDTH = 300;
static final int STAGE_DEFAULT_HEIGHT = 300;
static final double STAGE_DEFAULT_WIDTH = 350;
static final double STAGE_DEFAULT_HEIGHT = 350;
static final int BUTTON_DEFAULT_FONT_SIZE = 40;

private static Stage stage;
Expand All @@ -26,11 +29,14 @@ public static void main(String[] args) {
@Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("WelcomeScreen.fxml"));
final URL mainResource = getClass().getResource("WelcomeScreen.fxml");
Objects.requireNonNull(mainResource, "Error in initializing game board. System error occurred.");
final Parent screen = FXMLLoader.load(mainResource);
primaryStage.setTitle("Tic Tac Toe");
Scene scene = new Scene(root, STAGE_DEFAULT_WIDTH, STAGE_DEFAULT_HEIGHT);
final Scene scene = new Scene(screen, STAGE_DEFAULT_WIDTH, STAGE_DEFAULT_HEIGHT);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}

}
17 changes: 17 additions & 0 deletions src/TicTacToeJavaFX/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package TicTacToeJavaFX;

/**
* @author Akhilesh Garg
* @since 08-03-2023
*/
final class Utils {

private Utils() {
throw new UnsupportedOperationException();
}

static boolean isStringValid(final String str) {
return str != null && !str.trim().isEmpty();
}

}
4 changes: 2 additions & 2 deletions src/TicTacToeJavaFX/WelcomeScreen.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
<GridPane xmlns:fx="http://javafx.com/fxml"
xmlns="http://javafx.com/javafx"
fx:controller="TicTacToeJavaFX.WelcomeScreenController"
prefHeight="400.0" prefWidth="600.0">

Expand Down
39 changes: 26 additions & 13 deletions src/TicTacToeJavaFX/WelcomeScreenController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,45 @@
import javafx.scene.text.Font;

import java.io.IOException;
import java.net.URL;
import java.util.Objects;

@SuppressWarnings("UnusedDeclaration")
public class WelcomeScreenController {

private static final String PLAYER_ONE_DEFAULT_NAME = "Player 1";
private static final String PLAYER_TWO_DEFAULT_NAME = "Player 2";
private static final Font font = new Font(14);

private static String playerOneName;
private static String playerTwoName;
private static String playerOneSymbol;
private static String playerTwoSymbol;
private static String playerOneColor;
private static String playerTwoColor;

private ToggleGroup toggleGroup;
@FXML
private Label playerOneNameLabel;
@FXML
private TextField playerOneNameField;

@FXML
private Label playerTwoNameLabel;
@FXML
private TextField playerTwoNameField;

@FXML
private Label playerOneSymbolLabel;
@FXML
private RadioButton crossSymbol;
@FXML
private RadioButton zeroSymbol;

@FXML
private Label playerOneColorLabel;
@FXML
private ColorPicker playerOneColorPicker;

@FXML
private Label playerTwoColorLabel;
@FXML
private ColorPicker playerTwoColorPicker;

@FXML
private Button startGameButton;

Expand Down Expand Up @@ -78,10 +78,13 @@ static String getPlayerTwoColor() {
}

public void initialize() {
toggleGroup = new ToggleGroup();
final ToggleGroup toggleGroup = new ToggleGroup();
crossSymbol.setToggleGroup(toggleGroup);
zeroSymbol.setToggleGroup(toggleGroup);

playerOneName = PLAYER_ONE_DEFAULT_NAME;
playerTwoName = PLAYER_TWO_DEFAULT_NAME;

playerOneNameLabel.setFont(font);
playerTwoNameLabel.setFont(font);
playerOneSymbolLabel.setFont(font);
Expand All @@ -91,11 +94,18 @@ public void initialize() {
playerOneColorPicker.setValue(Color.BLACK);
playerTwoColorPicker.setValue(Color.BLACK);

startGameButton.setOnAction(new EventHandler<ActionEvent>() {
this.startGameButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
playerOneName = playerOneNameField.getText();
playerTwoName = playerTwoNameField.getText();
final String _playerOneName = playerOneNameField.getText();
if (Utils.isStringValid(_playerOneName)) {
playerOneName = _playerOneName;
}

final String _playerTwoName = playerTwoNameField.getText();
if (Utils.isStringValid(_playerTwoName)) {
playerTwoName = _playerTwoName;
}

if (crossSymbol.isSelected()) {
playerOneSymbol = "X";
Expand All @@ -109,13 +119,16 @@ public void handle(ActionEvent actionEvent) {
playerTwoColor = playerTwoColorPicker.getValue().toString();

try {
Parent root = FXMLLoader.load(getClass().getResource("GameBoard.fxml"));
Scene scene = new Scene(root, Main.STAGE_DEFAULT_WIDTH, Main.STAGE_DEFAULT_HEIGHT);
final URL gameBoardResource = getClass().getResource("GameBoard.fxml");
Objects.requireNonNull(gameBoardResource, "Error loading game board screen");
final Parent root = FXMLLoader.load(gameBoardResource);
final Scene scene = new Scene(root, Main.STAGE_DEFAULT_WIDTH, Main.STAGE_DEFAULT_HEIGHT);
Main.getPrimaryStage().setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}

}
13 changes: 6 additions & 7 deletions src/module-info.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module SampleJavaFX {

requires javafx.fxml;
requires javafx.controls;

opens TicTacToeJavaFX;
}
//module SampleJavaFX {
// requires jfxrt;
// requires rt;
//
// opens TicTacToeJavaFX;
//}

0 comments on commit f0b18f6

Please sign in to comment.