Skip to content

Commit

Permalink
Update to implement stakes
Browse files Browse the repository at this point in the history
  • Loading branch information
Antenehden authored Aug 11, 2024
1 parent 941dd80 commit e97cbce
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 27 deletions.
13 changes: 12 additions & 1 deletion src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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())) {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/uta/cse3310/Round.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Round {
private HashSet<Character> lettersinword;
public boolean waitingForInput = false;
private Player currentPlayer;
private String currentStake;

@SuppressWarnings("static-access")
public Round(List<Player> players, String wordFilePath, String stakeFilePath) throws IOException {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand All @@ -177,6 +183,10 @@ public Player getCurrentPlayer() {
return players.get(currentPlayerIndex);
}

public String getCurrentStake() {
return currentStake;
}

public ArrayList<String> getWordsForGame() {
return wordsforgame;
}
Expand Down
69 changes: 52 additions & 17 deletions src/main/java/uta/cse3310/Stake.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,72 @@
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<String> 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;
}
}
}
}









0 comments on commit e97cbce

Please sign in to comment.