3D Game of Life

Discussion in 'Programming' started by Assossa, Mar 26, 2016.

  1. Hey guys,
    I've been messing around with cellular automata recently and decided to make the game of life in Minecraft. The third dimension is time in my simulation. Here's what it looks like in game:
    [​IMG]
    That's a 100x100 board through 200 frames.

    Here's the code:
    Code (Text):
    package xyz.assossa.gol;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;

    public class GameOfLife extends JavaPlugin implements Listener {

        public static final int size = 100;
        public static final int y = 0;

        public int yOffset = 0;
        public boolean[][] board = new boolean[size][size];

        @Override
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);

            reset();
            new BukkitRunnable() {
                @Override
                public void run() {
                    step();
                }
            }.runTaskTimer(this, 200, 20);
        }

        @EventHandler
        public void onChat(PlayerChatEvent e) {
            reset();
        }

        private void reset() {
            yOffset = 0;
            for (int x = 0 - (size / 2); x <= size / 2; x++) {
                for (int z = 0 - (size / 2); z <= size / 2; z++) {
                    for (int y = this.y; y <= 200; y++) {
                        new Location(Bukkit.getWorlds().get(0), x, y, z).getBlock().setType(Material.AIR);
                    }
                }
            }
            generateRandom();
            draw();
        }

        private void generateRandom() {
            for (int x = 0; x < size; x++)
                for (int y = 0; y < size; y++)
                    board[x][y] = Math.random() > 0.5;
        }

        private void step() {
            if (yOffset > 200)
                return;
            boolean board2[][] = new boolean[size][size];
            for (int x = 0; x < size; x++)
                for (int y = 0; y < size; y++) {
                    int n = getNeighbors(x, y);
                    if (board[x][y])
                        board2[x][y] = n >= 2 && n < 4;
                    else
                        board2[x][y] = n == 3;
                }
            yOffset++;
            board = board2;
            draw();
        }

        private void draw() {
            for (int x = 0; x < size; x++)
                for (int y = 0; y < size; y++)
                    new Location(Bukkit.getWorlds().get(0), x - (size / 2), this.y + yOffset, y - (size / 2)).getBlock().setType(board[x][y] ? Material.IRON_BLOCK : Material.AIR);
        }

        private int getNeighbors(int x, int y) {
            int n = 0;

            n += getCell(x + 1, y) ? 1 : 0;
            n += getCell(x, y + 1) ? 1 : 0;
            n += getCell(x - 1, y) ? 1 : 0;
            n += getCell(x, y - 1) ? 1 : 0;
            n += getCell(x - 1, y - 1) ? 1 : 0;
            n += getCell(x + 1, y + 1) ? 1 : 0;
            n += getCell(x + 1, y - 1) ? 1 : 0;
            n += getCell(x - 1, y + 1) ? 1 : 0;

            return n;
        }

        private boolean getCell(int x, int y) {
            return x < 0 || x > size - 1 || y < 0 || y > size - 1 ? false : board[x][y];
        }
    }
     
    Please realize that I made this in a few minutes and is not optimized. I would recommend setting the size to 50 or under unless you have a decent computer.
    The simulation will start at 0,0 in your default world. One frame is rendered a second. Send any chat message to restart the simulation.

    I'm going to bed now, it's 2:30AM. Good night, I hope you guys enjoy this little toy!
     
    • Creative Creative x 4
    • Like Like x 3
  2. Can someone explain what exactly this is to me? I don't understand anything at all.
     
    • Funny Funny x 1
  3. See https://en.wikipedia.org/wiki/Conway's_Game_of_Life
    Be sure to read the rules (there are only 4). You set up a pattern of "cells" on a matrix, and each interval in time, you change the pattern based on the rules. You observe how it changes over time.

    Programming this game was a popular thing back in the days of the 8080.
     
    #5 Bobcat00, Mar 28, 2016
    Last edited: Mar 28, 2016
  4. Speaking of the 8080, I actually own a TRS-80 Model I. I've never made anything complex on it though, mainly because there is no hard drive or decent file editor :p
     
  5. Nobody had hard drives in the 1970's; they cost thousands of dollars. If you were lucky, you had 5-1/4" floppies. The rest of us had cassette tape interfaces.
     
    • Like Like x 1