From f7da380695b1b5c91e06b8064221e544e96ca37d Mon Sep 17 00:00:00 2001 From: Anteneh Denbel <112484153+Antenehden@users.noreply.github.com> Date: Tue, 6 Aug 2024 22:12:22 -0500 Subject: [PATCH] Update Round.java --- src/main/java/uta/cse3310/Round.java | 212 +++++++++++---------------- 1 file changed, 84 insertions(+), 128 deletions(-) diff --git a/src/main/java/uta/cse3310/Round.java b/src/main/java/uta/cse3310/Round.java index fd00753..5620f44 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,6 +24,7 @@ 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 { @@ -55,9 +56,8 @@ 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"); + System.out.println("returning after nextTurn"); } public void nextTurn() { @@ -73,7 +73,7 @@ public void nextTurn() { waitingForInput = true; synchronized (turnLock) { - new Timer().schedule(new TimerTask() { + new java.util.Timer().schedule(new java.util.TimerTask() { @Override public void run() { synchronized (turnLock) { @@ -82,7 +82,7 @@ public void run() { currentPlayer.getTimer().stop(); waitingForInput = false; turnLock.notifyAll(); - System.out.println("Reached inside loop"); + System.out.println("reached inside loop"); advanceTurn(); } } @@ -107,41 +107,112 @@ 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; - playerActionTaken(); // Notify the waiting thread - System.out.println("Going to next round"); + 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); @@ -151,11 +222,10 @@ 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(); @@ -192,121 +262,8 @@ 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); - - 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 { + /*public static void main(String[] args) throws IOException { List testwords = Arrays.asList("testword"); WordList wordlist = new WordList(testwords); @@ -323,6 +280,5 @@ public static void main(String[] args) throws IOException { game.addPlayer(new Player("Player2", PlayerType.HUMAN)); game.startGame(); System.exit(0); - } + }*/ } - */