Skip to content

Commit

Permalink
Update Round.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Antenehden authored Aug 7, 2024
1 parent de096b1 commit f7da380
Showing 1 changed file with 84 additions and 128 deletions.
212 changes: 84 additions & 128 deletions src/main/java/uta/cse3310/Round.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Player> players, String wordFilePath, String stakeFilePath, Scanner scanner) throws IOException {
Expand Down Expand Up @@ -55,9 +56,8 @@ public Round(List<Player> 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() {
Expand All @@ -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) {
Expand All @@ -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();
}
}
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -192,121 +262,8 @@ public Player getCurrentPlayer() {
public ArrayList<String> 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<String> testwords = Arrays.asList("testword");
WordList wordlist = new WordList(testwords);
Expand All @@ -323,6 +280,5 @@ public static void main(String[] args) throws IOException {
game.addPlayer(new Player("Player2", PlayerType.HUMAN));
game.startGame();
System.exit(0);
}
}*/
}
*/

0 comments on commit f7da380

Please sign in to comment.