Skip to content

Instantly share code, notes, and snippets.

@jeifour

jeifour/Spaceship.java

Last active Dec 10, 2015
Embed
What would you like to do?
very simple spaceship simulator
public class Spaceship {
public int people = 10; // the amount of people living on the ship
public int food = 100; // the amount of food the people can use for surviving
public int fuel = 100; // how much fuel is left for flying
public double[] cord = new double[2]; // coordinates of the ship - X,Y
public double[] course = new double[2]; // coordinates of the flying target - X,Y
public static void main(String[] args) {
Spaceship traumschiffSurprise = new Spaceship(5.0, 4.0, 9.0, 9.0);
int i = 1;
while (traumschiffSurprise.advancetime()) {
System.out.println("Tag " + i + " in ihrem Raumschiff.");
System.out.println("Wir befinden uns im Moment bei diesen Vektoren:");
System.out.println("x " + traumschiffSurprise.cord[0] + " | y " + traumschiffSurprise.cord[1]);
System.out.println("Wir halten Kurs auf diese Vektoren:");
System.out.println("x " + traumschiffSurprise.course[0] + " | y " + traumschiffSurprise.course[1]);
System.out.println("Weitere Schiffsdaten:");
System.out.println("Personen: " + traumschiffSurprise.people + " | Treibstoff: " + traumschiffSurprise.fuel + " | Essen: " + traumschiffSurprise.food);
System.out.println("Geben sie n ein, um zu schlafen...");
while (System.console().readLine().charAt(0) != 'n') {
System.out.println("Geben sie n ein, um zu schlafen...");
}
i++;
}
System.out.println("Das Schiff ist explodiert, da keine Personen mehr leben.");
}
public Spaceship(double cordx, double cordy, double coursex, double coursey) {
// if no valid values are submitted, standard values will be used
if (cordx > 0 && cordx < 10 && cordy > 0 && cordy < 10 && coursex > 0 && coursex < 10 && coursey > 0 && coursey < 10) {
this.cord[0] = cordx;
this.cord[1] = cordy;
this.course[0] = coursex;
this.course[1] = coursey;
} else {
this.cord[0] = 5.0;
this.cord[1] = 5.0;
this.course[0] = 5.0;
this.course[1] = 5.0;
}
}
public boolean advancetime() {
// advance one day in time
// FIRST - fly if enough fuel (>= 10) is left
if (this.fuel >= 10) {
double[] newcord = calcnewcoords();
this.cord[0] = newcord[0];
this.cord[1] = newcord[1];
// decrease amount of fuel by 10
this.fuel -= 10;
}
// SECOND - let people eat and/or die
if (this.food >= this.people) {
// enough food for everyone
this.food -= this.people;
} else {
// sorry for your loss
this.people = this.food;
this.food = 0;
}
// THIRD - explode if everyone is dead
if (this.people == 0) {
return false;
}
return true;
}
private double[] calcnewcoords() {
// calculates new coords. isn't interested in fuel
double[] direction = new double[2];
// get direction vector
direction[0] = this.course[0] - this.cord[0];
direction[1] = this.course[1] - this.cord[1];
// calculate length of vector using pythagoras
double len = Math.sqrt(direction[0]*direction[0] + direction[1]*direction[1]);
// make direction vector shorter so it has a len of 1
direction[0] = direction[0] / len;
direction[1] = direction[1] / len;
// calculate new coords (store them in direction vector - evil!)
direction[0] = this.cord[0] + direction[0];
direction[1] = this.cord[1] + direction[1];
return direction;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.