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
5 changes: 1 addition & 4 deletions src/main/java/edu/sdccd/cisc190/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* </p>
*/
public abstract class Game {

/**
* The name of the game.
*/
Expand All @@ -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;
Expand All @@ -53,4 +50,4 @@ public void endGame() {
public boolean isRunning() {
return isRunning;
}
}
}
52 changes: 40 additions & 12 deletions src/main/java/edu/sdccd/cisc190/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -45,20 +51,18 @@ 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));

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.
*
Expand All @@ -71,20 +75,32 @@ 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;");

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));
Expand All @@ -101,7 +117,6 @@ public void startGame() {
showQuestion();
startTimer();
}

/**
* Displays the current question and its answer options.
* <p>
Expand Down Expand Up @@ -134,7 +149,6 @@ private void showQuestion() {
optionsBox.getChildren().addAll(trueButton, falseButton);
}
}

/**
* Handles the user's answer selection.
* <p>
Expand All @@ -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();
Expand All @@ -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 {
Expand All @@ -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.
* <p>
Expand All @@ -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 {
Expand All @@ -201,4 +229,4 @@ private void showError(String message) {
Alert alert = new Alert(Alert.AlertType.ERROR, message);
alert.showAndWait();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ public boolean checkAnswer(String answer) {
}
}

//TODO: You could possibly add questions that have more than 4 multiple choices

1 change: 1 addition & 0 deletions src/main/java/edu/sdccd/cisc190/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
17 changes: 11 additions & 6 deletions src/main/java/edu/sdccd/cisc190/QuizGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -104,4 +109,4 @@ public int getScore() {
public void incrementScore() {
score++;
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/edu/sdccd/cisc190/TrueFalseQuestion.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ public TrueFalseQuestion(String questionText, String correctAnswer) {
public boolean checkAnswer(String answer) {
return correctAnswer.equalsIgnoreCase(answer);
}
}
}