handle game results
This commit is contained in:
1 file changed
+108
-8
@@ -5,6 +5,7 @@ import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.scene.text.FontWeight;
|
||||
import javafx.stage.Stage;
|
||||
@@ -29,7 +30,10 @@ import java.util.List;
|
||||
public class NumberGame
|
||||
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 MENU_FONT_SIZE = 16;
|
||||
private static final int FONT_SIZE = 24;
|
||||
private static final int WINDOW_WIDTH = 700;
|
||||
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 MIN_RAND_NUM = 1;
|
||||
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 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;
|
||||
@@ -58,6 +69,7 @@ public class NumberGame
|
||||
@Override
|
||||
public void start(final Stage stage)
|
||||
{
|
||||
this.gamesPlayed++;
|
||||
this.placementValidator = new AscendingPlacement();
|
||||
this.generator = new RandomNumberGenerator(MIN_RAND_NUM, MAX_RAND_NUM);
|
||||
this.numbersPlaced = STARTING_NUMBERS_PLACED;
|
||||
@@ -134,30 +146,115 @@ public class NumberGame
|
||||
{
|
||||
final Stage popup;
|
||||
final VBox layout;
|
||||
final HBox buttonLayout;
|
||||
final Label msgLabel;
|
||||
final Button restartButton;
|
||||
final Button quitButton;
|
||||
final Scene popupScene;
|
||||
|
||||
popup = new Stage();
|
||||
popup.setTitle(title);
|
||||
|
||||
msgLabel = new Label(message);
|
||||
msgLabel.setFont(FONT);
|
||||
msgLabel.setFont(MENU_FONT);
|
||||
msgLabel.setAlignment(Pos.CENTER);
|
||||
|
||||
restartButton = new Button("Restart");
|
||||
restartButton.setFont(FONT);
|
||||
restartButton.setOnAction(e ->
|
||||
{
|
||||
restartButton.setFont(MENU_FONT);
|
||||
restartButton.setOnAction(e -> {
|
||||
popup.close();
|
||||
restartGame();
|
||||
});
|
||||
|
||||
layout = new VBox(20, msgLabel, restartButton);
|
||||
layout.setAlignment(Pos.CENTER);
|
||||
layout.setPadding(new Insets(20));
|
||||
quitButton = new Button("Quit");
|
||||
quitButton.setFont(MENU_FONT);
|
||||
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.setResizable(false);
|
||||
popup.initOwner(numberLabel.getScene().getWindow());
|
||||
@@ -206,6 +303,7 @@ public class NumberGame
|
||||
*/
|
||||
private void triggerWin()
|
||||
{
|
||||
this.gamesWon++;
|
||||
this.numberLabel.setText("All numbers placed!");
|
||||
disableAllButtons();
|
||||
showPopup("You Won!", "Congratulations! You placed all numbers correctly.");
|
||||
@@ -224,7 +322,9 @@ public class NumberGame
|
||||
button.setMouseTransparent(true);
|
||||
button.setFocusTraversable(false);
|
||||
button.setStyle("-fx-background-color: #95f595; -fx-text-fill: black;");
|
||||
|
||||
this.numbersPlaced++;
|
||||
this.allTimePlaced++;
|
||||
|
||||
this.positions[index] = this.currentNumber;
|
||||
|
||||
|
||||
Reference in new issue
Block a user