diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java index 1efddc7..b879052 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/VoteHandler.java @@ -23,15 +23,17 @@ public class VoteHandler { public static final Logger LOGGER = LogManager.getLogger(); public static final BudaLogConfig l = new BudaLogConfig(LOGGER); - /** - * TODO(Alex): Documentation - * @param passengers + * Handles the ghost vote during nighttime: passengers who are ghosts are being asked on who to + * ghostify, others are waiting. Results are being collected and the player with most votes is + * being ghostified. + * + * @param passengers: passengers on the train */ public void ghostVote(Passenger[] passengers, Game game) { - - // array to collect votes for all players during voting, i.e. votes for player 1 (passengers[0]) are saved in + // array to collect votes for all players during voting, i.e. votes for player 1 (passengers[0]) + // are saved in // votesForPlayers[0] int[] votesForPlayers = new int[6]; @@ -42,7 +44,9 @@ public class VoteHandler { LOGGER.info("Send msg to Ghost in Position: " + passenger); passenger.send("Vote on who to ghostify!"); } else { - passenger.send("Please wait, ghosts are active"); //TODO(Seraina): make sure whatever clients send in this time, except chat is ignored + passenger.send( + "Please wait, ghosts are active"); // TODO(Seraina): make sure whatever clients send in + // this time, except chat is ignored LOGGER.info("Send msg to Human in Position: " + passenger); } } @@ -56,6 +60,7 @@ public class VoteHandler { for (int i = 0; i < votesForPlayers.length; i++) { if (passenger.getVote() == i) { votesForPlayers[i]++; + LOGGER.info(passengers[i] + " has received the most votes"); } } } @@ -64,9 +69,9 @@ public class VoteHandler { // count the votes - determine which player has the most votes by going through the // votesForPlayers array int currentMax = 0; - for (int i = 0; i < votesForPlayers.length; i++) { - if (votesForPlayers[i] > currentMax) { - currentMax = votesForPlayers[i]; + for (int votesForPlayer : votesForPlayers) { + if (votesForPlayer > currentMax) { + currentMax = votesForPlayer; } } LOGGER.info("Most votes" + currentMax); @@ -74,25 +79,27 @@ public class VoteHandler { // ghostify the player with most votes int ghostPosition = 0; for (int i = 0; i < votesForPlayers.length; i++) { - if (votesForPlayers[i] == currentMax) { // if player has most votes + if (votesForPlayers[i] == currentMax) { // if player at position i has most votes ghostPosition = i; LOGGER.info("Most votes for Passenger" + i); - } } GhostifyHandler gh = new GhostifyHandler(); - Ghost g = gh.ghost(passengers[ghostPosition],game); + Ghost g = gh.ghost(passengers[ghostPosition], game); passengers[ghostPosition] = g; passengers[ghostPosition].send( "You are now a ghost!"); // TODO: ServerGameInfoHandler might deal with this one } /** - * TODO(Alex): Documentation - * @param passengers + * Handles the human vote during daytime. Asks human players to vote for a ghost to kick out while + * ghosts are waiting. Votes are being collected, vote results are being handled in three possible + * ways: if passenger who was voted for is a human, continue with next ghost vote; if it's a + * normal ghost, kick him off; if it's the OG ghost, end game, humans win. + * + * @param passengers: train passengers */ public void humanVote(Passenger[] passengers) { - // very similar to ghostVote, differs mostly in the way votes are handled // array to collect votes for all players during voting, i.e. votes for player 1 are saved in // votesForPlayers[0] @@ -126,6 +133,7 @@ public class VoteHandler { for (int votesForPlayer : votesForPlayers) { if (votesForPlayer > currentMax) { currentMax = votesForPlayer; + LOGGER.info("Max amount of votes: " + currentMax); } } // deal with voting results @@ -133,17 +141,39 @@ public class VoteHandler { for (int i = 0; i < votesForPlayers.length; i++) { if (votesForPlayers[i] == currentMax) { // if player has most votes voteIndex = i; - + LOGGER.info("Player " + voteIndex + "has the most votes"); } } - if (!passengers[voteIndex].getIsGhost()) { // if player with most votes is human, notify everyone about it + if (!passengers[voteIndex] + .getIsGhost()) { // if player with most votes is human, notify everyone about it for (Passenger passenger : passengers) { passenger.send( "You voted for a human!"); // TODO: ServerGameInfoHandler might be better to use here } } if (passengers[voteIndex].getIsGhost()) { // if player is a ghost - if (passengers[voteIndex].i + if (passengers[voteIndex].getIsOG()) { // if ghost is OG --> end game, humans win + System.out.println("Game over: humans win!"); // TODO: correctly handle end of game + } else { + /* Special case: if ghost is not OG and if only one human is left (--> last human didn't vote for OG ghost), + ghosts win. + */ + int humans = 0; // variable to count the amount of humans left in the game + for (Passenger passenger : passengers) { + if (!passenger.getIsGhost()) { + humans++; + } + if (humans == 1) { + System.out.println("Game over: ghosts win!"); + } + } + // Usual case: there is more than one human left and a normal ghost has been voted for --> + // kick this ghost off + passengers[voteIndex].setKickedOff(true); + for (Passenger passenger : passengers) { + passenger.send("Player " + voteIndex + "has been kicked off!"); + } + } } } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Ghost.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Ghost.java index 4456946..2b7a53b 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Ghost.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Ghost.java @@ -8,11 +8,9 @@ public class Ghost extends Passenger { public static final Logger LOGGER = LogManager.getLogger(); public static final BudaLogConfig l = new BudaLogConfig(LOGGER); - protected boolean isOG = false; //true if the Ghost is the original ghost false by default. - public boolean getIsOG() { return isOG; } - public void setIsOG(boolean og) { isOG = og; }; + public void setIsOG(boolean og) { isOG = og; } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java index aa434b7..f584eee 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/gamelogic/klassenstruktur/Passenger.java @@ -11,9 +11,10 @@ public class Passenger { protected int position; //the player's Cabin number (0 to 5) protected String name; //the player's Name - protected Boolean isGhost; //boolean regarding if the player is a ghost. Could probably be removed since ghost is a subclass but I'm keeping it in. - protected Boolean isPlayer; //same here - protected Boolean kickedOff; //true if the player has been voted off. + protected boolean isGhost; //boolean regarding if the player is a ghost. Could probably be removed since ghost is a subclass but I'm keeping it in. + protected boolean isOG = false; //true if the player is the original ghost, false by default. + protected boolean isPlayer; //same here + protected boolean kickedOff; //true if the player has been voted off. protected ClientHandler clientHandler;//the socket for the client associated with this Passenger, for NPCs, this can be null. protected boolean hasVoted; //true if the player gave his vote during voting time protected int vote; //saves the number of the player this passenger voted for during voting (0-5) @@ -25,8 +26,7 @@ public class Passenger { **/ public void send(String msg) { //todo(Seraina): send protocol message to the respective client OR process messages for NPCS - int voteRandmom = (int) (Math.random() * 6); - this.vote = voteRandmom; + this.vote = (int) (Math.random() * 6); } /** @@ -74,21 +74,23 @@ public class Passenger { return name; } - public Boolean getIsGhost() { + public boolean getIsGhost() { return isGhost; } - public Boolean getKickedOff() { + public boolean getIsOG() { return isOG; } + + public boolean getKickedOff() { return kickedOff; } - public Boolean getIsPlayer() { + public boolean getIsPlayer() { return isPlayer; } - public boolean getHasVoted() { return hasVoted; }; // returns true if player already voted during a voting + public boolean getHasVoted() { return hasVoted; } // returns true if player already voted during a voting - public int getVote() { return vote; } + public int getVote() { return vote; } // returns who this passenger voted for during a voting public ClientHandler getClientHandler() { return clientHandler;