From 9c21f1ad76ae36020a48c3905398df84b863df29 Mon Sep 17 00:00:00 2001 From: Anteneh Denbel <112484153+Antenehden@users.noreply.github.com> Date: Tue, 6 Aug 2024 16:59:44 -0500 Subject: [PATCH] update cse3310 with fixes --- src/main/java/uta/cse3310/Game.java | 1 + src/main/java/uta/cse3310/Round.java | 210 ++++++++++++++++----------- 2 files changed, 129 insertions(+), 82 deletions(-) diff --git a/src/main/java/uta/cse3310/Game.java b/src/main/java/uta/cse3310/Game.java index 0538e75..955e6f8 100644 --- a/src/main/java/uta/cse3310/Game.java +++ b/src/main/java/uta/cse3310/Game.java @@ -125,6 +125,7 @@ public void startGame() { System.out.println("back in game"); isGameActive = true; startNextRound(); + System.out.println("finished start next round method"); if(i == 2){ isGameActive = false; diff --git a/src/main/java/uta/cse3310/Round.java b/src/main/java/uta/cse3310/Round.java index 54840bb..fd00753 100644 --- a/src/main/java/uta/cse3310/Round.java +++ b/src/main/java/uta/cse3310/Round.java @@ -2,11 +2,11 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Scanner; +import java.util.Timer; import java.util.TimerTask; -//import java.util.concurrent.CountDownLatch; -import java.util.HashSet; public class Round { private Word word; @@ -24,7 +24,6 @@ public class Round { private transient Scanner scanner; public boolean waitingForInput = false; private final Object turnLock = new Object(); - //private CountDownLatch latch; @SuppressWarnings("static-access") public Round(List players, String wordFilePath, String stakeFilePath, Scanner scanner) throws IOException { @@ -56,7 +55,9 @@ public Round(List players, WordList wordlist, String stakeFilePath, Scan public void startRound() { this.isRoundActive = true; + this.waitingForInput = true; // Reset the waitingForInput flag at the start of each round nextTurn(); + System.out.println("Returning after nextTurn"); } public void nextTurn() { @@ -72,7 +73,7 @@ public void nextTurn() { waitingForInput = true; synchronized (turnLock) { - new java.util.Timer().schedule(new java.util.TimerTask() { + new Timer().schedule(new TimerTask() { @Override public void run() { synchronized (turnLock) { @@ -81,6 +82,7 @@ public void run() { currentPlayer.getTimer().stop(); waitingForInput = false; turnLock.notifyAll(); + System.out.println("Reached inside loop"); advanceTurn(); } } @@ -105,112 +107,41 @@ public void playerActionTaken() { } } -/* - @SuppressWarnings("static-access") - private void presentOptions(Player currentPlayer) { - word.getWordProgress(wordsforgame, correctguesses); - System.out.println("\ncurrent word(s) to guess:" + wordsforgame); - System.out.println("Choose an option:"); - System.out.println("1. Buy a vowel"); - System.out.println("2. Select a consonant"); - System.out.println("3. Solve the puzzle"); - int choice = getPlayerChoice(); - handleChoice(currentPlayer, choice); - } - - private int getPlayerChoice() { - int choice = scanner.nextInt(); - System.out.println("Player choice: " + choice); - return choice; - } - - private void handleChoice(Player currentPlayer, int choice) { - switch (choice) { - case 1: - buyVowel(currentPlayer); - break; - case 2: - selectConsonant(currentPlayer); - break; - case 3: - solvePuzzle(currentPlayer); - break; - default: - System.out.println("Invalid choice. Turn skipped."); - advanceTurn(); - break; - } - }*/ - public void buyVowel(Player currentPlayer, char vowel) { - /*boolean validVowel = false; - while (!validVowel) { - //System.out.println("Enter a vowel to buy:"); - //char vowel = getUserInput(); - if ("aeiou".indexOf(vowel) != -1 && currentPlayer.getScore() >= VOWEL_COST && !currentPlayer.hasBoughtVowel(vowel)) { - validVowel = true; - currentPlayer.deductScore(VOWEL_COST); - currentPlayer.buyVowel(vowel); - processGuess(currentPlayer, vowel); - } else { - System.out.println("Invalid vowel or not enough points or already bought. Try again."); - } - }*/ - - //System.out.println("Attempting to buy vowel: " + vowel); // Debugging statement if ("aeiou".indexOf(vowel) != -1 && currentPlayer.getScore() >= VOWEL_COST && !currentPlayer.hasBoughtVowel(vowel)) { currentPlayer.deductScore(VOWEL_COST); currentPlayer.buyVowel(vowel); processGuess(currentPlayer, vowel); + playerActionTaken(); // Notify the waiting thread } else { System.out.println("Invalid vowel or not enough points or already bought. Try again."); } } public void selectConsonant(Player currentPlayer, char consonant) { - /*boolean validConsonant = false; - while (!validConsonant) { - //System.out.println("Enter a consonant to select:"); - //char consonant = getUserInput(); - if ("aeiou".indexOf(consonant) == -1 && !currentPlayer.hasGuessedConsonant(consonant)) { - validConsonant = true; - currentPlayer.guessConsonant(consonant); - processGuess(currentPlayer, consonant); - } else { - System.out.println("Invalid consonant or already guessed. Try again."); - } - }*/ - if ("aeiou".indexOf(consonant) == -1 && !currentPlayer.hasGuessedConsonant(consonant)) { currentPlayer.guessConsonant(consonant); processGuess(currentPlayer, consonant); + playerActionTaken(); // Notify the waiting thread } else { System.out.println("Invalid consonant or already guessed. Try again."); } } public void solvePuzzle(Player currentPlayer, String solution) { - //System.out.println("Enter the solution:"); - //String solution = scanner.next(); - //System.out.println("User solution input: " + solution); String solutions = solution.replace(" ", ""); if (word.solve(solutions)) { System.out.println(currentPlayer.getName() + " solved the puzzle!"); currentPlayer.addScore(10); isRoundActive = false; - System.out.println("going to next round"); + playerActionTaken(); // Notify the waiting thread + System.out.println("Going to next round"); } else { System.out.println("Incorrect solution by player " + currentPlayer.getName()); advanceTurn(); } } - private char getUserInput() { - char input = scanner.next().charAt(0); - System.out.println("User input: " + input); - return input; - } - @SuppressWarnings("static-access") private void processGuess(Player currentPlayer, char guessedLetter) { int isCorrect = word.iscorrect(guessedLetter, lettersinword); @@ -220,10 +151,11 @@ private void processGuess(Player currentPlayer, char guessedLetter) { currentPlayer.addScore(points); correctguesses.add(guessedLetter); System.out.println("Player " + currentPlayer.getName() + " awarded " + points + " points."); - System.out.println("\ncorrect guesses: " + correctguesses); + System.out.println("\nCorrect guesses: " + correctguesses); if (correctguesses.equals(lettersinword)) { System.out.println("Word guessed correctly! Round over."); isRoundActive = false; + playerActionTaken(); // Notify the waiting thread } else { currentPlayer.getTimer().reset(); nextTurn(); @@ -260,8 +192,121 @@ public Player getCurrentPlayer() { public ArrayList getWordsForGame() { return wordsforgame; } +} + + +/* + * public void nextTurn() { + if (!isRoundActive) { + System.out.println("Round is not active."); + return; + } + + Player currentPlayer = players.get(currentPlayerIndex); + currentPlayer.getTimer().reset(); + currentPlayer.getTimer().start(); + System.out.println("Current player: " + currentPlayer.getName() + " has " + TURN_TIME_LIMIT + " seconds to guess."); + waitingForInput = true; + + synchronized (turnLock) { + new java.util.Timer().schedule(new java.util.TimerTask() { + @Override + public void run() { + synchronized (turnLock) { + if (currentPlayer.getTimer().getElapsedTime() >= TURN_TIME_LIMIT || !waitingForInput) { + System.out.println("Time is up for player " + currentPlayer.getName()); + currentPlayer.getTimer().stop(); + waitingForInput = false; + turnLock.notifyAll(); + System.out.println("reached inside loop"); + advanceTurn(); + } + } + } + }, TURN_TIME_LIMIT * 1000); - /*public static void main(String[] args) throws IOException { + while (waitingForInput) { + try { + turnLock.wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.out.println("Thread interrupted: " + e.getMessage()); + } + } + } + } + + public void playerActionTaken() { + synchronized (turnLock) { + waitingForInput = false; + turnLock.notifyAll(); + } + } + + public void buyVowel(Player currentPlayer, char vowel) { + /*boolean validVowel = false; + while (!validVowel) { + //System.out.println("Enter a vowel to buy:"); + //char vowel = getUserInput(); + if ("aeiou".indexOf(vowel) != -1 && currentPlayer.getScore() >= VOWEL_COST && !currentPlayer.hasBoughtVowel(vowel)) { + validVowel = true; + currentPlayer.deductScore(VOWEL_COST); + currentPlayer.buyVowel(vowel); + processGuess(currentPlayer, vowel); + } else { + System.out.println("Invalid vowel or not enough points or already bought. Try again."); + } + } + + //System.out.println("Attempting to buy vowel: " + vowel); // Debugging statement + if ("aeiou".indexOf(vowel) != -1 && currentPlayer.getScore() >= VOWEL_COST && !currentPlayer.hasBoughtVowel(vowel)) { + currentPlayer.deductScore(VOWEL_COST); + currentPlayer.buyVowel(vowel); + processGuess(currentPlayer, vowel); + } else { + System.out.println("Invalid vowel or not enough points or already bought. Try again."); + } + } + + public void selectConsonant(Player currentPlayer, char consonant) { + /*boolean validConsonant = false; + while (!validConsonant) { + //System.out.println("Enter a consonant to select:"); + //char consonant = getUserInput(); + if ("aeiou".indexOf(consonant) == -1 && !currentPlayer.hasGuessedConsonant(consonant)) { + validConsonant = true; + currentPlayer.guessConsonant(consonant); + processGuess(currentPlayer, consonant); + } else { + System.out.println("Invalid consonant or already guessed. Try again."); + } + } + + if ("aeiou".indexOf(consonant) == -1 && !currentPlayer.hasGuessedConsonant(consonant)) { + currentPlayer.guessConsonant(consonant); + processGuess(currentPlayer, consonant); + } else { + System.out.println("Invalid consonant or already guessed. Try again."); + } + } + + public void solvePuzzle(Player currentPlayer, String solution) { + //System.out.println("Enter the solution:"); + //String solution = scanner.next(); + //System.out.println("User solution input: " + solution); + String solutions = solution.replace(" ", ""); + if (word.solve(solutions)) { + System.out.println(currentPlayer.getName() + " solved the puzzle!"); + currentPlayer.addScore(10); + isRoundActive = false; + System.out.println("going to next round"); + } else { + System.out.println("Incorrect solution by player " + currentPlayer.getName()); + advanceTurn(); + } + } + + public static void main(String[] args) throws IOException { List testwords = Arrays.asList("testword"); WordList wordlist = new WordList(testwords); @@ -278,5 +323,6 @@ public ArrayList getWordsForGame() { game.addPlayer(new Player("Player2", PlayerType.HUMAN)); game.startGame(); System.exit(0); - }*/ + } } + */