Fixed some bugs now the VoteHanlder does what it is supposed to

This commit is contained in:
Seraina 2022-04-08 12:42:02 +02:00
parent 5cfa6809a2
commit 4337583de6
4 changed files with 37 additions and 13 deletions

View File

@ -38,7 +38,7 @@ public class GameFunctions {
for (int i = 0; i < nrOfPlayers; i++) {
Human h = new Human();
h.setPosition(train.orderOfTrain[i]);
passengerTrain[i] = h;
passengerTrain[train.orderOfTrain[i]] = h;
}
this.passengerTrain = passengerTrain;
}

View File

@ -1,6 +1,7 @@
package ch.unibas.dmi.dbis.cs108.gamelogic;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.GhostPlayer;
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
import org.apache.logging.log4j.LogManager;
@ -16,10 +17,13 @@ public class GhostifyHandler {
* @param p Passenger to be ghostified
*/
public GhostPlayer ghost(Passenger p, Game game) {
public Ghost ghost(Passenger p, Game game) { //TODO: Adjust for not only players but also npcs
LOGGER.debug("Passenger Position " + p.getPosition());
p.setGhost();
GhostPlayer g;
g = new GhostPlayer(p.getPosition(), p.getName(), p.getClientHandler(), false);
Ghost g;
g = new Ghost();
g.setGhost();
g.setPosition(p.getPosition());
game.gameFunctions.passengerTrain[g.getPosition()] = g;
LOGGER.info("Passenger at position " + p.getPosition() + "has been ghostified");
return g;

View File

@ -83,8 +83,10 @@ public class VoteHandler {
if (votesForPlayers[i] == currentMax) { // if player at position i has most votes
ghostPosition = i;
LOGGER.info("Most votes for Passenger " + i);
}
}
LOGGER.debug("ghostPosition: " + ghostPosition);
GhostifyHandler gh = new GhostifyHandler();
Ghost g = gh.ghost(passengers[ghostPosition], game);
passengers[ghostPosition] = g;
@ -170,10 +172,10 @@ public class VoteHandler {
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);
@ -192,10 +194,14 @@ public class VoteHandler {
System.out.println();
String[] print = new String[6];
for (int i = 0; i < array.length; i++) {
if(array[i].getIsGhost()) {
print[i] = "| ghost |";
if (array[i].getKickedOff()) {
print[i] = "| kicked off " + array[i].getPosition() + "|";
} else {
print[i] = "| human |";
if (array[i].getIsGhost()) {
print[i] = "| ghost " + array[i].getPosition() + "|";
} else {
print[i] = "| human " + array[i].getPosition() + "|";
}
}
}
@ -215,6 +221,7 @@ public class VoteHandler {
testArray[3] = ghost;
testArray[3].setGhost();
testArray[3].setIsOg();
testArray[3].setPosition(3);
print(testArray);
LOGGER.info("NIGHT");
ghostVote(testArray,game);
@ -224,6 +231,13 @@ public class VoteHandler {
humanVote(testArray, game);
print(testArray);
LOGGER.info("NIGHT");
ghostVote(testArray,game);
print(testArray);
LOGGER.info("Day");
humanVote(testArray, game);
print(testArray);
} catch (TrainOverflow e) {
LOGGER.warn(e.getMessage());
}

View File

@ -19,17 +19,23 @@ public class Passenger {
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)
int sendcounter = 0;
/**
* Sends a protocol message to the respective player or NPC.
* @param msg the message that is sent to this player.
**/
public void send(String msg) {
if (msg.equals("Vote on who to ghostify!") || msg.equals("Vote for a ghost to kick off!")) {
vote = 1;
sendcounter++;
if (msg.equals("Vote on who to ghostify!")) {
vote = 1*sendcounter;
hasVoted = true; // for testing, when is it set to false again?
LOGGER.info("Voted for Position " + vote);
} else if(msg.equals("Vote for a ghost to kick off!")) {
vote = (int) (0.5*sendcounter);
hasVoted = true; // for testing, when is it set to false again?
LOGGER.info("Voted for Position " + vote);
} else {
LOGGER.debug(msg);
}
/*if (isPlayer) {