From 5f779cb0276c50d1db279079a335823a7179b558 Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Sat, 29 Nov 2025 16:07:26 -0800 Subject: [PATCH] fix formatting + launching JavaFX application launching --- data/score.txt | 21 +++ .../comp2522/project/AscendingPlacement.java | 18 ++- .../ca/bcit/comp2522/project/Country.java | 4 +- .../ca/bcit/comp2522/project/GameBoard.java | 3 + src/code/ca/bcit/comp2522/project/Main.java | 132 +++++++++++++----- src/code/ca/bcit/comp2522/project/Mines.java | 12 -- .../ca/bcit/comp2522/project/MinesScore.java | 18 +-- .../ca/bcit/comp2522/project/MinesUI.java | 28 +++- .../ca/bcit/comp2522/project/NumberGame.java | 36 ++--- .../project/RandomNumberGenerator.java | 4 +- src/code/ca/bcit/comp2522/project/Score.java | 6 +- .../ca/bcit/comp2522/project/WordGame.java | 18 ++- src/code/ca/bcit/comp2522/project/World.java | 2 +- 13 files changed, 209 insertions(+), 93 deletions(-) diff --git a/data/score.txt b/data/score.txt index 7e81d2d..796faf0 100644 --- a/data/score.txt +++ b/data/score.txt @@ -19,3 +19,24 @@ Correct Second Attempts: 1 Incorrect Attempts: 8 Total Score: 3 +Date and Time: 2025-11-29 15:05:40 +Games Played: 1 +Correct First Attempts: 0 +Correct Second Attempts: 0 +Incorrect Attempts: 10 +Score: 0 points + +Date and Time: 2025-11-29 15:09:31 +Games Played: 1 +Correct First Attempts: 0 +Correct Second Attempts: 0 +Incorrect Attempts: 10 +Score: 0 points + +Date and Time: 2025-11-29 15:29:31 +Games Played: 1 +Correct First Attempts: 0 +Correct Second Attempts: 0 +Incorrect Attempts: 10 +Score: 0 points + diff --git a/src/code/ca/bcit/comp2522/project/AscendingPlacement.java b/src/code/ca/bcit/comp2522/project/AscendingPlacement.java index 7a2f228..23b9409 100644 --- a/src/code/ca/bcit/comp2522/project/AscendingPlacement.java +++ b/src/code/ca/bcit/comp2522/project/AscendingPlacement.java @@ -4,6 +4,9 @@ package ca.bcit.comp2522.project; * AscendingPlacement contains the logical and * validation of placing an integer in an array * of integers following an ascending order. + * + * @author Braeden Sowinski + * @version 1.0.0 */ public class AscendingPlacement extends PlacementRule @@ -22,8 +25,11 @@ public class AscendingPlacement * @return if the value is in a valid placement */ @Override - public boolean isValidPlacement(int[] positions, int index, int value) - { + public boolean isValidPlacement( + final int[] positions, + final int index, + final int value + ) { for (int i = 0; i < positions.length; i++) { if (positions[i] == FIRST) @@ -55,8 +61,10 @@ public class AscendingPlacement * without violating the ascending rule */ @Override - public boolean canPlaceNext(int[] positions, int nextValue) - { + public boolean canPlaceNext( + final int[] positions, + final int nextValue + ) { for (int i = 0; i < positions.length; i++) { if (positions[i] != FIRST) @@ -67,7 +75,7 @@ public class AscendingPlacement int left; int right; - left = Integer.MIN_VALUE; + left = Integer.MIN_VALUE; right = Integer.MAX_VALUE; // scan left diff --git a/src/code/ca/bcit/comp2522/project/Country.java b/src/code/ca/bcit/comp2522/project/Country.java index 665d907..75478ef 100644 --- a/src/code/ca/bcit/comp2522/project/Country.java +++ b/src/code/ca/bcit/comp2522/project/Country.java @@ -4,7 +4,7 @@ import java.util.Random; /** * Country class stores a country's - * name, capital city name, and NUMBER_OF_FACTS + * name, capital city name * about the country. * * @author Braeden Sowinski @@ -12,8 +12,6 @@ import java.util.Random; */ public class Country { - private static final int NUMBER_OF_FACTS = 3; - private final String name; private final String capitalCityName; private final String[] facts; diff --git a/src/code/ca/bcit/comp2522/project/GameBoard.java b/src/code/ca/bcit/comp2522/project/GameBoard.java index 4a68666..0f6dfa4 100644 --- a/src/code/ca/bcit/comp2522/project/GameBoard.java +++ b/src/code/ca/bcit/comp2522/project/GameBoard.java @@ -5,6 +5,9 @@ package ca.bcit.comp2522.project; * to represent some two-dimensional board, * it stores the width and height of board * and must have a reset method. + * + * @author Braeden Sowinski + * @version 1.0.0 */ public abstract class GameBoard { diff --git a/src/code/ca/bcit/comp2522/project/Main.java b/src/code/ca/bcit/comp2522/project/Main.java index 3cb2106..bc857bf 100644 --- a/src/code/ca/bcit/comp2522/project/Main.java +++ b/src/code/ca/bcit/comp2522/project/Main.java @@ -1,62 +1,128 @@ package ca.bcit.comp2522.project; +import java.io.File; +import java.io.IOException; import java.util.Scanner; /** - * Main program entry to access a games menu, - * users can select from 3 games. Word Game, - * Number Game, or My Game (Minesweeper) + * Main launcher for the games application. + * Provides a CLI for selecting WordGame, NumberGame, + * or Minesweeper, launching JavaFX games in separate + * JVM processes for lifecycle stability. * * @author Braeden Sowinski - * @version 1.0.0 + * @version 1.0.1 */ public class Main { /** - * main program entry - * @param args from the command line + * Program entry point for the CLI menu. + * + * @param args command-line arguments. Optional: + * --jfxlib=/path/to/javafx-sdk/lib */ public static void main(final String[] args) { + final String javafxLib; + javafxLib = System.getProperty("jdk.module.path"); + + if (javafxLib == null) + { + System.err.println( + "JavaFX library path missing.\n" + + "Provide with:\n" + + " 1) VM option: -Djfxlib=/path/to/javafx/lib\n" + + " 2) Or program arg: --jfxlib=/path/to/javafx/lib" + ); + return; + } + final Scanner scanner; + boolean running; scanner = new Scanner(System.in); + running = true; - boolean run = true; - - do { - final String input; - - System.out.println("Select a game: "); + while (running) + { System.out.println("Word Game (W)"); System.out.println("Number Game (N)"); - System.out.println("My Game (M)"); + System.out.println("Minesweeper (M)"); System.out.println("Quit (Q)"); System.out.print("> "); - input = scanner.nextLine().toUpperCase(); + final String choice; - switch (input) { - case "W": - System.out.println("Word game..."); - break; - case "N": - System.out.println("Number game..."); - break; - case "M": - System.out.println("My game..."); - break; - case "Q": - System.out.println("Quitting..."); - run = false; - break; - default: - System.out.println("Invalid input: " + input); - break; + choice = scanner.nextLine().trim().toUpperCase(); + + switch (choice) + { + case "W" -> runWordGame(scanner); + case "N" -> launchJavaFxApp("ca.bcit.comp2522.project.NumberGame", javafxLib); + case "M" -> launchJavaFxApp("ca.bcit.comp2522.project.MyGame", javafxLib); + case "Q" -> running = false; + default -> System.out.println("Invalid selection."); } + } + } - } while (run); + /** + * Runs the WordGame inside the CLI environment. + * + * @param scanner shared scanner for System.in + */ + private static void runWordGame(final Scanner scanner) + { + try + { + final WordGame game; + game = new WordGame(); + game.runTrivia(scanner); + } + catch (final IOException e) + { + System.err.println("Unable to start WordGame."); + } + } - scanner.close(); + /** + * Launches a JavaFX Application subclass in a new + * JVM process. + * + * @param mainClass fully-qualified class name + * @param javafxLib path to JavaFX lib directory + */ + private static void launchJavaFxApp( + final String mainClass, + final String javafxLib + ) { + final String javaBin; + final String cp; + final ProcessBuilder pb; + + javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; + cp = System.getProperty("java.class.path"); + + pb = new ProcessBuilder( + javaBin, + "--module-path", javafxLib, + "--add-modules", "javafx.controls,javafx.fxml", + "--enable-native-access=javafx.graphics", + "-cp", cp, + mainClass + ); + + pb.inheritIO(); + + try + { + final Process p; + p = pb.start(); + p.waitFor(); + } + catch (final IOException | InterruptedException e) + { + Thread.currentThread().interrupt(); + } } } diff --git a/src/code/ca/bcit/comp2522/project/Mines.java b/src/code/ca/bcit/comp2522/project/Mines.java index 9ec57c9..e59974b 100644 --- a/src/code/ca/bcit/comp2522/project/Mines.java +++ b/src/code/ca/bcit/comp2522/project/Mines.java @@ -1,14 +1,6 @@ package ca.bcit.comp2522.project; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.time.format.DateTimeFormatter; import java.util.Random; -import java.util.Scanner; import java.util.function.Consumer; /** @@ -36,10 +28,6 @@ public class Mines extends GameBoard public static final int FLAG = 1; public static final int FLAG_QUESTION = 2; - private static final int INITIAL_BEST_SCORE = Integer.MAX_VALUE; - - private static final String SCORE_FILE_NAME = "minesweeper-score.txt"; - private static final Random RANDOM_GENERATOR; static diff --git a/src/code/ca/bcit/comp2522/project/MinesScore.java b/src/code/ca/bcit/comp2522/project/MinesScore.java index ebc9fee..308bf3d 100644 --- a/src/code/ca/bcit/comp2522/project/MinesScore.java +++ b/src/code/ca/bcit/comp2522/project/MinesScore.java @@ -58,8 +58,8 @@ public class MinesScore * @param scoreFilePath filepath to append Score to */ public static void appendScoreToFile( - final MinesScore score, - final String scoreFilePath + final MinesScore score, + final String scoreFilePath ) { try { @@ -165,8 +165,8 @@ public class MinesScore * @return if score is a new high-score in list */ public static boolean isHighScore( - final MinesScore score, - final List scores + final MinesScore score, + final List scores ) { final MinesScore highScore; @@ -191,11 +191,13 @@ public class MinesScore * @param randomMode was on or off */ public MinesScore( - final LocalDateTime dateTime, - final int seconds, - final String difficulty, - final boolean randomMode + final LocalDateTime dateTime, + final int seconds, + final String difficulty, + final boolean randomMode ) { + validateDifficulty(difficulty); + final DateTimeFormatter formatter; formatter = DateTimeFormatter.ofPattern(DATE_PATTERN); diff --git a/src/code/ca/bcit/comp2522/project/MinesUI.java b/src/code/ca/bcit/comp2522/project/MinesUI.java index dbfc5cb..cf91869 100644 --- a/src/code/ca/bcit/comp2522/project/MinesUI.java +++ b/src/code/ca/bcit/comp2522/project/MinesUI.java @@ -130,6 +130,10 @@ public class MinesUI this.timerRunning = false; } + /* + * showInfo displays an info popup + * about the random mode + */ private void showInfo(final Window window) { final Stage popup; @@ -172,6 +176,12 @@ public class MinesUI * game mode, and field size. * @param primaryStage to display main menu to */ + /** + * showMainMenu displays main menu to choose + * game mode and field size. + * + * @param primaryStage to display main menu on + */ public void showMainMenu(final Stage primaryStage) { final Label titleLabel; @@ -181,6 +191,7 @@ public class MinesUI final Label modeLabel; final Button toggleModeButton; final Button modeInfoButton; + final Button quitButton; final HBox modeButtons; final VBox root; final Scene scene; @@ -202,14 +213,19 @@ public class MinesUI modeLabel = new Label("Random Mode: OFF"); toggleModeButton = new Button("Toggle Random Mode"); modeInfoButton = new Button("?"); - modeButtons = new HBox(MENU_PADDING, toggleModeButton, modeInfoButton); + quitButton = new Button("Quit"); + + modeButtons = new HBox(MENU_PADDING, toggleModeButton, modeInfoButton); toggleModeButton.setOnAction(e -> { this.randomMode = !this.randomMode; modeLabel.setText("Random Mode: " + (this.randomMode ? "ON" : "OFF")); }); - modeInfoButton.setOnAction(e -> showInfo(primaryStage.getOwner())); + modeInfoButton.setOnAction(e -> showInfo(primaryStage)); + + quitButton.setFont(MENU_FONT); + quitButton.setOnAction(e -> primaryStage.close()); easyButton.setOnAction(e -> startGame( EASY_WIDTH, @@ -250,7 +266,8 @@ public class MinesUI mediumButton, hardButton, modeLabel, - modeButtons + modeButtons, + quitButton ); scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT); @@ -739,4 +756,9 @@ public class MinesUI button.setMouseTransparent(true); button.setFocusTraversable(false); } + + public Stage getGameStage() + { + return this.gameStage; + } } diff --git a/src/code/ca/bcit/comp2522/project/NumberGame.java b/src/code/ca/bcit/comp2522/project/NumberGame.java index db9678a..1edba57 100644 --- a/src/code/ca/bcit/comp2522/project/NumberGame.java +++ b/src/code/ca/bcit/comp2522/project/NumberGame.java @@ -1,6 +1,7 @@ package ca.bcit.comp2522.project; import javafx.application.Application; +import javafx.application.Platform; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.layout.GridPane; @@ -51,16 +52,16 @@ public class NumberGame private static final Font FONT = Font.font("Arial", FontWeight.BOLD, FONT_SIZE); private static final Font MENU_FONT = Font.font("Arial", FontWeight.NORMAL, MENU_FONT_SIZE); - private int gamesPlayed; - private int gamesWon; - private int allTimePlaced; - private int numbersPlaced; - private Label numberLabel; - private int currentNumber; - private int[] positions; - private List