package SimNed;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Simulation {
private static int Day;
private static Vector Population = new Vector();
private static Grid Playground = new Grid();
// Einlesen eines int-Wertes von der Standardeingabe:
public static int readInt() {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String eingabe = input.readLine();
Integer String_to_int = new Integer(eingabe);
return String_to_int.intValue();
} catch (Exception e) {
return 0;
}
}
// Einlesen eines char-Wertes von der Standardeingabe:
public static char readChar() {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String eingabe = input.readLine();
return eingabe.charAt(0);
} catch(Exception e) {
return '\0';
}
}
// Einlesen eines Strings
public static String readString() {
try {
BufferedReader input =
new BufferedReader(new InputStreamReader(System.in));
return input.readLine();
}
catch (Exception e) {
return "";
}
}
public static void main(String args[]) {
char choice;
System.out.print("Wieviel Nahrung soll ein NED maximal pro Tag aufheben"
+ " koennen (Default=3)? ");
String MaxFoodPickedUp = readString();
System.out.print("Wieviel Nahrung soll ein NED mindestens an einem Tag"
+ " konsumieren (Default=1)? ");
String MinFoodUsedPerDay = readString();
System.out.print("Wieviel Nahrung soll auf einem Feld pro Tag maximal"
+ " generiert werden (Default=3)? ");
String MaxFoodGenerated = readString();
System.out.print("Wieviel Nahrung soll bei einer Paarung verbraucht"
+ " werden (Default=2) ?");
String FoodUsedPerMating = readString();
if (!MaxFoodPickedUp.equals("")) Ned.MaxFoodPickedUp = Integer.valueOf(MaxFoodPickedUp).intValue();
if (!MinFoodUsedPerDay.equals("")) Ned.MinFoodUsedPerDay = Integer.valueOf(MinFoodUsedPerDay).intValue();
if (!MaxFoodGenerated.equals("")) Square.MaxFoodGenerated = Integer.valueOf(MaxFoodGenerated).intValue();
if (!FoodUsedPerMating.equals("")) Ned.FoodUsedPerMating = Integer.valueOf(FoodUsedPerMating).intValue();
System.out.print("Wollen Sie NEDs vorgeben (j/n)? ");
if (readChar() == 'j') {
do {
System.out.print("Bitte DNA eingeben ");
String dna = readString();
System.out.print("Bitte Geschlecht eingeben ");
char gender = readChar();
System.out.print("Bitte Anzahl Futter des NEDs eingeben ");
int food = readInt();
System.out.print("Bitte X-Position eingeben ");
int x = readInt();
System.out.print("Bitte Y-Position eingeben ");
int y = readInt();
System.out.print("Bitte Blickrichtung eingeben ");
int direction = readInt();
Ned NewNed = new Ned(dna, gender, food, x, y, direction);
Population.add(NewNed);
Playground.getSquares()[x][y].getNeds().add(NewNed);
System.out.print("Noch einen NED erstellen(j/n)? ");
choice = readChar();
} while(choice == 'j');
} else {
System.out.print("Wie viele NEDs sollen mit Zufallswerten erzeugt"
+ " werden? ");
int Count = readInt();
System.out.print("Wie viele davon sollen weiblich sein (Default=random)? ");
String Females = readString();
if (Females.equals("")) {
for (int i = 0; i <= Count - 1; i++) {
Ned NewNed = new Ned();
Population.add(NewNed);
Playground.getSquares()[NewNed.getXPosition()][NewNed.getYPosition()].getNeds().add(NewNed);
}
} else {
int FemaleCount = Integer.valueOf(Females).intValue();
for (int i = 0; (i < FemaleCount) && (i < Count); i++) {
Ned NewNed = new Ned(Ned.getRandomDNA(), 'w', 0, Random.getInteger(0, 7), Random.getInteger(0, 7),
Random.getInteger(0, 3));
Population.add(NewNed);
Playground.getSquares()[NewNed.getXPosition()][NewNed.getYPosition()].getNeds().add(NewNed);
}
for (int i = FemaleCount; i < Count; i++) {
Ned NewNed = new Ned(Ned.getRandomDNA(), 'm', 0, Random.getInteger(0, 7), Random.getInteger(0, 7),
Random.getInteger(0, 3));
Population.add(NewNed);
Playground.getSquares()[NewNed.getXPosition()][NewNed.getYPosition()].getNeds().add(NewNed);
}
}
}
System.out.println("\nSimulation startet mit folgenden Parametern:\n");
System.out.println("Maximal aufgehobene Nahrung pro Tag: " + Ned.MaxFoodPickedUp);
System.out.println("Mindestens konsumierte Nahrung pro Tag: " + Ned.MinFoodUsedPerDay);
System.out.println("Maximal generierte Nahrung pro Tag: " + Square.MaxFoodGenerated);
System.out.println("Bei Paarung verbrauchte Nahrung: " + Ned.FoodUsedPerMating);
showDay();
do {
nextDay();
showDay();
if (Population.size() == 0) {
System.out.println("\nDie Population ist ausgestorben.");
choice = 'x';
} else {
System.out.print("\n[ENTER] um fortzufahren, [x] um abzubrechen ");
choice = readChar();
System.out.print("\n");
}
} while(choice != 'x');
}
public static void nextDay() {
int i, j;
Day += 1;
for(i = 0; i <= 7; i++) {
for(j = 0; j <= 7; j++) {
Playground.getSquares()[i][j].addFood();
}
}
for (i = 0; i < Population.size(); i++) {
Ned ActualNed = (Ned)Population.elementAt(i);
ActualNed.setActionPerformed(false);
}
for (i = 0; i < Population.size(); i++) {
Ned ActualNed = (Ned)Population.elementAt(i);
ActualNed.incrementAge();
ActualNed.pickUpFood(Playground.getSquares()[ActualNed.getXPosition()][ActualNed.getYPosition()]);
if (!ActualNed.eat()) {
Population.remove(ActualNed);
Playground.getSquares()[ActualNed.getXPosition()][ActualNed.getYPosition()].getNeds().remove(ActualNed);
System.out.println("Auf Position " + ActualNed.getXPosition() + ", " + ActualNed.getYPosition()
+ " ist ein Ned gestorben.");
i--;
continue;
}
String NedAction = ActualNed.getAction();
if (NedAction.equals("forward") && !ActualNed.getActionPerformed()) {
Playground.getSquares()[ActualNed.getXPosition()][ActualNed.getYPosition()].getNeds().remove(ActualNed);
ActualNed.move();
Playground.getSquares()[ActualNed.getXPosition()][ActualNed.getYPosition()].getNeds().add(ActualNed);
} else if (NedAction.equals("rotate") && !ActualNed.getActionPerformed()) {
ActualNed.rotate();
} else if (NedAction.equals("mate") && !ActualNed.getActionPerformed()) {
Ned ActualPartner = null;
for (j = 0; j < Playground.getSquares()[ActualNed.getXPosition()][ActualNed.getYPosition()].getNeds().size(); j++) {
Ned PossiblePartner = (Ned)Playground.getSquares()[ActualNed.getXPosition()][ActualNed.getYPosition()].getNeds().get(j);
if ((PossiblePartner != ActualNed) && (PossiblePartner.getGender() != ActualNed.getGender())
&& PossiblePartner.isWillingToMate()) {
ActualPartner = PossiblePartner;
break;
}
}
if (ActualPartner != null) {
System.out.println("Ned #" + (Population.indexOf(ActualNed) + 1)
+ " paart sich mit Ned #" + (Population.indexOf(ActualPartner) + 1));
Ned Child = ActualNed.mate(ActualPartner);
Population.add(Child);
Playground.getSquares()[ActualNed.getXPosition()][ActualNed.getYPosition()].getNeds().add(Child);
ActualPartner.setActionPerformed(true);
Child.setActionPerformed(true);
if (!ActualNed.eat(2)) {
Population.remove(ActualNed);
Playground.getSquares()[ActualNed.getXPosition()][ActualNed.getYPosition()].getNeds().remove(ActualNed);
System.out.println("Auf Position " + ActualNed.getXPosition() + ", " + ActualNed.getYPosition()
+ " ist ein Ned (Parent 1) nach der Paarung gestorben.");
i--;
}
if (!ActualPartner.eat(2)) {
Population.remove(ActualPartner);
Playground.getSquares()[ActualNed.getXPosition()][ActualNed.getYPosition()].getNeds().remove(ActualPartner);
System.out.println("Auf Position " + ActualPartner.getXPosition() + ", " + ActualNed.getYPosition()
+ " ist ein Ned (Parent 2) nach der Paarung gestorben.");
}
}
}
ActualNed.setActionPerformed(true);
ActualNed.incrementActionToken();
}
}
public static void showDay() {
System.out.println("\nTag " + Day + ":\n");
for (int i = 0; i < Population.size(); i++) {
System.out.print("Ned " + (i + 1) + ": \t");
Ned ActualNed = (Ned)Population.elementAt(i);
System.out.print(ActualNed.getDNAString() + " ");
System.out.print("Position: " + ActualNed.getLocationString() + " ");
System.out.print("Direction: " + ActualNed.getDirectionString() + " ");
System.out.print("Food: " + ActualNed.getFoodBag() + " ");
System.out.print("Doing: " + ActualNed.getAction() + " ");
System.out.print("Gender: " + ActualNed.getGender() + " ");
System.out.print("Age: " + ActualNed.getAge() + "\n");
}
System.out.print("\n");
for (int i = 0; i <= 7; i++) {
for (int j = 0; j <= 7; j++) {
System.out.print(Playground.getSquares()[j][i].getNeds().size());
System.out.print("\t");
}
System.out.print("\n");
}
System.out.print("\n");
}
}