From e97cbce5b95a27bf88ee0db0fc72b5ea21f06526 Mon Sep 17 00:00:00 2001 From: Anteneh Denbel <112484153+Antenehden@users.noreply.github.com> Date: Sat, 10 Aug 2024 19:08:09 -0500 Subject: [PATCH] Update to implement stakes --- src/main/java/uta/cse3310/App.java | 13 +++++- src/main/java/uta/cse3310/Game.java | 4 -- src/main/java/uta/cse3310/Round.java | 20 ++++++-- src/main/java/uta/cse3310/Stake.java | 69 +++++++++++++++++++++------- 4 files changed, 79 insertions(+), 27 deletions(-) diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index 53b2a5d..50777fe 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -70,7 +70,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) { players.add(newPlayer); // Initialize with the new player try { - game = new Game(players, "src/main/resources/words.txt", "src/main/resources/stakes.txt", + game = new Game(players, "src/main/resources/words.txt", "src/main/resources/stake.txt", new Statistics()); } catch (IOException e) { e.printStackTrace(); @@ -132,6 +132,17 @@ public void onMessage(WebSocket conn, String message) { Game game = conn.getAttachment(); if (game != null) { + if ("START_GAME".equals(event.getAction())) { + if (!game.isGameActive() && game.getPlayers().size() >= 2) { + game.startGame(); + String jsonString = gson.toJson(game); + broadcastToGame(game, jsonString); // Broadcast the game state to all players + } else { + System.out.println("Game cannot be started. Either it's already active or there aren't enough players."); + } + return; + } + Player currentPlayer = game.getCurrentRound().getCurrentPlayer(); if (!event.getPlayerId().equals(currentPlayer.getId())) { diff --git a/src/main/java/uta/cse3310/Game.java b/src/main/java/uta/cse3310/Game.java index ee94a44..a065046 100644 --- a/src/main/java/uta/cse3310/Game.java +++ b/src/main/java/uta/cse3310/Game.java @@ -101,10 +101,6 @@ public void update(UserEvent event) { switch (action) { - case "START_GAME": - startGame(); - correctGuess = true; - break; case "BUY_VOWEL": correctGuess = currentRound.buyVowel(currentPlayer, event.getValue().charAt(0)); break; diff --git a/src/main/java/uta/cse3310/Round.java b/src/main/java/uta/cse3310/Round.java index c16444f..796366a 100644 --- a/src/main/java/uta/cse3310/Round.java +++ b/src/main/java/uta/cse3310/Round.java @@ -20,6 +20,7 @@ public class Round { private HashSet lettersinword; public boolean waitingForInput = false; private Player currentPlayer; + private String currentStake; @SuppressWarnings("static-access") public Round(List players, String wordFilePath, String stakeFilePath) throws IOException { @@ -66,6 +67,9 @@ public void nextTurn() { currentPlayer = getCurrentPlayer(); System.out.println("Current player: " + currentPlayer.getName() + " has " + TURN_TIME_LIMIT + " seconds to guess."); + // Set the current stake for this player's turn + currentStake = stake.getRandomStake(); // Initialize currentStake with a valid value + // Start the player's timer currentPlayer.getTimer().reset(); currentPlayer.getTimer().start(); @@ -122,12 +126,9 @@ private boolean processGuess(Player player, char guessedLetter) { // if the guess is correct add points to player, add guessed letter, current player doesnt change (they get another turn) if (isCorrect == 1) { - System.out.println("Correct guess!"); - int points = stake.calculatePoints(guessedLetter); - player.addScore(points); + int reward = calculateReward(guessedLetter); + player.addScore(reward); correctguesses.add(guessedLetter); - System.out.println("Player " + player.getName() + " awarded " + points + " points."); - System.out.println("\nCorrect guesses: " + correctguesses); //puzzle is solved if (correctguesses.equals(lettersinword)) { @@ -163,6 +164,11 @@ public void advanceTurn() { } + private int calculateReward(char guessedLetter) { + int multiplier = stake.calculatePoints(currentStake, guessedLetter); + return multiplier * (int) wordsforgame.stream().filter(word -> word.contains(String.valueOf(guessedLetter))).count(); + } + public void resetRound() throws IOException { this.stake.reset(); this.currentPlayerIndex = 0; @@ -177,6 +183,10 @@ public Player getCurrentPlayer() { return players.get(currentPlayerIndex); } + public String getCurrentStake() { + return currentStake; + } + public ArrayList getWordsForGame() { return wordsforgame; } diff --git a/src/main/java/uta/cse3310/Stake.java b/src/main/java/uta/cse3310/Stake.java index e666eef..606340e 100644 --- a/src/main/java/uta/cse3310/Stake.java +++ b/src/main/java/uta/cse3310/Stake.java @@ -1,33 +1,64 @@ package uta.cse3310; -public class Stake { - private String currentStake; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; - // Default constructor - public Stake() { - } +public class Stake { + private List stakes; - // Constructor that accepts a string argument - public Stake(String currentStake) { - this.currentStake = currentStake; + public Stake(String filePath) throws IOException { + this.stakes = new ArrayList<>(); + loadStakes(filePath); } - public String getCurrentStake() { - return currentStake; + private void loadStakes(String filePath) throws IOException { + try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { + String line; + while ((line = br.readLine()) != null) { + stakes.add(line.trim()); + } + } } - public void setCurrentStake(String currentStake) { - this.currentStake = currentStake; + public String getRandomStake() { + Random random = new Random(); + return stakes.get(random.nextInt(stakes.size())); } public void reset() { - this.currentStake = null; // Or any default value + this.stakes = null; // Or any default value } - public int calculatePoints(char guessedLetter) { - // Placeholder for actual point calculation logic - // Implement your game logic here - return 10; // Example fixed value, replace with actual logic + public int calculatePoints(String stake, char guessedLetter) { + if (stake == null) { + System.err.println("Stake is null. Defaulting to 0 points."); + return 0; + } + + switch (stake) { + case "Bankrupt": + return 0; + case "Lose Turn": + return 0; + case "Double Points": + return 2 * 10; // Double points, assuming base is 10 + case "Half Points": + return 5; // Half points, assuming base is 10 + case "Free Spin": + case "Extra Turn": + return 10; // Regular points + default: + try { + return Integer.parseInt(stake); + } catch (NumberFormatException e) { + System.err.println("Invalid stake format: " + stake); + return 0; + } + } } } @@ -35,3 +66,7 @@ public int calculatePoints(char guessedLetter) { + + + +