handle game results

This commit is contained in:
SowinskiBraeden committed 2025-11-28 12:21:50 -08:00
1 parent 9e867cd7ab
commit 7208d3dd44
1 file changed
+108 -8
@@ -5,6 +5,7 @@ import javafx.geometry.Pos;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import javafx.scene.text.FontWeight; import javafx.scene.text.FontWeight;
import javafx.stage.Stage; import javafx.stage.Stage;
@@ -29,7 +30,10 @@ import java.util.List;
public class NumberGame public class NumberGame
extends Application extends Application
{ {
private static final int DECIMAL_PLACES = 2;
private static final int NO_GAMES_WON = 0;
private static final int STARTING_NUMBERS_PLACED = 0; private static final int STARTING_NUMBERS_PLACED = 0;
private static final int MENU_FONT_SIZE = 16;
private static final int FONT_SIZE = 24; private static final int FONT_SIZE = 24;
private static final int WINDOW_WIDTH = 700; private static final int WINDOW_WIDTH = 700;
private static final int WINDOW_HEIGHT = 600; private static final int WINDOW_HEIGHT = 600;
@@ -41,8 +45,15 @@ public class NumberGame
private static final int TOTAL_NUMBERS = 20; private static final int TOTAL_NUMBERS = 20;
private static final int MIN_RAND_NUM = 1; private static final int MIN_RAND_NUM = 1;
private static final int MAX_RAND_NUM = 1001; private static final int MAX_RAND_NUM = 1001;
private static final int MENU_PADDING = 20;
private static final int POPUP_WIDTH = 400;
private static final int POPUP_HEIGHT = 200;
private static final Font FONT = Font.font("Arial", FontWeight.BOLD, FONT_SIZE); 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 int numbersPlaced;
private Label numberLabel; private Label numberLabel;
private int currentNumber; private int currentNumber;
@@ -58,6 +69,7 @@ public class NumberGame
@Override @Override
public void start(final Stage stage) public void start(final Stage stage)
{ {
this.gamesPlayed++;
this.placementValidator = new AscendingPlacement(); this.placementValidator = new AscendingPlacement();
this.generator = new RandomNumberGenerator(MIN_RAND_NUM, MAX_RAND_NUM); this.generator = new RandomNumberGenerator(MIN_RAND_NUM, MAX_RAND_NUM);
this.numbersPlaced = STARTING_NUMBERS_PLACED; this.numbersPlaced = STARTING_NUMBERS_PLACED;
@@ -134,30 +146,115 @@ public class NumberGame
{ {
final Stage popup; final Stage popup;
final VBox layout; final VBox layout;
final HBox buttonLayout;
final Label msgLabel; final Label msgLabel;
final Button restartButton; final Button restartButton;
final Button quitButton;
final Scene popupScene; final Scene popupScene;
popup = new Stage(); popup = new Stage();
popup.setTitle(title); popup.setTitle(title);
msgLabel = new Label(message); msgLabel = new Label(message);
msgLabel.setFont(FONT); msgLabel.setFont(MENU_FONT);
msgLabel.setAlignment(Pos.CENTER); msgLabel.setAlignment(Pos.CENTER);
restartButton = new Button("Restart"); restartButton = new Button("Restart");
restartButton.setFont(FONT); restartButton.setFont(MENU_FONT);
restartButton.setOnAction(e -> restartButton.setOnAction(e -> {
{
popup.close(); popup.close();
restartGame(); restartGame();
}); });
layout = new VBox(20, msgLabel, restartButton); quitButton = new Button("Quit");
layout.setAlignment(Pos.CENTER); quitButton.setFont(MENU_FONT);
layout.setPadding(new Insets(20)); quitButton.setOnAction(e -> {
popup.close();
popupScene = new Scene(layout, 400, 200); showResults();
});
buttonLayout = new HBox(MENU_PADDING, restartButton, quitButton);
layout = new VBox(MENU_PADDING, msgLabel, buttonLayout);
buttonLayout.setAlignment(Pos.CENTER);
layout.setAlignment(Pos.CENTER);
layout.setPadding(new Insets(MENU_PADDING));
popupScene = new Scene(layout, POPUP_WIDTH, POPUP_HEIGHT);
popup.setScene(popupScene);
popup.setResizable(false);
popup.initOwner(numberLabel.getScene().getWindow());
popup.show();
}
private void showResults()
{
final Stage popup;
final VBox layout;
final Label msgLabel;
final Button okButton;
final Scene popupScene;
final StringBuilder results;
results = new StringBuilder();
popup = new Stage();
popup.setTitle("Results");
if (this.gamesWon > NO_GAMES_WON)
{
results.append("You won ");
results.append(this.gamesWon);
results.append(" out of ");
results.append(this.gamesPlayed);
results.append(" games");
if (this.gamesWon != this.gamesPlayed)
{
results.append(" and ");
}
}
if (this.gamesPlayed - this.gamesWon > NO_GAMES_WON)
{
results.append("You lost ");
results.append(this.gamesPlayed - this.gamesWon);
results.append(" out of ");
results.append(this.gamesPlayed);
results.append(" games");
}
final float average;
final String averageFormatted;
average = (float) this.allTimePlaced / (float) this.gamesPlayed;
averageFormatted = String.format(String.format("%%.%df", DECIMAL_PLACES), average);
results.append(", with ");
results.append(this.allTimePlaced);
results.append(" successful\nplacements, ");
results.append("an average of ");
results.append(averageFormatted);
results.append(" per game.");
msgLabel = new Label(results.toString());
msgLabel.setFont(MENU_FONT);
msgLabel.setAlignment(Pos.CENTER);
okButton = new Button("OK");
okButton.setFont(MENU_FONT);
okButton.setOnAction(e -> {
popup.close();
final Stage gameStage;
gameStage = (Stage) this.numberLabel.getScene().getWindow();
gameStage.close();
});
layout = new VBox(MENU_PADDING, msgLabel, okButton);
layout.setAlignment(Pos.CENTER);
layout.setPadding(new Insets(MENU_PADDING));
popupScene = new Scene(layout, POPUP_WIDTH, POPUP_HEIGHT);
popup.setScene(popupScene); popup.setScene(popupScene);
popup.setResizable(false); popup.setResizable(false);
popup.initOwner(numberLabel.getScene().getWindow()); popup.initOwner(numberLabel.getScene().getWindow());
@@ -206,6 +303,7 @@ public class NumberGame
*/ */
private void triggerWin() private void triggerWin()
{ {
this.gamesWon++;
this.numberLabel.setText("All numbers placed!"); this.numberLabel.setText("All numbers placed!");
disableAllButtons(); disableAllButtons();
showPopup("You Won!", "Congratulations! You placed all numbers correctly."); showPopup("You Won!", "Congratulations! You placed all numbers correctly.");
@@ -224,7 +322,9 @@ public class NumberGame
button.setMouseTransparent(true); button.setMouseTransparent(true);
button.setFocusTraversable(false); button.setFocusTraversable(false);
button.setStyle("-fx-background-color: #95f595; -fx-text-fill: black;"); button.setStyle("-fx-background-color: #95f595; -fx-text-fill: black;");
this.numbersPlaced++; this.numbersPlaced++;
this.allTimePlaced++;
this.positions[index] = this.currentNumber; this.positions[index] = this.currentNumber;