Skip to content

Commit

Permalink
update to working ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Antenehden authored Aug 9, 2024
1 parent f7da380 commit 8ac99d0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 109 deletions.
7 changes: 4 additions & 3 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
System.out.println("< " + jsonString);
broadcastToGame(game, jsonString); // Broadcast to the specific game

if (game.getPlayers().size() == 2) {
/*if (game.getPlayers().size() == 2) {
game.startGame();
}
}*/
}

@Override
Expand Down Expand Up @@ -137,6 +137,7 @@ public void onStart() {
}

private void broadcastToGame(Game game, String message) {
System.out.println("Broadcasting to game: " + game.getGameId());
for (WebSocket conn : getConnections()) { // Changed from connections() to getConnections()
if (conn.getAttachment() == game) {
conn.send(message);
Expand All @@ -160,4 +161,4 @@ public static void main(String[] args) {
webSocketServer.start();
System.out.println("WebSocket server started on port: " + wsPort);
}
}
}
16 changes: 10 additions & 6 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import org.java_websocket.WebSocket;

import com.google.gson.Gson;

public class Game {
private static final int MAX_PLAYERS = 4;
private List<Player> players;
Expand Down Expand Up @@ -83,7 +85,7 @@ public void update(UserEvent event) {
System.err.println("Received null event");
return;
}

String action = event.getAction();
if (action == null) {
System.err.println("Event action is null");
Expand All @@ -92,24 +94,26 @@ public void update(UserEvent event) {

Round currentRound = rounds.get(currentRoundIndex);
Player currentPlayer = currentRound.getCurrentPlayer();

switch (action) {
case "BUY_VOWEL":
currentRound.buyVowel(currentPlayer, event.getValue().charAt(0));
currentRound.waitingForInput = false;
break;
case "SELECT_CONSONANT":
currentRound.selectConsonant(currentPlayer, event.getValue().charAt(0));
currentRound.waitingForInput = false;
break;
case "SOLVE_PUZZLE":
currentRound.solvePuzzle(currentPlayer, event.getValue());
currentRound.waitingForInput = false;
break;
default:
System.out.println("Unknown action: " + action);
}

// Debugging log
System.out.println("Updated game state: " + new Gson().toJson(this));
}




public void startGame() {
Expand Down Expand Up @@ -211,4 +215,4 @@ public void resetGame() {
public boolean isGameActive() {
return isGameActive;
}
}
}
120 changes: 20 additions & 100 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 @@ -57,7 +56,7 @@ public Round(List<Player> players, WordList wordlist, String stakeFilePath, Scan
public void startRound() {
this.isRoundActive = true;
nextTurn();
System.out.println("returning after nextTurn");
System.out.println("Returning after nextTurn");
}

public void nextTurn() {
Expand All @@ -73,7 +72,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 @@ -82,7 +81,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,128 +106,60 @@ 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);
correctguesses.addAll(lettersinword); // Ensure all letters are marked as correct
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);
lettersguessed.add(guessedLetter); // Add guessed letter to the list of guessed letters
if (isCorrect == 1) {
System.out.println("Correct guess!");
int points = stake.calculatePoints(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();
//nextTurn();
}
} else {
System.out.println("Incorrect guess.");
Expand Down Expand Up @@ -263,22 +194,11 @@ public ArrayList<String> getWordsForGame() {
return wordsforgame;
}

/*public static void main(String[] args) throws IOException {
List<String> testwords = Arrays.asList("testword");
WordList wordlist = new WordList(testwords);
List<Player> players = new ArrayList<>();
// Simulated user input for two rounds
String simulatedUserInput = "3\n" + "testword\n" + "3\n" + "testword\n" + "3\n" + "testword\n";
ByteArrayInputStream inputStream = new ByteArrayInputStream(simulatedUserInput.getBytes());
//System.setIn(inputStream);
Scanner scanner = new Scanner(System.in);
Game game = new Game(players, "src/test/resources/test_words.txt", "src/test/resources/test_stakes.txt", new Statistics(), scanner);
public HashSet<Character> getCorrectGuesses() {
return correctguesses;
}

game.addPlayer(new Player("Player1", PlayerType.HUMAN));
game.addPlayer(new Player("Player2", PlayerType.HUMAN));
game.startGame();
System.exit(0);
}*/
public HashSet<Character> getLettersGuessed() {
return lettersguessed;
}
}

0 comments on commit 8ac99d0

Please sign in to comment.