⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameoflife.java

📁 生命游戏的java源代码。基于元胞自动机的生命游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    } else {                        int x = x0 + j * d;                        int y = y0 + i * d;                        osg.fillRect(x, y, dRect, dRect);                    }                }            }        }        int mouseX;        int mouseY;        boolean toggle;                void editCell () {            int x = mouseX - x0;            int y = mouseY - y0;            if (!small) {                x = (int) (x / (double) d);                y = (int) (y / (double) d);            }            if (x > 0 && x <= L && y > 0 && y <= L) {                boolean newValue = grow;                if (toggle)                    newValue = !newCell[y][x];                toggle = false;                newCell[y][x] = newValue;                if (boundary == PERIODIC) {                    if (x == 1)                        newCell[y][L + 1] = newValue;                    if (x == L)                        newCell[y][0] = newValue;                    if (y == 1)                        newCell[L + 1][x] = newValue;                    if (y == L)                        newCell[0][x] = newValue;                    if (y == 1 && x == 1)                        newCell[L + 1][L + 1] = newValue;                    if (y == 1 && x == L)                        newCell[L + 1][0] = newValue;                    if (y == L && x == 1)                        newCell[0][L + 1] = newValue;                    if (y == L && x == L)                        newCell[0][0] = newValue;                }                xScribble = x;                yScribble = y;                scribble = true;                repaint();            }        }        public void mouseClicked (MouseEvent me) { }        public void mouseEntered (MouseEvent me) {              if (userHasChangedL) {                  initial();                  needToClear = true;                  userHasChangedL = false;                  repaint();              }        }        public void mouseExited (MouseEvent me) { }        public void mousePressed (MouseEvent me) {            mouseX = me.getX();            mouseY = me.getY();            toggle = true;            editCell();        }        public void mouseReleased (MouseEvent me) {            repaint();        }        public void mouseMoved (MouseEvent me) { }        public void mouseDragged (MouseEvent me) {            mouseX = me.getX();            mouseY = me.getY();            editCell();        }    }    class RuleWindow extends Plot implements MouseListener {        int xPixels = 400;        int yPixels = 150;        Color bgColor = new Color(255, 250, 245);        RuleWindow () {            setSize(xPixels, yPixels);            addMouseListener(this);        }        // locations of clickable squares of side unit pixels        int unit, maxNeighbors;        int xNeighborhood, xBirth, xDeath;        int yNeighborhood, yBirth, yDeath;        public void paint () {            setWindow(0, xPixels, 0, yPixels);            osg.setColor(bgColor);            osg.fillRect(0, 0, xPixels, yPixels);            int d = unit = 20;            int dy = d;            int y = yNeighborhood = d / 2;            int x = xNeighborhood = xPixels - d / 2 - 3 * d;            maxNeighbors = 0;            for (int i = 0; i < 3; i++) {                for (int j = 0; j < 3; j++) {                    if (neighborhood[i][j]) {                        setColor("blue");                        ++maxNeighbors;                    } else                        setColor("white");                    osg.fillRect(x + j * d, y + i * d, d - 1, d - 1);                }            }            setColor("cyan");            osg.fillRect(x + d, y + d, d - 1, d - 1);            xBirth = x = xPixels - 9 * d - d / 2;            yBirth = y += 4 * d - d / 2;            for (int i = 0; i <= maxNeighbors; i++) {                setColor("blue");                osg.drawRect(x + i * d, y, d, d);                if (birthRule[i]) {                    setColor("red");                    osg.fillRect(x + i * d + 1, y + 1, d - 1, d - 1);                } else if (fossil) {                    setColor("lightGray");                    osg.fillRect(x + i * d + 1, y + 1, d - 1, d - 1);                }            }            xDeath = x;            yDeath = y += 2 * d;            for (int i = 0; i <= maxNeighbors; i++) {                setColor("yellow");                osg.drawRect(x + i * d, y, d, d);                if (deathRule[i]) {                    setColor("black");                    osg.fillRect(x + i * d + 1, y + 1, d - 1, d - 1);                } else {                    setColor("green");                    osg.fillRect(x + i * d + 1, y + 1, d - 1, d - 1);                }            }            x = d / 2;            y = 2 * dy - d / 2 - 4;            setColor("blue");            osg.drawString("Time step = " + t, x, y);            y += dy;            setColor("green");            osg.drawString("Adults: " + adults, x, y);            setColor("gray");            if (fossil)                osg.drawString("Fossils: " + fossils, x + 100, y);            y += dy;            setColor("red");            osg.drawString("Births: " + births, x, y);            setColor("black");            osg.drawString("Deaths: " + deaths, x + 100, y);            y += dy + d / 2;            setColor("magenta");            osg.drawString("Dead cell comes alive if", x, y);            y += dy;            setColor("blue");            osg.drawString("Number of neighbors =", x, y);            x = xPixels - 9 * d - d / 2 + 8;            for (int i = 0; i <= maxNeighbors; i++)                osg.drawString("" + i, x + i * d, y);            x = d / 2;            y += dy;            setColor("magenta");            osg.drawString("Live cell dies or survives", x, y);        }        public void mouseClicked (MouseEvent me) { }        public void mouseEntered (MouseEvent me) { }        public void mouseExited (MouseEvent me) { }        public void mousePressed (MouseEvent me) {            int x = me.getX();            int y = me.getY();            if (y > yNeighborhood && y < yNeighborhood + 3 * unit) {                if (x > xNeighborhood && x < xNeighborhood + 3 * unit) {                    int i = (int) ( (y - yNeighborhood) / (double) unit );                    int j = (int) ( (x - xNeighborhood) / (double) unit );                    if (!(i == 1 && j == 1))                        neighborhood[i][j] = !neighborhood[i][j];                    repaint();                }            }            if (y > yBirth && y < yBirth + unit) {                if (x > xBirth && x < xBirth + (maxNeighbors + 1) * unit) {                    int j = (int) ( (x - xBirth) / (double) unit );                    birthRule[j] = !birthRule[j];                    repaint();                }            }            if (y > yDeath && y < yDeath + unit) {                if (x > xDeath && x < xDeath + (maxNeighbors + 1) * unit) {                    int j = (int) ( (x - xDeath) / (double) unit );                    deathRule[j] = !deathRule[j];                    repaint();                }            }        }        public void mouseReleased (MouseEvent me) { }    }        Ecosystem ecosystem;    RuleWindow ruleWindow;    Reader LReader;    Checkbox growBox, smallBox, fossilBox;    Choice boundaryChooser, initialChooser;    public void init () {        setConway();        initial();        needToClear = true;        add(ecosystem = new Ecosystem());        add(ruleWindow = new RuleWindow());        add(LReader = new Reader("L = ", L));        add(growBox = new Checkbox("Create", grow));        growBox.addItemListener(new ItemListener() {            public void itemStateChanged(ItemEvent ie) {                grow = growBox.getState();            }        });        add(smallBox = new Checkbox("Small", small));        smallBox.addItemListener(new ItemListener() {            public void itemStateChanged(ItemEvent ie) {                small = smallBox.getState();                needToClear = true;            }        });        add(fossilBox = new Checkbox("Fossils", fossil));        fossilBox.addItemListener(new ItemListener() {            public void itemStateChanged(ItemEvent ie) {                fossil = fossilBox.getState();            }        });        add(boundaryChooser = new Choice());        boundaryChooser.add("Periodic bndry");        boundaryChooser.add("Dead boundary");        boundaryChooser.add("Live boundary");        boundaryChooser.addItemListener(new ItemListener() {            public void itemStateChanged(ItemEvent ie) {                String boundaryType = boundaryChooser.getSelectedItem();                if (boundaryType.equals("Periodic bndry"))                    boundary = PERIODIC;                 else if (boundaryType.equals("Dead boundary"))                    boundary = DEAD;                 else                    boundary = ALIVE;                updateCells();            }        });        add(initialChooser = new Choice());        initialChooser.add("Start random");        initialChooser.add("Start dead");        initialChooser.add("Start alive");        initialChooser.add("Gosper gun");        initialChooser.addItemListener(new ItemListener() {            public void itemStateChanged(ItemEvent ie) {                String initialType = initialChooser.getSelectedItem();                if (initialType.equals("Start random"))                    initialConfiguration = RANDOM;                 else if (initialType.equals("Start dead"))                    initialConfiguration = DEAD;                else if (initialType.equals("Gosper gun"))                    initialConfiguration = GOSPER;                else                    initialConfiguration = ALIVE;            }        });        addControlPanel();    }    public void step () {        updateCells();        timeStep();        ecosystem.repaint();        ruleWindow.repaint();    }    public void reset () {        L = LReader.readInt();        if (L > 198) {            L = 198;            LReader.setInt(198);        }        initial();        needToClear = true;        ecosystem.repaint();        ruleWindow.repaint();    }    public static void main (String[] args) {        GameOfLife gameOfLife = new GameOfLife();        gameOfLife.frame("Conway's Game of Life", 430, 800);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -