From 262ee7d60f08849af25ff1579478c36f55a0eb6b Mon Sep 17 00:00:00 2001 From: Anteneh Denbel <112484153+Antenehden@users.noreply.github.com> Date: Tue, 6 Aug 2024 02:32:34 -0500 Subject: [PATCH] Update Round.java --- src/main/java/uta/cse3310/Round.java | 110 ++++++++++++++++++--------- 1 file changed, 74 insertions(+), 36 deletions(-) diff --git a/src/main/java/uta/cse3310/Round.java b/src/main/java/uta/cse3310/Round.java index efe07fd..f86dba6 100644 --- a/src/main/java/uta/cse3310/Round.java +++ b/src/main/java/uta/cse3310/Round.java @@ -15,35 +15,47 @@ public class Round { private static final int TURN_TIME_LIMIT = 100; private static final int VOWEL_COST = 50; private WordList wordlist = new WordList(); - //private Word words; private ArrayList wordsforgame; HashSet lettersguessed = new HashSet<>(); HashSet correctguesses = new HashSet<>(); private HashSet lettersinword; - //ArrayList wordsforgame = new ArrayList<>(wordlist.getArrList()); - //HashSet lettersinword = wordlist.findletters(wordsforgame); + private transient Scanner scanner; - public Round(List players, String wordFilePath, String stakeFilePath) throws IOException { + @SuppressWarnings("static-access") + public Round(List players, String wordFilePath, String stakeFilePath, Scanner scanner) throws IOException { this.players = players; + this.scanner = scanner; wordlist.gatherwords(); - //ArrayList wordsforgame = new ArrayList<>(wordlist.getArrList()); - this. wordsforgame = new ArrayList<>(wordlist.getArrList()); + this.wordsforgame = new ArrayList<>(wordlist.getArrList()); System.out.println("words chosen" + wordsforgame); this.lettersinword = wordlist.findletters(wordsforgame); - //HashSet lettersinword = wordlist.findletters(wordsforgame); this.word = new Word(wordsforgame); this.stake = new Stake(stakeFilePath); this.currentPlayerIndex = 0; this.isRoundActive = true; } - public void startRound() {//might be here + @SuppressWarnings("static-access") + public Round(List players, WordList wordlist, String stakeFilePath, Scanner scanner) throws IOException { + this.players = players; + this.wordlist = wordlist; + this.scanner = scanner; + this.wordsforgame = new ArrayList<>(wordlist.getWords()); + System.out.println("words chosen" + wordsforgame); + this.lettersinword = wordlist.findletters(wordsforgame); + this.word = new Word(wordsforgame); + this.stake = new Stake(stakeFilePath); + this.currentPlayerIndex = 0; + this.isRoundActive = true; + } + + public void startRound() { this.isRoundActive = true; nextTurn(); } public void nextTurn() { - if (!isRoundActive) {//might be here + if (!isRoundActive) { System.out.println("Round is not active."); return; } @@ -53,7 +65,7 @@ public void nextTurn() { currentPlayer.getTimer().start(); System.out.println("Current player: " + currentPlayer.getName() + " has " + TURN_TIME_LIMIT + " seconds to guess."); - presentOptions(currentPlayer); + //presentOptions(currentPlayer); new java.util.Timer().schedule(new java.util.TimerTask() { @Override @@ -66,7 +78,8 @@ public void run() { } }, TURN_TIME_LIMIT * 1000); } - +/* + @SuppressWarnings("static-access") private void presentOptions(Player currentPlayer) { word.getWordProgress(wordsforgame, correctguesses); System.out.println("\ncurrent word(s) to guess:" + wordsforgame); @@ -79,8 +92,9 @@ private void presentOptions(Player currentPlayer) { } private int getPlayerChoice() { - Scanner scanner = new Scanner(System.in); - return scanner.nextInt(); + int choice = scanner.nextInt(); + System.out.println("Player choice: " + choice); + return choice; } private void handleChoice(Player currentPlayer, int choice) { @@ -99,13 +113,13 @@ private void handleChoice(Player currentPlayer, int choice) { advanceTurn(); break; } - } + }*/ - private void buyVowel(Player currentPlayer) { - boolean validVowel = false; + public void buyVowel(Player currentPlayer, char vowel) { + /*boolean validVowel = false; while (!validVowel) { - System.out.println("Enter a vowel to buy:"); - char vowel = getUserInput(); + //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); @@ -114,14 +128,23 @@ private void buyVowel(Player currentPlayer) { } 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."); } } - private void selectConsonant(Player currentPlayer) { - boolean validConsonant = false; + public void selectConsonant(Player currentPlayer, char consonant) { + /*boolean validConsonant = false; while (!validConsonant) { - System.out.println("Enter a consonant to select:"); - char consonant = getUserInput(); + //System.out.println("Enter a consonant to select:"); + //char consonant = getUserInput(); if ("aeiou".indexOf(consonant) == -1 && !currentPlayer.hasGuessedConsonant(consonant)) { validConsonant = true; currentPlayer.guessConsonant(consonant); @@ -129,21 +152,26 @@ private void selectConsonant(Player currentPlayer) { } 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."); } } - private void solvePuzzle(Player currentPlayer) { - System.out.println("Enter the solution:"); - Scanner scanner = new Scanner(System.in); - String solution = scanner.nextLine(); - //String solutions = String.join("", solution); - //System.out.println("\nwhat you typed: " + solution); + 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) == true) { - //if(correctguesses.equals(lettersinword)){ + 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(); @@ -151,10 +179,12 @@ private void solvePuzzle(Player currentPlayer) { } private char getUserInput() { - Scanner scanner = new Scanner(System.in); - return scanner.next().charAt(0); + 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); if (isCorrect == 1) { @@ -163,7 +193,6 @@ private void processGuess(Player currentPlayer, char guessedLetter) { currentPlayer.addScore(points); correctguesses.add(guessedLetter); System.out.println("Player " + currentPlayer.getName() + " awarded " + points + " points."); - //word.getWordProgress(wordsforgame, correctguesses); System.out.println("\ncorrect guesses: " + correctguesses); if (correctguesses.equals(lettersinword)) { System.out.println("Word guessed correctly! Round over."); @@ -205,13 +234,22 @@ public ArrayList getWordsForGame() { return wordsforgame; } - 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); + List players = new ArrayList<>(); - Game game = new Game(players, "src/test/resources/test_words.txt", "src/test/resources/test_stakes.txt", new Statistics()); + // Simulated user input for two rounds + String simulatedUserInput = "3\n" + "testword\n" + "3\n" + "testword\n" + "3\n" + "testword\n"; + ByteArrayInputStream inputStream = new ByteArrayInputStream(simulatedUserInput.getBytes()); + //System.setIn(inputStream); + Scanner scanner = new Scanner(System.in); + + Game game = new Game(players, "src/test/resources/test_words.txt", "src/test/resources/test_stakes.txt", new Statistics(), scanner); game.addPlayer(new Player("Player1", PlayerType.HUMAN)); game.addPlayer(new Player("Player2", PlayerType.HUMAN)); game.startGame(); System.exit(0); - } + }*/ }