Started working on some basic game logic stuff

Added a tool to randomize the order of the train (->Train)
This commit is contained in:
Seraina 2022-03-24 15:17:19 +01:00
parent 4bad0857cf
commit 66628b07cd
4 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf;
import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Passenger;
import java.lang.reflect.Constructor;
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
Train train; // safes who sits where ToDo: Figure out who and where and if this needs to be changed i.e. when a client looses connection. (Schon gelöst mit timer?)
/**
* 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 NPC's)
*/
Game (int nrOfPlayers, int nrOfGhosts, int nrOfUsers) {
this.nrOfPlayers = nrOfPlayers;
this.nrOfGhosts = nrOfGhosts;
this.nrOfUsers = nrOfUsers;
}
}

View File

@ -0,0 +1,28 @@
package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf;
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
GameFunctions (int nrOfPlayers, int nrOfGhosts, int nrOfUsers) {
this.nrOfPlayers = nrOfPlayers;
this.nrOfGhosts = nrOfGhosts;
this.nrOfUsers = nrOfUsers;
}
public int getNrOfGhosts() {
return nrOfGhosts;
}
public int getNrOfPlayers() {
return nrOfPlayers;
}
public int getNrOfUsers() {
return nrOfUsers;
}
}

View File

@ -0,0 +1,72 @@
package ch.unibas.dmi.dbis.cs108.Spiellogikentwurf;
import ch.unibas.dmi.dbis.cs108.Klassenstruktur.Passenger;
import java.util.Arrays;
public class Train {
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
* @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?
}
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++;
}
}
this.orderOfTrain = userTrain;
}
/**
* 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);
} catch (TrainOverflow e) {
System.out.println(e.getMessage());
}
}
}

View File

@ -0,0 +1,10 @@
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;
}
}