Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package packageName;
- public class Gameoflife {
- private static Boolean[][] world = new Boolean[20][20];
- public static void main(String[] args) throws InterruptedException {
- populate();
- begin_simulation();
- }
- public static void begin_simulation() throws InterruptedException {
- // uses a loop to simulate time passing and to apply rules at each interval for
- // movement, new life, expired life
- for (int x = 0; x < 100; x++) { // simulate 7 rounds
- displayGrid(); // show the two dimensional array in the console window
- next_round(); // you will need to create this method and figure you what you want to happen
- // (apply the game rules) at each iteration
- Thread.sleep(500);
- }
- }
- public static void populate() {
- // blanket fill all as false
- for (int x = 0; x < world.length; x++) { // moves through columns {
- for (int y = 0; y < world.length; y++) { // moves through rows {
- world[x][y] = false;
- }
- }
- // custom values
- world[3][4] = true;
- world[4][4] = true;
- world[5][4] = true;
- }
- private static void next_round() {
- Boolean[][] newWorld = new Boolean[world.length][world[0].length];
- for (int x = 0; x < world.length; x++) { // moves through columns {
- for (int y = 0; y < world.length; y++) { // moves through rows {
- // calculate number of living adjacent cells
- int number = 0;
- if (y > 0) {
- if (x > 0) {
- if (world[x - 1][y - 1])
- number++;
- }
- if (world[x][y - 1])
- number++;
- if (x < world[0].length - 1) {
- if (world[x + 1][y - 1])
- number++;
- }
- }
- // CENTRE VALUES
- // if not at left border
- if (x > 0) {
- // centre left
- if (world[x - 1][y])
- number++;
- }
- // if not at right border
- if (x < world[0].length - 1) {
- // centre right
- if (world[x + 1][y])
- world[x][y] = true;
- }
- // BOTTOM VALUES
- // if not at bottom border
- if (y < world.length - 1) {
- // if not at left border
- if (x > 0) {
- // top left
- if (world[x - 1][y + 1])
- number++;
- }
- // top middle
- if (world[x][y + 1])
- number++;
- // if not at right border
- if (x < world[0].length - 1) {
- if (world[x + 1][y + 1])
- number++;
- }
- }
- // if the cell is alive
- if (world[x][y]) {
- // any cell with fewer than 2 neighbours dies
- if (number < 2)
- newWorld[x][y] = false;
- // any cell with 2 or 3 neighbours survives
- if (number == 2 || number == 3)
- newWorld[x][y] = true;
- // any cell with more than 3 neighbours dies
- if (number > 3)
- newWorld[x][y] = false;
- } else {
- // if a dead cell has 3 neighbours it becomes alive
- if (number == 3)
- newWorld[x][y] = true;
- else
- newWorld[x][y] = false;
- }
- if ((x < 18) && (y < 18)) {
- if (world[x][y] && world[x + 1][y]) {
- world[x][y + 1] = true;
- world[x+1][y] = true;
- }
- }
- }
- }
- world = newWorld;
- }
- public static void displayGrid() {
- // creates output in console window
- for (int x = 0; x < world.length; x++) { // moves through columns {
- for (int y = 0; y < world.length; y++) // moves through rows {
- {
- if (world[x][y])
- System.out.print("v");
- else
- System.out.print(".");
- }
- System.out.println("\n");
- }
- System.out.println(""); // move down to next line after row is finished printing
- //
- System.out.println(""); // insert a blank line after the entire 2d arrays has been shown
- System.out.println("--------------------"); // add some dashes
- System.out.println(""); // one more blank line
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement