From 1607c9473c60df91ac147a4672120b6638aca792 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 23 May 2026 19:06:16 +0200 Subject: [PATCH] Feat: Added find stock game functionality --- .../minigames/games/FindStockGame.java | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/minigames/games/FindStockGame.java diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/minigames/games/FindStockGame.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/minigames/games/FindStockGame.java new file mode 100644 index 0000000..28d92ee --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/minigames/games/FindStockGame.java @@ -0,0 +1,156 @@ +package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.minigames.games; + +import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewElement; +import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.minigames.GameGimmick; +import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.minigames.MiniGamesActions; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.function.IntConsumer; +import javafx.scene.Node; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.VBox; + +/** + * Find correct stock game, found in the + * {@link edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.minigames.MiniGamesView}. + * + *

Extends {@link ViewElement}

+ * + *

Implements {@link GameGimmick}

+ * + * @see edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.minigames.GameEngineView + * @see edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.minigames.MiniGamesView + * */ +public final class FindStockGame + extends ViewElement + implements GameGimmick { + + /** + * Target stock to find. + * */ + private Label targetLabel; + + /** + * Grid of stock choices to select. + * */ + private GridPane grid; + + /** + * List of stock symbols to generate selection from. + * */ + private final List availableSymbols; + + /** + * The target stocks' symbol. + * */ + private String targetSymbol; + + /** + * The logic for changing score. + * + *

Gotten from + * {@link edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.minigames.GameEngineController}

+ * */ + private IntConsumer scoreModifier; + + /** + * Constructor. + * + * @param symbols a list of stocks to use during the game session. + * */ + public FindStockGame(final List symbols) { + super(new VBox(), MiniGamesActions.class); + this.availableSymbols = symbols; + } + + /** + * Generates a new round of "find the stock". + * + *

Gets the target based on the available symbols, and adds it to the + * options for the round. Then generates the remaining options based on + * the available symbols (Duplicate options are allowed). + * When all options are ready, the grid is populated with the options + * as buttons.

+ * */ + private void generateRound() { + grid.getChildren().clear(); + if (availableSymbols.isEmpty()) { + return; + } + + targetSymbol = availableSymbols.get( + (int) (Math.random() * availableSymbols.size()) + ); + targetLabel.setText("FIND: " + targetSymbol); + + List roundOptions = new ArrayList<>(); + roundOptions.add(targetSymbol); + while (roundOptions.size() < 8) { + String randomSymbol = availableSymbols.get( + (int) (Math.random() * availableSymbols.size()) + ); + roundOptions.add(randomSymbol); + } + Collections.shuffle(roundOptions); + + int index = 0; + for (int row = 0; row < 2; row++) { + for (int col = 0; col < 4; col++) { + String symbol = roundOptions.get(index++); + Button optBtn = new Button(symbol); + optBtn.setPrefSize(120, 100); + optBtn.setOnAction(e -> handleChoice(symbol)); + grid.add(optBtn, col, row); + } + } + } + + /** + * Checks if the chosen stock (button press) is the correct stock. + * + *

Accesses the given score modifier consumer to modify + * the score of this session.

+ * + * @param chosen the symbol of the chosen stock. + * */ + private void handleChoice(final String chosen) { + if (chosen.equals(targetSymbol)) { + scoreModifier.accept(2); + } else { + scoreModifier.accept(-1); + } + generateRound(); + } + + @Override + protected void initLayout() { + targetLabel = new Label(); + grid = new GridPane(); + getRootPane().getChildren().addAll(targetLabel, grid); + } + + @Override + protected void initStyling() { + getRootPane().getStyleClass().add("find-stock-minigame-root"); + grid.getStyleClass().add("find-stock-minigame-grid"); + } + + @Override + public Node getCanvasNode() { + return getRootPane(); + } + + @Override + public void initialize(final IntConsumer scoreModifier) { + this.scoreModifier = scoreModifier; + generateRound(); + } + + @Override + public void updateTick() { + // No automatic updates for this game. + } +}