From e5a64ce0c335782d7f495be25034dc3e6368f1d3 Mon Sep 17 00:00:00 2001 From: Seraina Date: Thu, 31 Mar 2022 13:12:52 +0200 Subject: [PATCH] Added Spiellogikentwurf package from LoggingExp branch --- .../dbis/cs108/Spiellogikentwurf/Game.java | 44 ++++++++++ .../Spiellogikentwurf/GameFunctions.java | 61 +++++++++++++ .../dbis/cs108/Spiellogikentwurf/Train.java | 86 +++++++++++++++++++ .../Spiellogikentwurf/TrainOverflow.java | 11 +++ 4 files changed, 202 insertions(+) create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/Game.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/GameFunctions.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/Train.java create mode 100644 src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/TrainOverflow.java diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/Game.java b/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/Game.java new file mode 100644 index 0000000..6542a4f --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/Game.java @@ -0,0 +1,44 @@ +package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf; + + +import org.apache.logging.log4j.*; + +public class Game { + + /** + * Can be extended for optional Game-settings + **/ + protected int nrOfPlayers; //sets the length of the train + protected int nrOfGhosts; // sets how many Ghosts we start witch + protected int nrOfUsers; // safes how many clients are active in this Game + protected GameFunctions gameFunctions; + /** + * Constructs a Game instance where: + * + * @param nrOfPlayers is the length of the Train + * @param nrOfGhosts is the number of OG Ghosts you want to start with and + * @param nrOfUsers is the number of active users at the time (non NPCs) + */ + Game(int nrOfPlayers, int nrOfGhosts, int nrOfUsers) + throws TrainOverflow { //ToDo: Who handles Exception how and where + this.nrOfPlayers = nrOfPlayers; + this.nrOfGhosts = nrOfGhosts; + this.nrOfUsers = nrOfUsers; + this.gameFunctions = new GameFunctions(nrOfPlayers, nrOfGhosts, nrOfUsers); + } + + + public static void main(String[] args) { + + try { + + Game game1 = new Game(6, 1, 1); + + } catch (TrainOverflow e) { + System.out.println(e.getMessage()); + } + + } + + +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/GameFunctions.java b/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/GameFunctions.java new file mode 100644 index 0000000..c48e1b2 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/GameFunctions.java @@ -0,0 +1,61 @@ +package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf; + +import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Human; +import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Passenger; + +public class GameFunctions { + + /** + * Can be extended for optional Game-settings + **/ + int nrOfPlayers; //sets the length of the train + int nrOfGhosts; // sets how many Ghosts we start witch + int nrOfUsers; // safes how many clients are active in this Game + Train train; // safes who sits where + Passenger[] passengerTrain; + + /** + * Constructs a GameFunctions instance where nrOfPlayers >= nrOfUsers. Fills passengerTrain with + * only humans + * + * @param nrOfPlayers is the length of the Train + * @param nrOfGhosts is the number of OG Ghosts you want to start with and + * @param nrOfUsers is the number of active users at the time (non NPCs) + * @throws TrainOverflow if nrOfPlayers < nrOfUsers + */ + GameFunctions(int nrOfPlayers, int nrOfGhosts, int nrOfUsers) + throws TrainOverflow { //ToDo: where will Exception be handled? + this.nrOfPlayers = nrOfPlayers; + this.nrOfGhosts = nrOfGhosts; + this.nrOfUsers = nrOfUsers; + this.train = new Train(nrOfPlayers, nrOfUsers); + Passenger[] passengerTrain = new Passenger[nrOfPlayers]; //Creates an array with Passengers with correlation positions (Train) + for (int i = 0; i < nrOfPlayers; i++) { + Human h = new Human(); + h.setPosition(train.orderOfTrain[i]); + passengerTrain[i] = h; + } + } + + public int getNrOfGhosts() { + return nrOfGhosts; + } + + public int getNrOfPlayers() { + return nrOfPlayers; + } + + public int getNrOfUsers() { + return nrOfUsers; + } + + public void ghostyfy(int position) { + + } + + public Passenger voteEval() { + return new Passenger(); + } + + +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/Train.java b/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/Train.java new file mode 100644 index 0000000..c500d98 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/Train.java @@ -0,0 +1,86 @@ +package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf; +import ch.unibas.dmi.dbis.cs108.BudaLogConfig; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class Train { + public static final Logger LOGGER = LogManager.getLogger(); + public static final BudaLogConfig l = new BudaLogConfig(LOGGER); + + int[] orderOfTrain; //gives the random order in which the passengers enter the train + int positionOfGhost; + + /** + * Constructs a Train with orderOfTrain of the size nrOfPlayers, filled with a Random order of the + * numbers 0-5. Puts the ghost approx in the middle of this order. + * + * @param nrOfPlayers sets how many Players fit in the Train + * @param nrOfUsers sets how many of the Players are Users (vs NPCs) + * @throws TrainOverflow if you want to play with to many users (Train is to small) + */ + Train(int nrOfPlayers, int nrOfUsers) throws TrainOverflow { + + if (nrOfPlayers < nrOfUsers) { + throw new TrainOverflow(); //ToDo: What kind of Exception must be thrown here and who handles it how? + } + + int[] userTrain = new int[nrOfPlayers]; + int[] check = new int[nrOfPlayers]; + int i = 0; + int zeroCounter = 0; + while (i < nrOfPlayers) { + int randomNr = (int) ((Math.random() * ((nrOfPlayers)) + 0)); + if (randomNr == 0) { + if (zeroCounter == 0) { + i++; + zeroCounter++; + } + } + if (!isInArray(check, randomNr)) { + userTrain[i] = randomNr; + check[randomNr] = randomNr; + i++; + } + + } + LOGGER.debug("A bug"); + this.orderOfTrain = userTrain; + this.positionOfGhost = nrOfPlayers / 2; + } + + /** + * Checks if the key is to be found in an array + * + * @param array the array to be searched + * @param key the value searched for + * @return true if and only if key is found + */ + static public boolean isInArray(int[] array, int key) { + int i = 0; + while (i < array.length) { + if (array[i] == key) { + return true; + } + i++; + } + LOGGER.info("An information"); + return false; + } + + public static void main(String[] args) { + int[] a = {1, 2, 3, 4, 5}; + System.out.println(isInArray(a, 0)); + //Test + try { + Train t = new Train(6, 1); + for (int i = 0; i < 6; i++) { + System.out.print("|" + t.orderOfTrain[i] + "|"); + } + System.out.println(" Ghost:" + t.positionOfGhost); + + } catch (TrainOverflow e) { + System.out.println(e.getMessage()); + } + + } +} diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/TrainOverflow.java b/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/TrainOverflow.java new file mode 100644 index 0000000..36f65d4 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/Spiellogikentwurf/TrainOverflow.java @@ -0,0 +1,11 @@ +package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf; + +public class TrainOverflow extends Exception { + + private static final String message = "Too many users are logged on"; + + @Override + public String getMessage() { + return message; + } +}