diff --git a/src/main/java/edu/sdccd/cisc190/Game.java b/src/main/java/edu/sdccd/cisc190/Game.java index 166f3d4..b863bc5 100644 --- a/src/main/java/edu/sdccd/cisc190/Game.java +++ b/src/main/java/edu/sdccd/cisc190/Game.java @@ -9,7 +9,6 @@ *

*/ public abstract class Game { - /** * The name of the game. */ @@ -20,13 +19,11 @@ public abstract class Game { private boolean isRunning; - /** * Constructor that initializes the game with a specified name. * * @param gameName the name of the game. */ - public Game(String gameName) { this.gameName = gameName; this.isRunning = false; @@ -53,4 +50,4 @@ public void endGame() { public boolean isRunning() { return isRunning; } -} +} \ No newline at end of file diff --git a/src/main/java/edu/sdccd/cisc190/Main.java b/src/main/java/edu/sdccd/cisc190/Main.java index a4aa6d7..9fba7f6 100644 --- a/src/main/java/edu/sdccd/cisc190/Main.java +++ b/src/main/java/edu/sdccd/cisc190/Main.java @@ -6,7 +6,8 @@ import javafx.scene.control.*; import javafx.scene.layout.VBox; import javafx.stage.Stage; - +import java.util.Collections; //add import statement for shuffling +import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -22,10 +23,15 @@ public class Main extends Application { private QuizGame quizGame; private Label questionLabel; private Label timerLabel; + private Label scoreLabel; private VBox optionsBox; private int currentQuestionIndex = 0; - private int timeLeft = 300; // Timer duration in seconds (5 minutes) + private static final int TIMER_DURATION = 300; + private int timeLeft = TIMER_DURATION; // Timer duration in seconds (5 minutes) + private int score = 0; //score tracking private ScheduledExecutorService timerExecutor; + private boolean isPaused = false; // Track if the game is paused + private Button pauseButton; // Button to toggle pause/resume /** * The main method that launches the JavaFX application. @@ -45,7 +51,6 @@ public static void main(String[] args) { public void start(Stage primaryStage) { Label banner = new Label("Welcome to the Quiz Game!"); banner.setStyle("-fx-font-size: 24px; -fx-font-weight: bold;"); - Button startButton = new Button("Start Game"); startButton.setStyle("-fx-font-size: 18px;"); startButton.setOnAction(e -> showGameScreen(primaryStage)); @@ -53,12 +58,11 @@ public void start(Stage primaryStage) { VBox welcomeLayout = new VBox(20, banner, startButton); welcomeLayout.setPrefSize(400, 300); welcomeLayout.setStyle("-fx-alignment: center;"); - primaryStage.setTitle("Quiz Game"); primaryStage.setScene(new Scene(welcomeLayout)); primaryStage.show(); } - +//TODO: You could possibly add somewhere in here sound effects for getting a right/wrong answer. /** * Transitions to the game screen, initializes the quiz game, and loads questions. * @@ -71,9 +75,13 @@ private void showGameScreen(Stage primaryStage) { quizGame.loadQuestions("src/main/resources/questions.txt"); } catch (Exception e) { showError("Error loading questions: " + e.getMessage()); + e.printStackTrace(); return; } + //Randomize the order of questions + Collections.shuffle(quizGame.getQuestions()); //shuffle the questions + questionLabel = new Label(); questionLabel.setWrapText(true); questionLabel.setStyle("-fx-font-size: 24px; -fx-font-weight: bold; -fx-text-alignment: center;"); @@ -81,10 +89,18 @@ private void showGameScreen(Stage primaryStage) { timerLabel = new Label("Time left: " + timeLeft + " seconds"); timerLabel.setStyle("-fx-font-size: 16px;"); + scoreLabel = new Label("Score: " + score); // intialize score label + scoreLabel.setStyle("-fx-font-size: 16px; -fx-font-weight: bold;"); + optionsBox = new VBox(15); - VBox gameLayout = new VBox(30, questionLabel, optionsBox, timerLabel); - gameLayout.setPrefSize(600, 400); + // Create Pause Button + pauseButton = new Button("Pause Timer"); + pauseButton.setStyle("-fx-font-size: 16px;"); + pauseButton.setOnAction(e -> togglePause()); // Set action for pause/resume + + VBox gameLayout = new VBox(30, questionLabel, optionsBox, scoreLabel, timerLabel, pauseButton); + gameLayout.setPrefSize(800, 800); //adjusted for larger window size gameLayout.setStyle("-fx-padding: 30; -fx-alignment: top-center; -fx-spacing: 20;"); primaryStage.setScene(new Scene(gameLayout)); @@ -101,7 +117,6 @@ public void startGame() { showQuestion(); startTimer(); } - /** * Displays the current question and its answer options. *

@@ -134,7 +149,6 @@ private void showQuestion() { optionsBox.getChildren().addAll(trueButton, falseButton); } } - /** * Handles the user's answer selection. *

@@ -147,6 +161,8 @@ private void handleAnswer(String answer) { Question question = quizGame.getQuestions().get(currentQuestionIndex); if (question.checkAnswer(answer)) { quizGame.incrementScore(); + score++; // + scoreLabel.setText("Score: " + score); // update score label after each correct answer } currentQuestionIndex++; showQuestion(); @@ -161,7 +177,7 @@ private void handleAnswer(String answer) { public void startTimer() { timerExecutor = Executors.newSingleThreadScheduledExecutor(); timerExecutor.scheduleAtFixedRate(() -> { - if (timeLeft > 0) { + if (timeLeft > 0 && !isPaused) { //check if game is not paused timeLeft--; Platform.runLater(() -> timerLabel.setText("Time left: " + timeLeft + " seconds")); } else { @@ -171,6 +187,18 @@ public void startTimer() { }, 0, 1, TimeUnit.SECONDS); } + /** Pauses/resumes timer + * + */ + private void togglePause() { + isPaused = !isPaused; // Toggle the pause + if (isPaused) { + pauseButton.setText("Resume Timer"); //Change button text when paused + } else { + pauseButton.setText("Pause Timer"); //Change button text when resumed + } + } + /** * Ends the quiz game and displays the final score. *

@@ -180,7 +208,7 @@ public void startTimer() { public void endGame() { quizGame.endGame(); timerExecutor.shutdown(); - Alert alert = new Alert(Alert.AlertType.INFORMATION, "Game Over! Your Score: " + quizGame.getScore()); + Alert alert = new Alert(Alert.AlertType.INFORMATION,"Game Over! Your Score: " + quizGame.getScore()); alert.showAndWait(); try { @@ -201,4 +229,4 @@ private void showError(String message) { Alert alert = new Alert(Alert.AlertType.ERROR, message); alert.showAndWait(); } -} +} \ No newline at end of file diff --git a/src/main/java/edu/sdccd/cisc190/MultipleChoiceQuestion.java b/src/main/java/edu/sdccd/cisc190/MultipleChoiceQuestion.java index 75f32fd..647a7ef 100644 --- a/src/main/java/edu/sdccd/cisc190/MultipleChoiceQuestion.java +++ b/src/main/java/edu/sdccd/cisc190/MultipleChoiceQuestion.java @@ -57,4 +57,5 @@ public boolean checkAnswer(String answer) { } } +//TODO: You could possibly add questions that have more than 4 multiple choices diff --git a/src/main/java/edu/sdccd/cisc190/Question.java b/src/main/java/edu/sdccd/cisc190/Question.java index 1c178dd..ffba382 100644 --- a/src/main/java/edu/sdccd/cisc190/Question.java +++ b/src/main/java/edu/sdccd/cisc190/Question.java @@ -43,3 +43,4 @@ public String getQuestionText() { */ public abstract boolean checkAnswer(String answer); } +//TODO: a possible implementation that you could add is changing and varying the difficulty levels for the questions such as easy, medium, or hard. \ No newline at end of file diff --git a/src/main/java/edu/sdccd/cisc190/QuizGame.java b/src/main/java/edu/sdccd/cisc190/QuizGame.java index e8c3fbe..5e7e2e1 100644 --- a/src/main/java/edu/sdccd/cisc190/QuizGame.java +++ b/src/main/java/edu/sdccd/cisc190/QuizGame.java @@ -54,11 +54,16 @@ public void loadQuestions(String filePath) throws IOException { while ((line = reader.readLine()) != null) { String[] parts = line.split(";"); - if (parts[0].equalsIgnoreCase("MC")) { - questions.add(new MultipleChoiceQuestion(parts[1], parts[2], parts[3].split(","))); - } else if (parts[0].equalsIgnoreCase("TF")) { - questions.add(new TrueFalseQuestion(parts[1], parts[2])); - } + switch (parts[0].toUpperCase()) { + case "MC": + questions.add(new MultipleChoiceQuestion(parts[1], parts[2], parts[3].split(","))); + break; + case "TF": + questions.add(new TrueFalseQuestion(parts[1], parts[2])); + break; + default: + System.out.println("Unknown question type: " + parts[0]); + } // Replaced if-else with a switch which improves readability and scalable for handling multiple cases. For example, if a new case is introduced such as short answers } reader.close(); } @@ -104,4 +109,4 @@ public int getScore() { public void incrementScore() { score++; } -} +} \ No newline at end of file diff --git a/src/main/java/edu/sdccd/cisc190/TrueFalseQuestion.java b/src/main/java/edu/sdccd/cisc190/TrueFalseQuestion.java index 4774d36..37a6857 100644 --- a/src/main/java/edu/sdccd/cisc190/TrueFalseQuestion.java +++ b/src/main/java/edu/sdccd/cisc190/TrueFalseQuestion.java @@ -38,4 +38,4 @@ public TrueFalseQuestion(String questionText, String correctAnswer) { public boolean checkAnswer(String answer) { return correctAnswer.equalsIgnoreCase(answer); } -} +} \ No newline at end of file