Skip to content

Commit

Permalink
encountering 3 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
HeEb authored and HeEb committed Jul 26, 2024
1 parent 0ea65aa commit 0504526
Show file tree
Hide file tree
Showing 25 changed files with 672 additions and 157 deletions.
60 changes: 42 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,52 @@
</properties>

<dependencies>
<!-- Updated JUnit dependency -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>

<!-- Mockito dependency -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.7.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
</dependency>

<dependency>
<groupId>net.freeutils</groupId>
<artifactId>jlhttp</artifactId>
<version>2.6</version>
</dependency>
<!-- Existing dependencies -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
</dependency>

<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>net.freeutils</groupId>
<artifactId>jlhttp</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.4</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -97,3 +120,4 @@
</plugins>
</reporting>
</project>

76 changes: 38 additions & 38 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,48 @@ public App(int port, Draft_6455 draft) {
super(new InetSocketAddress(port), Collections.<Draft>singletonList(draft));
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
connectionId++;
System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected");

ServerEvent event = new ServerEvent();
Game game = null;

for (Game g : activeGames) {
if (g.getPlayers().size() < 4) { // Assuming a maximum of 4 players per game
game = g;
System.out.println("found a match");
break;
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
connectionId++;
System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " connected");

ServerEvent event = new ServerEvent();
Game game = null;

for (Game g : activeGames) {
if (g.getPlayers().size() < 4) { // Assuming a maximum of 4 players per game
game = g;
System.out.println("found a match");
break;
}
}

if (game == null) {
List<Player> players = new ArrayList<>(); // Add logic to initialize players
try {
game = new Game(players, "src/main/resources/words.txt", "src/main/resources/stakes.txt", new Statistics());
} catch (IOException e) {
e.printStackTrace();
return;
}
game.setGameId(gameId++);
activeGames.add(game);
System.out.println("creating a new Game");
} else {
System.out.println("joining an existing game");
if (game == null) {
List<Player> players = new ArrayList<>();
players.add(new Player("Player1")); // Initialize at least one player
try {
game = new Game(players, "src/main/resources/words.txt", "src/main/resources/stakes.txt", new Statistics());
} catch (IOException e) {
e.printStackTrace();
return;
}
game.setGameId(gameId++);
activeGames.add(game);
System.out.println("creating a new Game");
} else {
System.out.println("joining an existing game");
}

conn.setAttachment(game);
Gson gson = new Gson();
String jsonString = gson.toJson(event);
conn.send(jsonString);
System.out.println("> " + jsonString);
conn.setAttachment(game); // This line ensures that the game is attached to the connection
Gson gson = new Gson();
String jsonString = gson.toJson(event);
conn.send(jsonString);
System.out.println("> " + jsonString);

jsonString = gson.toJson(game);
System.out.println("< " + jsonString);
broadcast(jsonString);
}
jsonString = gson.toJson(game);
System.out.println("< " + jsonString);
broadcast(jsonString);
}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
Expand All @@ -94,7 +95,7 @@ public void onMessage(WebSocket conn, String message) {
System.out.println("< " + message);
Gson gson = new GsonBuilder().create();
UserEvent event = gson.fromJson(message, UserEvent.class);

Game game = conn.getAttachment();
if (game != null) {
game.update(event);
Expand All @@ -103,7 +104,6 @@ public void onMessage(WebSocket conn, String message) {
broadcast(jsonString);
}
}


@Override
public void onMessage(WebSocket conn, ByteBuffer message) {
Expand Down
33 changes: 23 additions & 10 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ public void removePlayer(WebSocket conn) {
}

public void update(UserEvent event) {
switch (event.type) {
switch (event.getAction()) {
case "PLAY":
playRound();
break;
// Add cases for other event types as needed
// Add cases for other actions as needed
default:
System.out.println("Unknown event type: " + event.type);
System.out.println("Unknown action: " + event.getAction());
}
}


public void startGame() {
System.out.println("Game started with " + players.size() + " players.");
Expand All @@ -76,23 +75,30 @@ public void playRound() {
System.out.println("Game is not active.");
return;
}

Round currentRound = rounds.get(currentRoundIndex - 1); // -1 because we incremented in startNextRound
while (currentRound.isRoundActive()) {
currentRound.nextTurn();
}

// After the round ends, start the next round
startNextRound();

if (!currentRound.isRoundActive()) {
startNextRound();
if (currentRoundIndex >= rounds.size()) {
System.out.println("All rounds completed.");
isGameActive = false;
determineWinner();
}
}
}

public void endGame(Player player) {
stats.incrementGamesPlayed();
stats.updateWinner(player);
}

public Statistics getStatistics() {
return stats;
}

private void determineWinner() {
Player winner = null;
int highestScore = 0;
Expand All @@ -102,8 +108,9 @@ private void determineWinner() {
winner = player;
}
}

if (winner != null) {
winner.setWinner(true); // Ensure that the winner is marked correctly
System.out.println("The winner is " + winner.getName() + " with a score of " + highestScore);
stats.updateWinner(winner);
} else {
Expand All @@ -122,8 +129,14 @@ public void resetGame() {
}
}
}

public boolean isGameActive() {
return isGameActive;
}
}






5 changes: 3 additions & 2 deletions src/main/java/uta/cse3310/GameTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import java.util.TimerTask;

public class GameTimer {

private Timer timer;
private transient Timer timer;
// private Timer timer;
private long startTime;
private long elapsedTime;
private boolean isRunning;

public GameTimer() {

this.timer = new Timer();
this.elapsedTime = 0;
this.isRunning = false;
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/uta/cse3310/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ public class Player {
private String id;
private String name;
private int score;
private boolean winner;
private boolean iswinner;
private GameTimer timer; // Updated reference



public boolean isWinner() {
return winner;
return iswinner;
}

public void setWinner(boolean winner) {
this.winner = winner;
public void setWinner(boolean iswinner) {
this.iswinner = iswinner;
}

public Player(String name) {
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/uta/cse3310/Round.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private String[] loadWords(String filePath) throws IOException {

public void startRound() {
System.out.println("New round started. Word to guess: " + word.getWordProgress());
this.isRoundActive = true;
}

public void nextTurn() {
Expand All @@ -36,7 +37,8 @@ public void nextTurn() {
Player currentPlayer = players.get(currentPlayerIndex);
System.out.println("Current player: " + currentPlayer.getName());

char guessedLetter = 'a'; // Replace with actual guess from player input
// Simulate a guess with random letter, replace with actual user input logic
char guessedLetter = getRandomLetter();
boolean isCorrect = word.guessLetter(guessedLetter);
if (isCorrect) {
System.out.println("Correct guess!");
Expand All @@ -52,17 +54,22 @@ public void nextTurn() {
}
}

private char getRandomLetter() {
return (char) ('a' + new java.util.Random().nextInt(26));
}

public String getCurrentWordProgress() {
return word.getWordProgress();
}

/* public String getCurrentStake() {
return stake.getCurrentStake();
}*/
// Commented out as it does not exist
// public String getCurrentStake() {
// return stake.getCurrentStake();
// }

public void resetRound() throws IOException {
this.word.reset();
// this.stake.reset();
// this.stake.reset();
this.currentPlayerIndex = 0;
this.isRoundActive = true;
}
Expand All @@ -75,3 +82,4 @@ public boolean isRoundActive() {




1 change: 1 addition & 0 deletions src/main/java/uta/cse3310/UserEvent.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package uta.cse3310;
//package org.junit.jupiter.api;

public class UserEvent {
private PlayerType playerType;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/uta/cse3310/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ public void reset() {
}
}


2 changes: 1 addition & 1 deletion src/main/java/uta/cse3310/WordList.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public List<String> getWords() {

public static void main(String[] args) {
try {
WordList wordList = new WordList("path/to/words.txt");
WordList wordList = new WordList("src/main/resources/words.txt");
System.out.println("Random word: " + wordList.getRandomWord());
} catch (IOException e) {
System.err.println("Error loading words: " + e.getMessage());
Expand Down
Binary file modified src/test/.DS_Store
Binary file not shown.
Binary file modified src/test/Java/uta/cse3310/.DS_Store
Binary file not shown.
Loading

0 comments on commit 0504526

Please sign in to comment.