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 6, 2024
1 parent 078a71a commit 649b318
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 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.TimerTask;
//import java.util.concurrent.CountDownLatch;
import java.util.HashSet;

public class Round {
Expand All @@ -20,6 +22,9 @@ public class Round {
HashSet<Character> correctguesses = new HashSet<>();
private HashSet<Character> lettersinword;
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 @@ -64,20 +69,42 @@ public void nextTurn() {
currentPlayer.getTimer().reset();
currentPlayer.getTimer().start();
System.out.println("Current player: " + currentPlayer.getName() + " has " + TURN_TIME_LIMIT + " seconds to guess.");
waitingForInput = true;

//presentOptions(currentPlayer);
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();
advanceTurn();
}
}
}
}, TURN_TIME_LIMIT * 1000);

new java.util.Timer().schedule(new java.util.TimerTask() {
@Override
public void run() {
if (currentPlayer.getTimer().getElapsedTime() >= TURN_TIME_LIMIT) {
System.out.println("Time is up for player " + currentPlayer.getName());
currentPlayer.getTimer().stop();
advanceTurn();
while (waitingForInput) {
try {
turnLock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread interrupted: " + e.getMessage());
}
}
}, TURN_TIME_LIMIT * 1000);
}
}

public void playerActionTaken() {
synchronized (turnLock) {
waitingForInput = false;
turnLock.notifyAll();
}
}

/*
@SuppressWarnings("static-access")
private void presentOptions(Player currentPlayer) {
Expand Down Expand Up @@ -207,7 +234,7 @@ private void processGuess(Player currentPlayer, char guessedLetter) {
}
}

private void advanceTurn() {
public void advanceTurn() {
Player currentPlayer = players.get(currentPlayerIndex);
currentPlayer.getTimer().stop();
currentPlayerIndex = (currentPlayerIndex + 1) % players.size();
Expand Down

0 comments on commit 649b318

Please sign in to comment.