Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
RamonTorresUta committed Aug 2, 2024
1 parent 41c4fb3 commit 397d1ed
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 69 deletions.
67 changes: 54 additions & 13 deletions src/main/java/uta/cse3310/Round.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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--;
}
Expand All @@ -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<String> wordsforgame = new ArrayList<>(wordlist.getArrList());//storing the words in a new array list
//HashSet<Character> lettersinword = wordlist.findletters(wordsforgame);
//HashSet<Character> lettersguessed = new HashSet<>();
//HashSet<Character> correctguesses = new HashSet<>();



public Round(List<Player> players, String wordFilePath, String stakeFilePath) throws IOException {
public Round(List<Player> 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<String> 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<String> 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());
Expand Down Expand Up @@ -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");
Expand All @@ -131,19 +151,23 @@ 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);
}
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);
Expand All @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down
73 changes: 67 additions & 6 deletions src/main/java/uta/cse3310/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static void checksolutionandreveal(ArrayList<String> wordsforgame, char l
public static void getWordProgress(ArrayList<String> wordsforgame, HashSet<Character> 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)))
Expand All @@ -77,7 +77,7 @@ public static void getWordProgress(ArrayList<String> wordsforgame, HashSet<Chara
System.out.print("_");
}
}

System.out.println();
}


Expand All @@ -91,6 +91,71 @@ public static void winner(ArrayList<String> 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;
}



}


Expand Down Expand Up @@ -176,10 +241,6 @@ public boolean isFullyGuessed() {
}
return true;
}
public void reset() {
selectRandomWord();
}
}
*/

80 changes: 30 additions & 50 deletions src/main/java/uta/cse3310/WordList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<Character> 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<Character> lettersguessed = new HashSet<>();
HashSet<Character> 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<String> randomwords = new ArrayList<>();
Expand Down Expand Up @@ -152,5 +102,35 @@ public static HashSet<Character> findletters(ArrayList<String> wordsforgame) {
return letters;
}




private List<String> 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());
}
}
}

0 comments on commit 397d1ed

Please sign in to comment.