Skip to content

Commit

Permalink
update cse3310 with fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Antenehden committed Aug 6, 2024
1 parent 0a2b614 commit 9c21f1a
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void startGame() {
System.out.println("back in game");
isGameActive = true;
startNextRound();
System.out.println("finished start next round method");

if(i == 2){
isGameActive = false;
Expand Down
210 changes: 128 additions & 82 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,7 +24,6 @@ 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 @@ -56,7 +55,9 @@ 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");
}

public void nextTurn() {
Expand All @@ -72,7 +73,7 @@ public void nextTurn() {
waitingForInput = true;

synchronized (turnLock) {
new java.util.Timer().schedule(new java.util.TimerTask() {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
synchronized (turnLock) {
Expand All @@ -81,6 +82,7 @@ public void run() {
currentPlayer.getTimer().stop();
waitingForInput = false;
turnLock.notifyAll();
System.out.println("Reached inside loop");
advanceTurn();
}
}
Expand All @@ -105,112 +107,41 @@ 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;
System.out.println("going to next round");
playerActionTaken(); // Notify the waiting thread
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 @@ -220,10 +151,11 @@ 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 @@ -260,8 +192,121 @@ 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);
/*public static void main(String[] args) throws IOException {
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 {
List<String> testwords = Arrays.asList("testword");
WordList wordlist = new WordList(testwords);
Expand All @@ -278,5 +323,6 @@ public ArrayList<String> getWordsForGame() {
game.addPlayer(new Player("Player2", PlayerType.HUMAN));
game.startGame();
System.exit(0);
}*/
}
}
*/

0 comments on commit 9c21f1a

Please sign in to comment.