Skip to content

60 game ending #63

Merged
merged 3 commits into from
May 26, 2026
Merged
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package no.ntnu.gruppe53.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
import javafx.util.Subscription;
import no.ntnu.gruppe53.model.*;
Expand All @@ -26,6 +30,7 @@ public class GameController {
private MarketView marketView;
private PortfolioView portfolioView;
private TransactionArchiveView transactionArchiveView;
private EndView endView;
private PurchaseCalculator purchaseCalculator;
private SaleCalculator saleCalculator;

Expand Down Expand Up @@ -71,13 +76,16 @@ public void startNewGame() {
transactionArchiveView = new TransactionArchiveView();
transactionArchiveView.setPlayer(player);

endView = new EndView();

sharedNav.bindPlayer(player);
sharedNav.bindExchange(exchange);
sharedFooter.setExchange(exchange);

vm.addView("portfolio", portfolioView);
vm.addView("market", marketView);
vm.addView("history", transactionArchiveView);
vm.addView("endGame", endView);

initialize(sharedNav, sharedFooter);

Expand All @@ -97,7 +105,7 @@ public void showMarketView() {
/**
* Switches the view to the {@link PortfolioView}.
* <p>Contains the data from a player's {@link Portfolio}.</p>
* <p>PortfolioView lets the player perform {@link Sale} transactions.</p>
* <p>PortfolioView lets theEndView player perform {@link Sale} transactions.</p>
*/
public void showPortfolio() {
if (portfolioView != null) vm.switchView("portfolio");
Expand All @@ -123,6 +131,7 @@ public void initialize(NavigationBar nav, FooterBar footer) {
nav.onMarketButtonClick(() -> vm.switchView("market"));
nav.onPortfolioButtonClick(() -> vm.switchView("portfolio"));
nav.onHistoryButtonClick(() -> vm.switchView("history"));
nav.onEndGameButtonClick(() -> endGame());

footer.onLanguageChange(selected -> {
lm.setLocale(selected.locale());
Expand Down Expand Up @@ -168,7 +177,6 @@ public void initialize(NavigationBar nav, FooterBar footer) {
Stock stock = marketView.getSelectedStock();
if (stock != null) purchaseStock(stock.getSymbol(), marketView.getQuantityPurchase());
});

marketView.onQuantitySelect(() -> {
if (marketView.getSelectedStock() != null) {
marketView.setPurchaseCalculator(calculatePurchase(marketView.getSelectedStock(),
Expand Down Expand Up @@ -198,7 +206,11 @@ public void initialize(NavigationBar nav, FooterBar footer) {

portfolioView.onSellButton(this::handleSell);

endView.onQuitButton(Platform::exit);

endView.onNewGameButton(() -> {
startNewGame();
});
}

/**
Expand Down Expand Up @@ -270,6 +282,47 @@ private SaleCalculator calculateSale(Share share) {

}


/**
* Creates an alert where the Player can confirm if they want to end the game.
* If yes, then sells entire portfolio and displays the EndView.
*/
private void endGame() {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.titleProperty().bind(lm.bindString("endGameAlertTitle"));
alert.headerTextProperty().bind(lm.bindString("endGameAlertHeaderText"));
alert.contentTextProperty().bind(lm.bindString("endGameAlertContentText"));

ButtonType yesButton = new ButtonType(lm.getString("yes"), ButtonBar.ButtonData.YES);
ButtonType noButton = new ButtonType(lm.getString("no"), ButtonBar.ButtonData.NO);


alert.getButtonTypes().setAll(yesButton, noButton);

alert.showAndWait().ifPresent(button -> {
if (button == yesButton) {
sellPortfolio();
vm.setGlobalPanels(null, null);
vm.switchView("endGame");

endView.getStatusLabel().setText(player.getStatus(exchange));
endView.getMoneyLabel().setText(player.getMoney().setScale(2, RoundingMode.HALF_EVEN).toString());



}
});

}

private void sellPortfolio() {
List<Share> sharesToSell = List.copyOf(player.getPortfolio().getShares());

for (Share share : sharesToSell) {
exchange.sell(share, player);
}
}

/**
* Creates a default error alert to be used for notifying the player of errors.
* @param title title of the error alert
Expand Down
203 changes: 203 additions & 0 deletions millions/src/main/java/no/ntnu/gruppe53/view/EndView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package no.ntnu.gruppe53.view;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.text.TextAlignment;
import no.ntnu.gruppe53.service.LanguageManager;


/**
* Represents the End Game screen, showing the Level the Player reached,
* money earned in total after selling portfolio and gives the option
* to quit the program or start a new game.
*/
public class EndView extends BorderPane {


private Button newGameButton;
private Button quitButton;

private Label statusLabel;
private Label moneyLabel;


private final LanguageManager lm = LanguageManager.getInstance();

public EndView() {
this.setCenter(centerPane());
}

private BorderPane centerPane() {
BorderPane pane = new BorderPane();
pane.setPadding(new Insets(15, 12, 15, 12));
//pane.setSpacing(10);
//Blue Background color
pane.setStyle("-fx-background-color: #00bcf0;");

VBox centerPane = new VBox();

HBox congratsBox = new HBox();
Label congratsLabel = new Label();
congratsLabel.textProperty().bind(lm.bindString("congratsLabel"));
congratsLabel.setStyle("-fx-font-size: 18");
congratsLabel.setTextAlignment(TextAlignment.CENTER);

congratsBox.setStyle("-fx-background-color: #ffe556;" +
"-fx-text-fill: #303539;" +
"-fx-background-radius: 8;" +
"-fx-border-color: #303539;" +
"-fx-border-radius: 8;"
);
congratsBox.setPadding(new Insets(8, 14, 8, 14));
congratsBox.setAlignment(Pos.CENTER);
congratsBox.setMinHeight(65);

congratsBox.getChildren().add(congratsLabel);

HBox statusLabelBox = new HBox();
Label statusLabelText = new Label();
statusLabelText.textProperty().bind(lm.bindString("statusLabelText"));
statusLabelText.setStyle("-fx-font-size: 18");
statusLabel = new Label();
statusLabel.setStyle("-fx-font-size: 18");

statusLabelBox.setStyle("-fx-background-color: #ffe556;" +
"-fx-text-fill: #303539;" +
"-fx-background-radius: 8;" +
"-fx-border-color: #303539;" +
"-fx-border-radius: 8;"
);
statusLabelBox.setPadding(new Insets(8, 14, 8, 14));
statusLabelBox.setAlignment(Pos.CENTER);
statusLabelBox.setMinHeight(65);

statusLabelBox.getChildren().addAll(statusLabelText, statusLabel);

HBox moneyLabelBox = new HBox();
Label moneyLabelText = new Label();
moneyLabelText.textProperty().bind(lm.bindString("navMoneyLabelText"));
moneyLabelText.setStyle("-fx-font-size: 18");
moneyLabel = new Label();
moneyLabel.setStyle("-fx-font-size: 18");

moneyLabelBox.setStyle("-fx-background-color: #ffe556;" +
"-fx-text-fill: #303539;" +
"-fx-background-radius: 8;" +
"-fx-border-color: #303539;" +
"-fx-border-radius: 8;"
);
moneyLabelBox.setPadding(new Insets(8, 14, 8, 14));
moneyLabelBox.setAlignment(Pos.CENTER);
moneyLabelBox.setMinHeight(65);

moneyLabelBox.getChildren().addAll(moneyLabelText, moneyLabel);

centerPane.setPadding(new Insets(15, 12, 15, 12));
centerPane.setAlignment(Pos.CENTER);
centerPane.setSpacing(10);
centerPane.setMaxWidth(700);


centerPane.getChildren().addAll(congratsBox, statusLabelBox, moneyLabelBox);

HBox quitButtonBox = new HBox();
quitButton = new Button();
quitButton.textProperty().bind(lm.bindString("quitGame"));
quitButton.setPrefSize(200, 50);
quitButton.setStyle("-fx-text-fill: #303539;" +
"-fx-font-size: 18px;" +
"-fx-border-color: #303539;" +
"-fx-border-radius: 8;" +
"-fx-background-radius: 8;"
);


quitButtonBox.setStyle("-fx-background-color: #ffe556;" +
"-fx-text-fill: #303539;" +
"-fx-background-radius: 8;" +
"-fx-border-color: #303539;" +
"-fx-border-radius: 8;"
);
quitButtonBox.setPadding(new Insets(8, 14, 8, 14));
quitButtonBox.setAlignment(Pos.CENTER);


quitButtonBox.getChildren().addAll(quitButton);

HBox newGameButtonBox = new HBox();
newGameButton = new Button();
newGameButton.textProperty().bind(lm.bindString("newGame"));
newGameButton.setPrefSize(200, 50);
newGameButton.setStyle("-fx-text-fill: #303539;" +
"-fx-font-size: 18px;" +
"-fx-border-color: #303539;" +
"-fx-border-radius: 8;" +
"-fx-background-radius: 8;"
);
newGameButtonBox.setStyle("-fx-background-color: #ffe556;" +
"-fx-text-fill: #303539;" +
"-fx-background-radius: 8;" +
"-fx-border-color: #303539;" +
"-fx-border-radius: 8;"
);
newGameButtonBox.setPadding(new Insets(8, 14, 8, 14));
newGameButtonBox.setAlignment(Pos.CENTER);
newGameButtonBox.getChildren().add(newGameButton);

HBox buttons = new HBox(quitButtonBox, newGameButtonBox);



Region spacerBottomLeft = new Region();
Region spacerBottomRight = new Region();

HBox.setHgrow(spacerBottomLeft, Priority.ALWAYS);
HBox.setHgrow(spacerBottomRight, Priority.ALWAYS);

HBox bottomPane = new HBox(spacerBottomLeft, buttons, spacerBottomRight);

pane.setCenter(centerPane);
pane.setBottom(bottomPane);

return pane;
}

/**
* Sets the action to be run when clicking the quit button
*
* @param action the action to run when clicking the quit button
*/
public void onQuitButton(Runnable action) {
quitButton.setOnAction(e -> action.run());
}

/**
* Sets the action to be run when clicking the New Game button
*
* @param action the action to run when clicking the New Game button
*/
public void onNewGameButton(Runnable action) {
newGameButton.setOnAction(e -> action.run());
}

/**
* Returns the statusLabel.
*
* @return the statusLabel
*/
public Label getStatusLabel() {
return statusLabel;
}

/**
* Returns the moneyLabel.
*
* @return the moneyLabel
*/
public Label getMoneyLabel() {
return moneyLabel;
}
}
15 changes: 14 additions & 1 deletion millions/src/main/java/no/ntnu/gruppe53/view/NavigationBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class NavigationBar extends HBox {
private final Button marketButton;
private final Button portfolioButton;
private final Button historyButton;
private final Button endGameButton;
private Subscription netWorthSubscription;
private Subscription moneySubscription;
private Subscription currentWeekSubscription;
Expand Down Expand Up @@ -151,7 +152,11 @@ public NavigationBar() {
networthBox.setAlignment(Pos.CENTER);
networthBox.getChildren().addAll(networthLabelText, netWorthLabel);

this.getChildren().addAll(marketButton, portfolioButton, historyButton, spacer, playerBox, statusBox, moneyBox, networthBox);
endGameButton = new Button();
endGameButton.textProperty().bind(lm.bindString("endGameButton"));
endGameButton.setStyle("-fx-background-color: #ffe556; -fx-text-fill: #303539;");

this.getChildren().addAll(marketButton, portfolioButton, historyButton, spacer, playerBox, statusBox, moneyBox, networthBox, endGameButton);
}
/**
* Exposes the marketButton to the GameController.
Expand Down Expand Up @@ -180,6 +185,14 @@ public void onHistoryButtonClick(Runnable action) {
historyButton.setOnAction(e -> action.run());
}

/**
* Exposes the endGameButton to the GameController.
*
* @param action the action to be run when clicking the button
*/
public void onEndGameButtonClick(Runnable action) {
endGameButton.setOnAction(e -> action.run());
}


/**
Expand Down
18 changes: 17 additions & 1 deletion millions/src/main/resources/i18n/lang.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# File for the english language

# General
yes = Yes
no = No

# StartView
newGame = New Game
languageButtonLabel = Toggle Language
Expand All @@ -26,7 +30,8 @@ navPlayerLabelText = Player:
marketButton = Market
portfolioButton = Portfolio
historyButton = History
statusLabelText = Status:
statusLabelText = Level:
endGameButton = End Game

# FooterBar
currentWeekLabelText = Current Week:
Expand Down Expand Up @@ -64,3 +69,14 @@ historyTitle=Transaction History
historyTypePurchase=Purchase
historyTypeSale=Sale
historyCellFormat=Week %d | %s | %s | Price: %s | Qty: %s | Gross: %s | Commission: %s | Tax: %s | Total: $%s | Profit: %s

# EndView
congratsLabel = Thank you for playing Millions!\nBelow you can see what level you reached and the amount of\nmoney you ended up with!\nYou can also choose to start a new game.

# EndGameAlert
endGameAlertTitle = Ending Game
endGameAlertHeaderText=Warning, this ends the game!
endGameAlertContentText = Are your sure you want to end the game?



Loading