Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
muktar1907 committed May 2, 2024
2 parents f30543f + 8d0fd97 commit 63e943b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -539,5 +539,6 @@ <h2>Leaderboard</h2>
socket.send(JSON.stringify(data));

}

</script>

9 changes: 6 additions & 3 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,16 @@ public void insertInnerMap(UserEvent U,HashMap<String, HashMap<String, ArrayList
@Override
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();

if (conn != null) {
// some errors like port binding failed may not be assignable to a specific
// websocket
conn.close();
Connections.remove(conn);
} else {
System.out.println("An error occurred: " + ex.getMessage());
}
System.out.println("[ERROR] onError()");
}


@Override
public void onStart() {
System.out.println("Server has started!");
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Game // implements Chat, Timer, GridField, LeaderBoard, Wordlist
{
private int gameMode;
private String gameID;
private Set<String> usedColors;
private ArrayList<Player> playersList = new ArrayList<Player>();
public Player winner = null;
public boolean gameStatus;
Expand All @@ -22,6 +23,7 @@ public Game() {
this.gameMode = gameMode;
this.gameID = UUID.randomUUID().toString();
gameStatus = false;
usedColors = new HashSet<>();
}

public ArrayList<Player> getPlayersList() // I need to add this since Lobby class needs to access the game's player
Expand Down Expand Up @@ -182,6 +184,31 @@ public void displayRules() {

}

public boolean addPlayer(Player newPlayer) {
if (isUniquePlayerName(newPlayer.getNick())) {
if (isUniquePlayerColor(newPlayer.getColor())) {
playersList.add(newPlayer);
return true;
} else {
System.out.println("Color is already in use.");
}
} else {
System.out.println("Player name is already in use.");
}
return false;
}

private boolean isUniquePlayerName(String playerName) {
for (Player player : playersList) {
if (player.getNick().equals(playerName)) {
return false;
}
}
return true;
}

private boolean isUniquePlayerColor(String playerColor) {
return !usedColors.contains(playerColor);
}
}

13 changes: 8 additions & 5 deletions src/main/java/uta/cse3310/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ public int getScore()
}

// Method to increase player score
public void increaseScore(int points)
public void increaseScore(int points)
{
this.score += points;
this.totalPoints += points; // Assuming totalPoints accumulates all points ever scored
if (points >= 0) {
this.score += points;
this.totalPoints += points;
} else {
throw new IllegalArgumentException("Points must be non-negative.");
}
}

// Getter for player status
Expand Down Expand Up @@ -91,9 +95,8 @@ public void addGameWin()
@Override
public String toString()
{

return "Player{" +
"nick=" + nick +
"nick='" + nick + '\'' +
", score=" + score +
", status=" + status +
", totalPoints=" + totalPoints +
Expand Down

0 comments on commit 63e943b

Please sign in to comment.