From 66831c9d71fada10b64374c5a2c605f778582533 Mon Sep 17 00:00:00 2001 From: Anteneh Denbel <112484153+Antenehden@users.noreply.github.com> Date: Fri, 26 Jul 2024 18:52:33 -0500 Subject: [PATCH 1/4] Update Round.java --- src/main/java/uta/cse3310/Round.java | 187 +++++++++++++++++++++++---- 1 file changed, 165 insertions(+), 22 deletions(-) diff --git a/src/main/java/uta/cse3310/Round.java b/src/main/java/uta/cse3310/Round.java index ab355e8..5a053de 100644 --- a/src/main/java/uta/cse3310/Round.java +++ b/src/main/java/uta/cse3310/Round.java @@ -1,7 +1,9 @@ package uta.cse3310; import java.io.IOException; +import java.util.ArrayList; import java.util.List; +import java.util.Scanner; public class Round { private Word word; @@ -9,6 +11,8 @@ public class Round { private int currentPlayerIndex; private List players; private boolean isRoundActive; + private static final int TURN_TIME_LIMIT = 100; // Time limit for each player's turn in seconds + private static final int VOWEL_COST = 50; // Cost for buying a vowel public Round(List players, String wordFilePath, String stakeFilePath) throws IOException { this.players = players; @@ -24,8 +28,9 @@ private String[] loadWords(String filePath) throws IOException { } public void startRound() { - System.out.println("New round started. Word to guess: " + word.getWordProgress()); + //System.out.println("New round started. Word to guess: " + word.getWordProgress()); this.isRoundActive = true; + nextTurn(); } public void nextTurn() { @@ -35,41 +40,175 @@ public void nextTurn() { } Player currentPlayer = players.get(currentPlayerIndex); - System.out.println("Current player: " + currentPlayer.getName()); + currentPlayer.getTimer().reset(); + currentPlayer.getTimer().start(); + System.out.println("Current player: " + currentPlayer.getName() + " has " + TURN_TIME_LIMIT + " seconds to guess."); + + // Present options to the player + presentOptions(currentPlayer); + + // Schedule a task to check the timer and move to the next turn if time is up + new java.util.Timer().schedule(new java.util.TimerTask() { + @Override + public void run() { + if (currentPlayer.getTimer().getElapsedTime() >= TURN_TIME_LIMIT) { + System.out.println("Time is up for player " + currentPlayer.getName()); + currentPlayer.getTimer().stop(); + advanceTurn(); + } + } + }, TURN_TIME_LIMIT * 1000); + } + + private void presentOptions(Player currentPlayer) { + System.out.println("Word progress: " + word.getWordProgress()); + System.out.println("Choose an option:"); + System.out.println("1. Buy a vowel"); + System.out.println("2. Select a constant"); + System.out.println("3. Solve the puzzle"); + + // Get player input + int choice = getPlayerChoice(); + handleChoice(currentPlayer, choice); - // Simulate a guess with random letter, replace with actual user input logic - char guessedLetter = getRandomLetter(); + } + + private int getPlayerChoice() { + Scanner scanner = new Scanner(System.in); + return scanner.nextInt(); + } + + private void handleChoice(Player currentPlayer, int choice) { + switch (choice) { + case 1: + boolean validVowel = false; + while (!validVowel) { + System.out.println("Enter a vowel to buy:"); + char vowel = getUserInput(); + validVowel = buyVowel(currentPlayer, vowel); + } + break; + case 2: + boolean validConstant = false; + while (!validConstant) { + System.out.println("Enter a constant to select:"); + char constant = getUserInput(); + validConstant = selectConstant(currentPlayer, constant); + } + break; + case 3: + System.out.println("Enter the solution:"); + Scanner scanner = new Scanner(System.in); + String solution = scanner.nextLine(); + if (solvePuzzle(currentPlayer, solution)) { + isRoundActive = false; + } else { + advanceTurn(); + } + break; + default: + System.out.println("Invalid choice. Turn skipped."); + advanceTurn(); + break; + } + } + + private char getUserInput() { + Scanner scanner = new Scanner(System.in); + return scanner.next().charAt(0); + } + + private void processGuess(Player currentPlayer, char guessedLetter) { boolean isCorrect = word.guessLetter(guessedLetter); if (isCorrect) { System.out.println("Correct guess!"); + int points = stake.calculatePoints(guessedLetter); + currentPlayer.addScore(points); + System.out.println("Player " + currentPlayer.getName() + " awarded " + points + " points."); + if (word.isFullyGuessed()) { + System.out.println("Word guessed correctly! Round over."); + isRoundActive = false; + } else { + currentPlayer.getTimer().reset(); + nextTurn(); // Player gets another turn + } } else { System.out.println("Incorrect guess."); + advanceTurn(); } - - if (word.isFullyGuessed()) { - System.out.println("Word guessed correctly! Round over."); - isRoundActive = false; - } else { - currentPlayerIndex = (currentPlayerIndex + 1) % players.size(); + } + + private void advanceTurn() { + Player currentPlayer = players.get(currentPlayerIndex); + currentPlayer.getTimer().stop(); + currentPlayerIndex = (currentPlayerIndex + 1) % players.size(); + if (isRoundActive) { + nextTurn(); + } + } + + public boolean buyVowel(Player player, char vowel) { + if ("aeiou".indexOf(vowel) == -1) { + System.out.println("Invalid vowel: " + vowel); + return false; + } + + if (player.getScore() < VOWEL_COST) { + System.out.println("Player " + player.getName() + " does not have enough points to buy a vowel."); + return false; } + + if (player.hasBoughtVowel(vowel)) { + System.out.println("Vowel " + vowel + " has already been bought by player " + player.getName()); + return false; + } + + player.deductScore(VOWEL_COST); + player.buyVowel(vowel); + + processGuess(player, vowel); + + return true; } - private char getRandomLetter() { - return (char) ('a' + new java.util.Random().nextInt(26)); + public boolean selectConstant(Player player, char constant) { + if ("aeiou".indexOf(constant) != -1) { + System.out.println("Invalid constant: " + constant); + return false; + } + + if (player.hasGuessedConstant(constant)) { + System.out.println("Constant " + constant + " has already been selected by player " + player.getName()); + return false; + } + + player.guessConstant(constant); + + processGuess(player, constant); + + return true; + } + + public boolean solvePuzzle(Player player, String solution) { + if (word.solve(solution)) { + System.out.println(player.getName() + " solved the puzzle!"); + player.addScore(10); // Award points based on solution length + isRoundActive = false; + return true; + } else { + System.out.println("Incorrect solution by player " + player.getName()); + advanceTurn(); + return false; + } } public String getCurrentWordProgress() { return word.getWordProgress(); } - // Commented out as it does not exist - // public String getCurrentStake() { - // return stake.getCurrentStake(); - // } - public void resetRound() throws IOException { this.word.reset(); - // this.stake.reset(); + this.stake.reset(); this.currentPlayerIndex = 0; this.isRoundActive = true; } @@ -77,9 +216,13 @@ public void resetRound() throws IOException { public boolean isRoundActive() { return isRoundActive; } -} - - - + public static void main(String[] args) throws IOException { + List players = new ArrayList<>(); + Game game = new Game(players, "src/test/resources/test_words.txt", "src/test/resources/test_stakes.txt", new Statistics()); + game.addPlayer(new Player("Player1")); + game.addPlayer(new Player("Player2")); + game.startGame(); + } +} From 25b00b2d7c6768b2d3a5ae77ed9d28f031548222 Mon Sep 17 00:00:00 2001 From: Anteneh Denbel <112484153+Antenehden@users.noreply.github.com> Date: Fri, 26 Jul 2024 18:52:56 -0500 Subject: [PATCH 2/4] Update Game.java --- src/main/java/uta/cse3310/Game.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/uta/cse3310/Game.java b/src/main/java/uta/cse3310/Game.java index 4e28064..996d6a8 100644 --- a/src/main/java/uta/cse3310/Game.java +++ b/src/main/java/uta/cse3310/Game.java @@ -7,6 +7,8 @@ public class Game { + private static final int MAX_PLAYERS = 4; + private List players; private List rounds; private int currentRoundIndex; @@ -37,6 +39,16 @@ public void setGameId(int gameId) { this.gameId = gameId; } + public boolean addPlayer(Player player) { + if (players.size() < MAX_PLAYERS) { + players.add(player); + return true; // Player added successfully + } else { + System.out.println("Cannot add player: maximum number of players reached."); + return false; // Player not added + } + } + public void removePlayer(WebSocket conn) { players.removeIf(player -> player.getId().equals(conn.getAttachment())); } @@ -53,10 +65,17 @@ public void update(UserEvent event) { } public void startGame() { + if (players.size() < 2) { + System.out.println("Cannot start game: not enough players."); + isGameActive = false; + return; + } System.out.println("Game started with " + players.size() + " players."); + isGameActive = true; startNextRound(); } + public void startNextRound() { if (currentRoundIndex >= rounds.size()) { System.out.println("All rounds completed."); From 7c0fa2b43dc549a9e8ef76e42e098d3f465b0ce2 Mon Sep 17 00:00:00 2001 From: Anteneh Denbel <112484153+Antenehden@users.noreply.github.com> Date: Fri, 26 Jul 2024 18:53:13 -0500 Subject: [PATCH 3/4] Update Player.java --- src/main/java/uta/cse3310/Player.java | 51 ++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/main/java/uta/cse3310/Player.java b/src/main/java/uta/cse3310/Player.java index 6e4d002..9b50838 100644 --- a/src/main/java/uta/cse3310/Player.java +++ b/src/main/java/uta/cse3310/Player.java @@ -1,5 +1,7 @@ package uta.cse3310; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; public class Player { @@ -8,8 +10,18 @@ public class Player { private int score; private boolean iswinner; private GameTimer timer; // Updated reference + private List boughtVowels; + private List guessedConstants; + public Player(String name) { + this.id = UUID.randomUUID().toString(); + this.name = name; + this.score = 1000; + this.timer = new GameTimer(); // Updated reference + this.boughtVowels = new ArrayList<>(); + this.guessedConstants = new ArrayList<>(); + } public boolean isWinner() { return iswinner; @@ -19,13 +31,6 @@ public void setWinner(boolean iswinner) { this.iswinner = iswinner; } - public Player(String name) { - this.id = UUID.randomUUID().toString(); - this.name = name; - this.score = 0; - this.timer = new GameTimer(); // Updated reference - } - public String getId() { return id; } @@ -42,10 +47,40 @@ public void addScore(int points) { this.score += points; } + public void deductScore(int points) { + this.score -= points; + } + public void resetScore() { this.score = 0; } -} + public GameTimer getTimer() { + return timer; + } + + public boolean hasBoughtVowel(char vowel) { + return this.boughtVowels.contains(vowel); + } + + public void buyVowel(char vowel) { + this.boughtVowels.add(vowel); + } + + public List getBoughtVowels() { + return this.boughtVowels; + } + + public void guessConstant(char constant) { + this.guessedConstants.add(constant); + } + public List getGuessedConstants() { + return this.guessedConstants; + } + + public boolean hasGuessedConstant(char constant) { + return this.guessedConstants.contains(constant); + } +} From ddfa265aef512e111076531a9d31415074629cd7 Mon Sep 17 00:00:00 2001 From: Anteneh Denbel <112484153+Antenehden@users.noreply.github.com> Date: Fri, 26 Jul 2024 18:53:32 -0500 Subject: [PATCH 4/4] Update Stake.java --- src/main/java/uta/cse3310/Stake.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/uta/cse3310/Stake.java b/src/main/java/uta/cse3310/Stake.java index d0b2d41..0e173d0 100644 --- a/src/main/java/uta/cse3310/Stake.java +++ b/src/main/java/uta/cse3310/Stake.java @@ -23,6 +23,11 @@ public void setCurrentStake(String currentStake) { public void reset() { this.currentStake = null; // Or any default value } + + public int calculatePoints(char guessedLetter) { + // Example calculation logic needs to be replaced with actual game logic + return 10; + } }