Reformatted my Code and cleaned it up a little

This commit is contained in:
Seraina 2022-03-25 21:59:09 +01:00
parent 478bd71409
commit 18acef0a2e
5 changed files with 150 additions and 150 deletions

View File

@ -5,7 +5,7 @@ int spielerzahl = 6; //Default setting
Fill train -> Randomize Fill train -> Randomize
/** /**
Creats an array of random numbers 1 - spielerzahl Creates an array of random numbers 1 - spielerzahl
**/ **/
int[] fillTrain(int spielerzahl) //... arbitrary amount of users, gives Array I think^^ int[] fillTrain(int spielerzahl) //... arbitrary amount of users, gives Array I think^^
if spielerzahl < #users if spielerzahl < #users

View File

@ -1,46 +1,44 @@
package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf; package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf;
import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Passenger;
import java.lang.reflect.Constructor;
public class Game { 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 * Can be extended for optional Game-settings
protected int nrOfUsers; // safes how many clients are active in this Game **/
protected GameFunctions gameFunctions; 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: * 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 nrOfPlayers is the length of the Train
* @param nrOfUsers is the number of active users at the time (non NPC's) * @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; Game(int nrOfPlayers, int nrOfGhosts, int nrOfUsers)
this.nrOfGhosts = nrOfGhosts; throws TrainOverflow { //ToDo: Who handles Exception how and where
this.nrOfUsers = nrOfUsers; this.nrOfPlayers = nrOfPlayers;
this.gameFunctions = new GameFunctions(nrOfPlayers,nrOfGhosts,nrOfUsers); this.nrOfGhosts = nrOfGhosts;
} this.nrOfUsers = nrOfUsers;
this.gameFunctions = new GameFunctions(nrOfPlayers, nrOfGhosts, nrOfUsers);
}
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);
for(int j = 0; j< game1.nrOfPlayers; j++) { for (int j = 0; j < game1.nrOfPlayers; j++) {
System.out.println(game1.gameFunctions.passengerTrain[j].getPosition()); System.out.println(game1.gameFunctions.passengerTrain[j].getPosition());
} }
} catch (TrainOverflow e) {
System.out.println(e.getMessage());
}
} catch (TrainOverflow e) {
System.out.println(e.getMessage());
} }
}
} }

View File

@ -1,59 +1,61 @@
package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf; package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf;
import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Ghost;
import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Human; import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Human;
import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Passenger; import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Passenger;
public class GameFunctions { 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 * Can be extended for optional Game-settings
* @param nrOfPlayers is the length of the Train **/
* @param nrOfGhosts is the number of OG Ghosts you want to start with and int nrOfPlayers; //sets the length of the train
* @param nrOfUsers is the number of active users at the time (non NPC's) int nrOfGhosts; // sets how many Ghosts we start witch
* @throws TrainOverflow if nrOfPlayers < nrOfUsers int nrOfUsers; // safes how many clients are active in this Game
*/ Train train; // safes who sits where
GameFunctions (int nrOfPlayers, int nrOfGhosts, int nrOfUsers) throws TrainOverflow { //ToDo: where will Exception be handeled? Passenger[] passengerTrain;
this.nrOfPlayers = nrOfPlayers;
this.nrOfGhosts = nrOfGhosts; /**
this.nrOfUsers = nrOfUsers; * Constructs a GameFunctions instance where nrOfPlayers >= nrOfUsers. Fills passengerTrain with
this.train = new Train(nrOfPlayers,nrOfUsers); * only humans
Passenger[] passengerTrain = new Passenger[nrOfPlayers]; //Creates an array with Passengers with correlation positions (Train) *
for(int i = 0; i < nrOfPlayers;i++) { * @param nrOfPlayers is the length of the Train
Human h = new Human(); * @param nrOfGhosts is the number of OG Ghosts you want to start with and
h.setPosition(train.orderOfTrain[i]); * @param nrOfUsers is the number of active users at the time (non NPCs)
passengerTrain[i] = h; * @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() { public int getNrOfGhosts() {
return nrOfGhosts; return nrOfGhosts;
} }
public int getNrOfPlayers() { public int getNrOfPlayers() {
return nrOfPlayers; return nrOfPlayers;
} }
public int getNrOfUsers() { public int getNrOfUsers() {
return nrOfUsers; return nrOfUsers;
} }
public void ghostyfy(int position) {
}
public Passenger voteEval(){
return new Passenger();
}
public void ghostyfy(int position) {
}
public Passenger voteEval() {
return new Passenger();
}
} }

View File

@ -1,80 +1,79 @@
package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf; package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf;
import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Passenger;
import java.util.Arrays;
public class Train { public class Train {
int[] orderOfTrain; //gives the random order in which the passengers enter the train
int positionOfGhost;
/** int[] orderOfTrain; //gives the random order in which the passengers enter the train
* Constructs a Train with orderOfTrain of the size nrOfPlayers, filled with a Random order of the numbers 0-5. int positionOfGhost;
* 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 NPC's)
* @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? * 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 {
int[] userTrain = new int[nrOfPlayers]; if (nrOfPlayers < nrOfUsers) {
int[] check = new int[nrOfPlayers]; throw new TrainOverflow(); //ToDo: What kind of Exception must be thrown here and who handles it how?
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++;
}
}
this.orderOfTrain = userTrain;
this.positionOfGhost = (int) (nrOfPlayers/2);
} }
/** int[] userTrain = new int[nrOfPlayers];
* Checks if the key is to be found in an array int[] check = new int[nrOfPlayers];
* @param array the array to be searched int i = 0;
* @param key the value searched for int zeroCounter = 0;
* @return true if and only if key is found while (i < nrOfPlayers) {
*/ int randomNr = (int) ((Math.random() * ((nrOfPlayers)) + 0));
static public boolean isInArray(int[] array, int key) { if (randomNr == 0) {
int i = 0; if (zeroCounter == 0) {
while (i < array.length) { i++;
if (array[i] == key) { zeroCounter++;
return true;
}
i++;
}
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());
} }
}
if (!isInArray(check, randomNr)) {
userTrain[i] = randomNr;
check[randomNr] = randomNr;
i++;
}
} }
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++;
}
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());
}
}
} }

View File

@ -1,10 +1,11 @@
package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf; package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf;
public class TrainOverflow extends Exception{ public class TrainOverflow extends Exception {
private static final String message = "Too many users are logged on";
@Override private static final String message = "Too many users are logged on";
public String getMessage() {
return message; @Override
} public String getMessage() {
return message;
}
} }