Introduction: In this lab, we will simulate our own universe, as well as attempt
ID: 3672433 • Letter: I
Question
Introduction:
In this lab, we will simulate our own universe, as well as attempt to understand arrays and writing to files.
Assignment:
Causal determinism is the belief that every event is necessary and caused by events that occurred before it. For example, the reason that there is a broken egg on the floor is that I dropped it; the reason that I dropped it was that I was looking out the window; the reason I was looking out the window was to see a beautiful sunset; the reason the sunset was so beautiful was a volcano that erupted; the reason that the volcano erupted was a fissure in the Earth's surface; the reason for the fissure was a weakness in the granite... ad infinitum, or at least until the beginning of the Universe. Determinism holds that there is no such thing as free will; our actions and thoughts are also the result of whatever the state of our own universe was a moment ago.
We will create a one-dimensional universe, and it will be governed by deterministic rules. The universe will consist of an array of characters representing spots of land. Every fifth spot of land, starting at position 5 in the array, will have a Carrot on it (represented as a "^"). Any spot of land without a Carrot or a Numericon (see below) will be represented as a ".".
It will be peopled by the Numericons. Numericons have three stages of life - baby, child, and adult (represented by 0, 1, and 2, respectively). Numericons exist on every seventh spot of land, starting on the zeroth spot. All Numericons are generated initially as Baby Numericons. If a Numericon is generated on a spot of land where a Carrot is, the Numericon takes precedence and no Carrot is there.
Baby and Child Numericons do not move or eat; they simple grow from Baby to Child, and Child to Adult, each after one iteration. Adult Numericons move to the "right" (increasing index in the array) by one spot each iteration until they find a Carrot. At this point, they eat the Carrot (it is removed from the Universe), and become a Baby Numericon. If an Adult Numericon tries to move but another Numericon is blocking their way, they stay in the same spot. This is true even if the other Numericon has not yet moved in that iteration! For example,
The first Adult Numericon cannot move, but the second one can. After one iteration, the Universe will look like this:
At the beginning of the simulation, you should ask the user what size they would like the simulation to be, and generate the universe in a method with the following method signature:
Other aspects of the signature, such as whether it is private or public, static or not, etc., are up to you.
At each iteration, you should display the world in its current state. You may choose to (Q)uit, (A)dvance one iteration or (S)ave to a file. You may assume that only valid input will be entered.
Quitting will end the simulation (and the Java program)
Advancing one iteration will cause all Numericons to age one stage (a baby Numericon becomes a child, a child becomes an adult, and an adult moves or becomes a child after eating a Carrot). Carrots do not age.
Saving to a file will write the current state of the universe to a file named "universe.txt" in the current directory. It will overwrite any file which is already there with the same name.
You will then be able to save your universe to a text file, along with the number of baby, child, and adult Numericons. The universe will be saved on one line, and then the number of baby, child, and adult Numericons on separate lines after that (see below for a sample).
Eventually, your Universe will run out of Carrots, and your poor adult Numericons will be hungry forever. After finishing the assignment, you may want to add an additional "Carrot regeneration" rule if you would like the Numericons. However, this is entirely optional. It is up to you and your own personal ethical system whether you should spent time generating Carrots for virtual beings. You also may want to add the ability to "load" a world that had previously been written to a file, or you may allow the Numericons in that Universe to live in an eternal statis until you delete the directory in which they are stored.
Example of Sample Output:
Sample Saved Universe:
Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
int arrayLength;
char[] world = new char[1];
boolean valid = false;
while (!valid) {
System.out.print("How big of the world > ");
if (scan.hasNextInt()) {
arrayLength = scan.nextInt();
world = generateUniverse(arrayLength);
valid = true;
} else {
System.out.println("Try again");
scan.next();
}
}
for (int i = 0; i < world.length; i++) {
if (i > 0 && i % 5 == 0 && i % 7 != 0) {
world[i] = '^';
} else if (i % 7 == 0) {
world[i] = '0';
} else {
world[i] = '.';
}
}
printArray(world);
boolean stop = false;
while (!stop) {
System.out.print("What to do now? Q)uit, (A)dvance or (S)ave ");
String choice = scan.next();
if (choice.equalsIgnoreCase("Q")) {
System.exit(0);
} else if (choice.equalsIgnoreCase("A")) {
stop = false;
for (int i = 0; i < world.length; i++) {
if (world[i] == '0') {
world[i] = '1';
} else if (world[i] == '1') {
world[i] = '2';
} else if (world[i] == '2' && i == world.length - 1) {
world[i] = '2';
} else if (world[i] == '2' && world[i + 1] == '2') {
world[i] = '2';
world[i + 1] = '2';
} else if (world[i] == '2' && world[i + 1] == '.') {
world[i] = '.';
world[i + 1] = '2';
i++;
} else if (world[i] == '2' && world[i + 1] == '^') {
world[i] = '.';
world[i + 1] = '0';
i++;
}
//System.out.println(i);
}
printArray(world);
} else if (choice.equalsIgnoreCase("S")) {
int baby=0, child=0, adult=0;
for (int j=0; j<world.length; j++){
if (world[j] == '0'){
baby++;
}else if (world[j] == '1'){
child++;
}else if (world[j] == '2'){
adult++;
}
}
PrintWriter output = new PrintWriter("universe.txt");
output.println(world);
output.println("Babies: " + baby);
output.println("Children: " + child);
output.println("Adults: " + adult);
output.close();
}
}
}
public static void printArray(char[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
System.out.println("]");
}
public static char[] generateUniverse(int size) {
char[] temp = new char[size];
return temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.