Skip to content

Commit

Permalink
Feat: Added find stock game functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed May 23, 2026
1 parent 4da7300 commit 1607c94
Showing 1 changed file with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>Extends {@link ViewElement}</p>
*
* <p>Implements {@link GameGimmick}</p>
*
* @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<VBox, MiniGamesActions>
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<String> availableSymbols;

/**
* The target stocks' symbol.
* */
private String targetSymbol;

/**
* The logic for changing score.
*
* <p>Gotten from
* {@link edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.minigames.GameEngineController}</p>
* */
private IntConsumer scoreModifier;

/**
* Constructor.
*
* @param symbols a list of stocks to use during the game session.
* */
public FindStockGame(final List<String> symbols) {
super(new VBox(), MiniGamesActions.class);
this.availableSymbols = symbols;
}

/**
* Generates a new round of "find the stock".
*
* <p>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.</p>
* */
private void generateRound() {
grid.getChildren().clear();
if (availableSymbols.isEmpty()) {
return;
}

targetSymbol = availableSymbols.get(
(int) (Math.random() * availableSymbols.size())
);
targetLabel.setText("FIND: " + targetSymbol);

List<String> 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.
*
* <p>Accesses the given score modifier consumer to modify
* the score of this session.</p>
*
* @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.
}
}

0 comments on commit 1607c94

Please sign in to comment.