diff --git a/html/index.html b/html/index.html index 26c2037..432931d 100644 --- a/html/index.html +++ b/html/index.html @@ -539,5 +539,6 @@

Leaderboard

socket.send(JSON.stringify(data)); } + diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index 7f794bc..814be1e 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -187,13 +187,16 @@ public void insertInnerMap(UserEvent U,HashMap usedColors; private ArrayList playersList = new ArrayList(); public Player winner = null; public boolean gameStatus; @@ -22,6 +23,7 @@ public Game() { this.gameMode = gameMode; this.gameID = UUID.randomUUID().toString(); gameStatus = false; + usedColors = new HashSet<>(); } public ArrayList getPlayersList() // I need to add this since Lobby class needs to access the game's player @@ -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); + } } \ No newline at end of file diff --git a/src/main/java/uta/cse3310/Player.java b/src/main/java/uta/cse3310/Player.java index 9eefb6c..d559f22 100644 --- a/src/main/java/uta/cse3310/Player.java +++ b/src/main/java/uta/cse3310/Player.java @@ -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 @@ -91,9 +95,8 @@ public void addGameWin() @Override public String toString() { - return "Player{" + - "nick=" + nick + + "nick='" + nick + '\'' + ", score=" + score + ", status=" + status + ", totalPoints=" + totalPoints +