Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 91 additions & 10 deletions src/main/java/co/ppg2/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import co.ppg2.views.GameView;
import co.ppg2.views.PlayerPopup;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import java.util.ArrayList;

Expand All @@ -27,10 +28,23 @@ public class Main extends Application {
*
* @param args the command-line arguments (unused).
*/

//The PlayerDataController.loadPlayers() method could fail due to issues like a corrupted file or missing data.
// Added exception handling to manage these scenarios gracefully.
public static void main(String[] args) {
players = PlayerDataController.loadPlayers(); // Load players on start
launch(args);
}
try {
players = PlayerDataController.loadPlayers(); // Load players on start
if (players == null) {
players = new ArrayList<>();
}
} catch(Exception e) {
System.err.println("Error loading players: " + e.getMessage());
players = new ArrayList<>(); // Initialize an empty list if loading fails
}
launch(args);
}



/**
* Starts the application by displaying the player setup and initializing the game.
Expand All @@ -40,6 +54,7 @@ public static void main(String[] args) {
* @param primaryStage the main stage for the application.
*/
@Override

public void start(Stage primaryStage) {
// Prompt for Player X's username and Player O's username
Player playerX = PlayerPopup.showPopup("Player X");
Expand Down Expand Up @@ -71,15 +86,81 @@ public void start(Stage primaryStage) {
* @param username the username of the player to find or add.
* @return the found or newly created Player.
*/

//Added try-catch blocks with meaningful error messages to handle potential issues.
private Player findOrAddPlayer(String username) {
for (Player player : players) {
if (player.getUsername().equals(username)) {
return player;
try {
if (username == null || username.trim().isEmpty()) {
throw new IllegalArgumentException("Username cannot be null or empty.");
}

for (Player player : players) {
if (player.getUsername().equals(username)) {
return player;
}
}

Player newPlayer = new Player(username);
players.add(newPlayer);

try {
PlayerDataController.savePlayers(players);
} catch (Exception e) {
System.err.println("Error saving players: " + e.getMessage());
}

return newPlayer;
} catch (Exception e) {
System.err.println("Error finding or adding player: " + e.getMessage());
return new Player("Unknown"); // Return a fallback player to avoid crashes
}
Player newPlayer = new Player(username);
players.add(newPlayer);
PlayerDataController.savePlayers(players);
return newPlayer;
}

//This approach ensures your application exits cleanly in the event of an error.
// GracefulExitHandler can be used to handle exceptions effectively by centralizing the error handling and application exit logic.
public class GracefulExitHandler {
/**
* Handles critical errors by logging the error, notifying the user,
* and exiting the application gracefully.
*
*/
public static void handleCriticalError(String message, Throwable throwable) {
// Log the error (console or logging framework)
System.err.println("Critical error: " + message);
if (throwable != null) {
throwable.printStackTrace(); // Log stack trace for debugging
}

// Notify the user (optional, if the UI is still active)
Platform.runLater(() -> {
// Replace this with your UI framework's error dialog or message
javafx.scene.control.Alert alert = new javafx.scene.control.Alert(
javafx.scene.control.Alert.AlertType.ERROR
);
alert.setTitle("Critical Error");
alert.setHeaderText("An unexpected error occurred.");
alert.setContentText(message);
alert.showAndWait();

// Exit the application gracefully
exitApplication();
});
}

/**
* Cleans up resources and exits the application with a non-zero code.
*/
private static void exitApplication() {
// Perform any necessary cleanup here (close files, stop threads, etc.)
System.err.println("Cleaning up resources before exiting...");

// Ensure the JavaFX application exits properly
Platform.exit();

// Exit the JVM with a non-zero code to indicate an error
System.exit(1);
}
}


}
2 changes: 0 additions & 2 deletions src/main/java/co/ppg2/views/LabelWin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Extends {@link LabelBase} and overrides its default styling to indicate a win.
*/
public class LabelWin extends LabelBase {
// Message to be displayed when a player wins
public String winMessage;

/**
* Constructs a LabelWin instance with the specified text.
Expand Down