Implemented a run method in game, that goes through a game until it ends
This commit is contained in:
parent
f878bee40d
commit
5b4a06dd75
@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic;
|
|||||||
|
|
||||||
|
|
||||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
|
||||||
import org.apache.logging.log4j.*;
|
import org.apache.logging.log4j.*;
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
@ -15,6 +16,8 @@ public class Game {
|
|||||||
protected int nrOfGhosts; // sets how many Ghosts we start witch
|
protected int nrOfGhosts; // sets how many Ghosts we start witch
|
||||||
protected int nrOfUsers; // safes how many clients are active in this Game
|
protected int nrOfUsers; // safes how many clients are active in this Game
|
||||||
protected GameFunctions gameFunctions;
|
protected GameFunctions gameFunctions;
|
||||||
|
protected boolean isDay; //false means it is night, it is night by default
|
||||||
|
protected VoteHandler voteHandler;
|
||||||
//TODO: Figure out where Day/Night game state is saved maybe think about a game state class or smt.
|
//TODO: Figure out where Day/Night game state is saved maybe think about a game state class or smt.
|
||||||
/**
|
/**
|
||||||
* Constructs a Game instance where:
|
* Constructs a Game instance where:
|
||||||
@ -47,12 +50,35 @@ public class Game {
|
|||||||
return nrOfUsers;
|
return nrOfUsers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setDay(boolean day) {
|
||||||
|
isDay = day;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(ClientHandler clientHandler) {
|
||||||
|
int i = 0;
|
||||||
|
String gameOverCheck = "";
|
||||||
|
while (true) { //ToDo: was ist die Abbruchbedingung? VoteHandler muss das schicken.
|
||||||
|
if (!isDay) {
|
||||||
|
voteHandler.ghostVote(gameFunctions.getPassengerTrain(), this);
|
||||||
|
setDay(true);
|
||||||
|
} else {
|
||||||
|
gameOverCheck = voteHandler.humanVote(gameFunctions.getPassengerTrain(), this);
|
||||||
|
}
|
||||||
|
if (gameOverCheck.equals("Game over: ghosts win!") || gameOverCheck.equals("Game over: humans win!")){
|
||||||
|
clientHandler.broadcastAnnouncement(gameOverCheck);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
Game game1 = new Game(6, 1, 1);
|
Game game1 = new Game(6, 1, 1);
|
||||||
|
|
||||||
|
|
||||||
} catch (TrainOverflow e) {
|
} catch (TrainOverflow e) {
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.gamelogic;
|
package ch.unibas.dmi.dbis.cs108.gamelogic;
|
||||||
|
|
||||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost;
|
||||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Human;
|
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Human;
|
||||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
|
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
@ -13,11 +14,11 @@ public class GameFunctions {
|
|||||||
/**
|
/**
|
||||||
* Can be extended for optional Game-settings
|
* Can be extended for optional Game-settings
|
||||||
**/
|
**/
|
||||||
int nrOfPlayers; //sets the length of the train
|
protected int nrOfPlayers; //sets the length of the train
|
||||||
int nrOfGhosts; // sets how many Ghosts we start witch
|
protected int nrOfGhosts; // sets how many Ghosts we start witch
|
||||||
int nrOfUsers; // safes how many clients are active in this Game
|
protected int nrOfUsers; // safes how many clients are active in this Game
|
||||||
Train train; // safes who sits where
|
protected Train train; // safes who sits where
|
||||||
public Passenger[] passengerTrain;
|
protected Passenger[] passengerTrain;
|
||||||
protected Game game;
|
protected Game game;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,10 +38,18 @@ public class GameFunctions {
|
|||||||
this.train = new Train(nrOfPlayers, nrOfUsers);
|
this.train = new Train(nrOfPlayers, nrOfUsers);
|
||||||
Passenger[] passengerTrain = new Passenger[nrOfPlayers]; //Creates an array with Passengers with correlation positions (Train)
|
Passenger[] passengerTrain = new Passenger[nrOfPlayers]; //Creates an array with Passengers with correlation positions (Train)
|
||||||
for (int i = 0; i < nrOfPlayers; i++) {
|
for (int i = 0; i < nrOfPlayers; i++) {
|
||||||
|
if (i == 3) {
|
||||||
|
Ghost g = new Ghost(game);
|
||||||
|
g.setPosition(train.orderOfTrain[i]);
|
||||||
|
g.setIsOG(true);
|
||||||
|
passengerTrain[train.orderOfTrain[i]] = g;
|
||||||
|
} else {
|
||||||
Human h = new Human(game);
|
Human h = new Human(game);
|
||||||
h.setPosition(train.orderOfTrain[i]);
|
h.setPosition(train.orderOfTrain[i]);
|
||||||
passengerTrain[train.orderOfTrain[i]] = h;
|
passengerTrain[train.orderOfTrain[i]] = h;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.passengerTrain = passengerTrain;
|
this.passengerTrain = passengerTrain;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,6 +65,7 @@ public class GameFunctions {
|
|||||||
return nrOfUsers;
|
return nrOfUsers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Passenger[] getPassengerTrain() {
|
||||||
|
return passengerTrain;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
package ch.unibas.dmi.dbis.cs108.gamelogic;
|
package ch.unibas.dmi.dbis.cs108.gamelogic;
|
||||||
|
|
||||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Ghost;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.GhostNPC;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.HumanNPC;
|
||||||
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
|
import ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur.Passenger;
|
||||||
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
|
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
@ -37,27 +40,32 @@ public class ServerGameInfoHandler {
|
|||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static void ghostNpcParser(GhostNPC npc, String msg, Game game) {
|
||||||
* TODO(Seraina): Handle NPC's Maybe do that in Passenger send methode!
|
switch (msg) {
|
||||||
* Send a message "GVOTR" to a passenger urging them to vote for a human to infect
|
case "noise":
|
||||||
* Currently only handles only Players, so send a message to corresponding client
|
npc.noise();
|
||||||
* @param passenger the passenger the message is meant to, should be a Ghost
|
break;
|
||||||
*/
|
case "Vote on who to ghostify!":
|
||||||
public void sendVoteRequestGhosts(Passenger passenger){
|
npc.vote(game);
|
||||||
passenger.getClientHandler().sendMsgToClient("GVOTR");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO(Seraina): Handle NPC's
|
|
||||||
* Send a message "HVOTR" to a passenger urging them to vote for sm to kick off the train.
|
|
||||||
* Currently only handles only Players, so send a message to corresponding client
|
|
||||||
* @param passenger the passenger the message is meant to, can be either human or ghost
|
|
||||||
*/
|
|
||||||
public void sendVoteRequestHumans(Passenger passenger){
|
|
||||||
passenger.getClientHandler().sendMsgToClient("HVOTR");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void humanNpcParser(HumanNPC npc, String msg, Game game) {
|
||||||
|
switch (msg) {
|
||||||
|
case "noise":
|
||||||
|
npc.noise();
|
||||||
|
break;
|
||||||
|
case "Vote for a ghost to kick off!":
|
||||||
|
npc.vote();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ServerGameInfoHandler s = new ServerGameInfoHandler();
|
ServerGameInfoHandler s = new ServerGameInfoHandler();
|
||||||
s.format("jhbvdwfzu");
|
s.format("jhbvdwfzu");
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
|
|||||||
|
|
||||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||||
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
|
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
|
||||||
import ch.unibas.dmi.dbis.cs108.gamelogic.TrainOverflow;
|
import ch.unibas.dmi.dbis.cs108.gamelogic.TrainOverflow;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@ -32,13 +33,18 @@ public class GhostNPC extends Ghost {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void send(String msg) {
|
||||||
|
ServerGameInfoHandler.ghostNpcParser(this, msg, game);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets vote of this Ghost position on a number between 0 and 5,
|
* Sets vote of this Ghost position on a number between 0 and 5,
|
||||||
* but only for positions where there aren't any ghosts and sets hasVoted to true
|
* but only for positions where there aren't any ghosts and sets hasVoted to true
|
||||||
*/
|
*/
|
||||||
public void vote(Game game){
|
public void vote(Game game){
|
||||||
int ghostCounter = 0;
|
int ghostCounter = 0;
|
||||||
Passenger[] train = game.getGameFunctions().passengerTrain;
|
Passenger[] train = game.getGameFunctions().getPassengerTrain();
|
||||||
for(Passenger passenger : train) {
|
for(Passenger passenger : train) {
|
||||||
if(passenger.isGhost) {
|
if(passenger.isGhost) {
|
||||||
ghostCounter++;
|
ghostCounter++;
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
|
|||||||
|
|
||||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||||
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
|
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
|
||||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
|
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@ -34,7 +35,10 @@ public class GhostPlayer extends Ghost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
public void send(String msg) {
|
public void send(String msg) {
|
||||||
//todo(Jonas): pass message along to client.
|
String formattedMsg = ServerGameInfoHandler.format(msg);
|
||||||
|
clientHandler.sendMsgToClient(formattedMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
|
|||||||
|
|
||||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||||
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
|
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
@ -29,6 +30,11 @@ public class HumanNPC extends Human {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void send(String msg) {
|
||||||
|
ServerGameInfoHandler.humanNpcParser(this, msg, game);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Currently returns a random integer for voting
|
* Currently returns a random integer for voting
|
||||||
* @return integer between 0 and 5
|
* @return integer between 0 and 5
|
||||||
@ -36,4 +42,8 @@ public class HumanNPC extends Human {
|
|||||||
public int vote(){
|
public int vote(){
|
||||||
return (int) (Math.random()*6);
|
return (int) (Math.random()*6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void noise() {
|
||||||
|
clientHandler.broadcastChatMessage("I heard some noise tonight");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package ch.unibas.dmi.dbis.cs108.gamelogic.klassenstruktur;
|
|||||||
|
|
||||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||||
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
|
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.gamelogic.ServerGameInfoHandler;
|
||||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
|
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@ -31,5 +32,9 @@ public class HumanPlayer extends Human {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void send(String msg) {
|
||||||
|
String formattedMsg = ServerGameInfoHandler.format(msg);
|
||||||
|
clientHandler.sendMsgToClient(formattedMsg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,33 +32,11 @@ public class Passenger {
|
|||||||
* @param msg the message that is sent to this player.
|
* @param msg the message that is sent to this player.
|
||||||
**/
|
**/
|
||||||
public void send(String msg) {
|
public void send(String msg) {
|
||||||
/*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) {
|
if (isPlayer) {
|
||||||
String formattedMsg = ServerGameInfoHandler.format(msg);
|
String formattedMsg = ServerGameInfoHandler.format(msg);
|
||||||
clientHandler.sendMsgToClient(formattedMsg);
|
clientHandler.sendMsgToClient(formattedMsg);
|
||||||
} else { //is a NPC
|
|
||||||
if(isGhost) {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} else { //is a human
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: call a method that identifies message for NPC and calls respective methode NPCParser
|
|
||||||
}
|
}
|
||||||
|
LOGGER.warn("This object should not just be a passenger");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user