-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Added find stock game functionality
- Loading branch information
Showing
1 changed file
with
156 additions
and
0 deletions.
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
...ain/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/minigames/games/FindStockGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| } | ||
| } |