From 397d1ed340f2d4c34eb80b39579f3288fd809647 Mon Sep 17 00:00:00 2001 From: Ramon Torres Date: Thu, 1 Aug 2024 21:40:18 -0500 Subject: [PATCH] updated --- src/main/java/uta/cse3310/Round.java | 67 +++++++++++++++++---- src/main/java/uta/cse3310/Word.java | 73 ++++++++++++++++++++-- src/main/java/uta/cse3310/WordList.java | 80 ++++++++++--------------- 3 files changed, 151 insertions(+), 69 deletions(-) diff --git a/src/main/java/uta/cse3310/Round.java b/src/main/java/uta/cse3310/Round.java index 0675159..04549dc 100644 --- a/src/main/java/uta/cse3310/Round.java +++ b/src/main/java/uta/cse3310/Round.java @@ -4,6 +4,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Scanner; +import java.util.HashSet; + /* //MAIN FUNCTION WOULD BE LIKE THIS @@ -49,6 +51,7 @@ public static void main(String[] args) System.out.print("Enter a letter: "); String guess = scanner.next(); char letter = guess.charAt(0); + words.checksolutionandreveal(wordsforgame, letter, lettersinword, lettersguessed, correctguesses); i--; } @@ -66,19 +69,35 @@ public class Round { 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 + //what I added + //WordList wordlist = new WordList(); + //Word words = new Word(); + //wordlist.gatherwords(); + //ArrayList wordsforgame = new ArrayList<>(wordlist.getArrList());//storing the words in a new array list + //HashSet lettersinword = wordlist.findletters(wordsforgame); + //HashSet lettersguessed = new HashSet<>(); + //HashSet correctguesses = new HashSet<>(); + + - public Round(List players, String wordFilePath, String stakeFilePath) throws IOException { + public Round(List players, String wordFilePath, String stakeFilePath) throws IOException + { this.players = players; this.word = new Word(loadWords(wordFilePath)); + //WordList wordlist = new WordList(); //<-- calls file + //wordlist.gatherwords(); //<-- fills words array + //ArrayList wordsforgame = new ArrayList<>(wordlist.getArrList()); //<-- now have an array for file this.stake = new Stake(stakeFilePath); this.currentPlayerIndex = 0; this.isRoundActive = true; } + private String[] loadWords(String filePath) throws IOException { List lines = java.nio.file.Files.readAllLines(java.nio.file.Paths.get(filePath)); return lines.toArray(new String[0]); } + public void startRound() { //System.out.println("New round started. Word to guess: " + word.getWordProgress()); @@ -114,7 +133,8 @@ public void run() { } private void presentOptions(Player currentPlayer) { - System.out.println("Word progress: " + word.getWordProgress()); + //word.getWordProgress(wordsforgame, correctguesses); + System.out.println("Word progress: " + word.getWordProgress()); //getWordProgress System.out.println("Choose an option:"); System.out.println("1. Buy a vowel"); System.out.println("2. Select a constant"); @@ -131,11 +151,14 @@ private int getPlayerChoice() { return scanner.nextInt(); } + + //-------------------------here on down----------------------- private void handleChoice(Player currentPlayer, int choice) { switch (choice) { case 1: boolean validVowel = false; - while (!validVowel) { + while (!validVowel) + { System.out.println("Enter a vowel to buy:"); char vowel = getUserInput(); validVowel = buyVowel(currentPlayer, vowel); @@ -143,7 +166,8 @@ private void handleChoice(Player currentPlayer, int choice) { break; case 2: boolean validConstant = false; - while (!validConstant) { + while (!validConstant) + { System.out.println("Enter a constant to select:"); char constant = getUserInput(); validConstant = selectConstant(currentPlayer, constant); @@ -153,9 +177,12 @@ private void handleChoice(Player currentPlayer, int choice) { System.out.println("Enter the solution:"); Scanner scanner = new Scanner(System.in); String solution = scanner.nextLine(); - if (solvePuzzle(currentPlayer, solution)) { + if (solvePuzzle(currentPlayer, solution)) + { isRoundActive = false; - } else { + } + else + { advanceTurn(); } break; @@ -171,21 +198,29 @@ private char getUserInput() { return scanner.next().charAt(0); } - private void processGuess(Player currentPlayer, char guessedLetter) { + + private void processGuess(Player currentPlayer, char guessedLetter) + { boolean isCorrect = word.guessLetter(guessedLetter); - if (isCorrect) { + 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()) { + if (word.isFullyGuessed()) + { System.out.println("Word guessed correctly! Round over."); isRoundActive = false; - } else { + } + else + { currentPlayer.getTimer().reset(); nextTurn(); // Player gets another turn } - } else { + } + else + { System.out.println("Incorrect guess."); advanceTurn(); } @@ -243,22 +278,28 @@ public boolean selectConstant(Player player, char constant) { } public boolean solvePuzzle(Player player, String solution) { - if (word.solve(solution)) { + if (word.solve(solution)) + //if(solution == ) + { System.out.println(player.getName() + " solved the puzzle!"); player.addScore(10); // Award points based on solution length isRoundActive = false; return true; - } else { + } + else + { System.out.println("Incorrect solution by player " + player.getName()); advanceTurn(); return false; } } + public String getCurrentWordProgress() { return word.getWordProgress(); } +//need this public void resetRound() throws IOException { this.word.reset(); this.stake.reset(); diff --git a/src/main/java/uta/cse3310/Word.java b/src/main/java/uta/cse3310/Word.java index 963ed47..8692021 100644 --- a/src/main/java/uta/cse3310/Word.java +++ b/src/main/java/uta/cse3310/Word.java @@ -65,7 +65,7 @@ public static void checksolutionandreveal(ArrayList wordsforgame, char l public static void getWordProgress(ArrayList wordsforgame, HashSet correctguesses) { String result = String.join(" ", wordsforgame); - + System.out.println("Word progress: "); for(int i = 0; i < result.length(); i++) { if(correctguesses.contains(result.charAt(i))) @@ -77,7 +77,7 @@ public static void getWordProgress(ArrayList wordsforgame, HashSet wordsforgame) } System.out.println(); } + + //--------------------------- + public void reset() + { + selectRandomWord(); + } + + private String[] words; + private String selectedWord; + private boolean[] guessedLetters; + + public Word(String[] wordList) { + this.words = wordList; + selectRandomWord(); + } + + private void selectRandomWord() { + Random rand = new Random(); + selectedWord = words[rand.nextInt(words.length)]; + guessedLetters = new boolean[selectedWord.length()]; + } + + public String getSelectedWord() { + return selectedWord; + } + + public boolean guessLetter(char letter) { + boolean found = false; + for (int i = 0; i < selectedWord.length(); i++) { + if (selectedWord.charAt(i) == letter) { + guessedLetters[i] = true; + found = true; + } + } + return found; + } + + public String getWordProgress() { + StringBuilder progress = new StringBuilder(); + for (int i = 0; i < selectedWord.length(); i++) { + if (guessedLetters[i]) { + progress.append(selectedWord.charAt(i)); + } else { + progress.append('_'); + } + } + return progress.toString(); + } + + public boolean isFullyGuessed() { + for (boolean guessed : guessedLetters) { + if (!guessed) { + return false; + } + } + return true; + } + + public boolean solve(String solution) + { + return true; + } + + + } @@ -176,10 +241,6 @@ public boolean isFullyGuessed() { } return true; } - - public void reset() { - selectRandomWord(); - } } */ diff --git a/src/main/java/uta/cse3310/WordList.java b/src/main/java/uta/cse3310/WordList.java index 89b4899..16ddc6a 100644 --- a/src/main/java/uta/cse3310/WordList.java +++ b/src/main/java/uta/cse3310/WordList.java @@ -10,58 +10,8 @@ import java.util.Random; import java.io.BufferedReader; import java.io.FileReader; -/*------------------------------------IMPORTANT!!!!!!!!!!!!!!!-------------------------- -MAIN FUNCTION WOULD BE LIKE THIS -public class Main { - public static void main(String[] args) - { - //access to Wordlist file - WordList wordlist = new WordList(); - Word words = new Word(); - - //gathering the words - wordlist.gatherwords(); - ArrayList wordsforgame = new ArrayList<>(wordlist.getArrList());//storing the words in a new array list - - //printing out the words for verification - for (String word : wordsforgame) - { - System.out.println("word(s) selected: " + word); - } - - - //gathering letters - HashSet lettersinword = wordlist.findletters(wordsforgame); - - //showing letters - System.out.print("letters to guess: "); - for (char letters : lettersinword) - { - System.out.print(letters); - } - System.err.println(); - - - //simple guessing functionality - HashSet lettersguessed = new HashSet<>(); - HashSet correctguesses = new HashSet<>(); - int i = 17; - Scanner scanner = new Scanner(System.in); - - while ( i > 0) - { - System.out.print("Enter a letter: "); - String guess = scanner.next(); - char letter = guess.charAt(0); - words.checksolutionandreveal(wordsforgame, letter, lettersinword, lettersguessed, correctguesses); - i--; - } - scanner.close(); - } -} -*/ public class WordList { ArrayList randomwords = new ArrayList<>(); @@ -152,5 +102,35 @@ public static HashSet findletters(ArrayList wordsforgame) { return letters; } + + + + private List words; + + public WordList(String filePath) throws IOException + { + loadWords(filePath); + } + private void loadWords(String filePath) throws IOException + { + words = Files.readAllLines(Paths.get(filePath)); + } + public String getRandomWord() + { + Random rand = new Random(); + return words.get(rand.nextInt(words.size())); + } + public static void main(String[] args) + { + try + { + WordList wordList = new WordList("path/to/words.txt"); + System.out.println("Random word: " + wordList.getRandomWord()); + } + catch (IOException e) + { + System.err.println("Error loading words: " + e.getMessage()); + } + } }