📄 gameoflife.java
字号:
// GameOfLife.javaimport comphys.graphics.*;import java.awt.*;import java.awt.event.*;public class GameOfLife extends Animation { boolean[][] neighborhood = new boolean[3][3]; boolean[] birthRule = new boolean[9]; boolean[] deathRule = new boolean[9]; void setConway () { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) neighborhood[i][j] = true; neighborhood[1][1] = false; for (int i = 0; i < 9; i++) { deathRule[i] = true; birthRule[i] = false; } birthRule[3] = true; deathRule[2] = deathRule[3] = false; } int maxL = 198; int L = 50; boolean[][] cell = new boolean[maxL + 2][maxL + 2]; boolean[][] newCell = new boolean[maxL + 2][maxL + 2]; boolean[][] fossilCell = new boolean[maxL + 2][maxL + 2]; static final int DEAD = 0; static final int ALIVE = DEAD + 1; static final int PERIODIC = ALIVE + 1; int boundary = PERIODIC; void updateCells () { if (boundary == PERIODIC) { for (int x = 1; x <= L; x++) { newCell[x][0] = newCell[x][L]; newCell[x][L+1] = newCell[x][1]; } for (int y = 1; y <= L; y++) { newCell[0][y] = newCell[L][y]; newCell[L+1][y] = newCell[1][y]; } newCell[0][0] = newCell[0][L]; newCell[0][L+1] = newCell[0][1]; newCell[L+1][0] = newCell[1][0]; newCell[L+1][L+1] = newCell[L+1][1]; } else if (boundary == DEAD) { for (int i = 0; i <= L+1; i++) newCell[0][i] = newCell[i][0] = newCell[L+1][i] = newCell[i][L+1] = false; } else if (boundary == ALIVE) { for (int i = 0; i <= L+1; i++) newCell[0][i] = newCell[i][0] = newCell[L+1][i] = newCell[i][L+1] = true; } for (int y = 0; y <= L + 1; y++) for (int x = 0; x <= L + 1; x++) { if (newCell[x][y]) fossilCell[x][y] = false; else if (cell[x][y]) fossilCell[x][y] = true; cell[x][y] = newCell[x][y]; } } static final int RANDOM = ALIVE + 1; int initialConfiguration = RANDOM; static final int GOSPER = RANDOM + 1; String[] GosperGliderGun = { "000000000000000000000000100000000000", "000000000000000000000010100000000000", "000000000000110000001100000000000011", "000000000001000100001100000000000011", "110000000010000010001100000000000000", "110000000010001011000010100000000000", "000000000010000010000000100000000000", "000000000001000100000000000000000000", "000000000000110000000000000000000000", }; void setConfiguration () { if (initialConfiguration == RANDOM) { for (int y = 1; y <= L; y++) { for (int x = 1; x <= L; x++) { if (Math.random() < 0.5) newCell[x][y] = true; else newCell[x][y] = false; } } } else if (initialConfiguration == ALIVE) { for (int y = 1; y <= L; y++) { for (int x = 1; x <= L; x++) { newCell[x][y] = true; } } } else { // DEAD or prepare for pattern for (int y = 1; y <= L; y++) { for (int x = 1; x <= L; x++) { newCell[x][y] = false; } } } if (initialConfiguration == GOSPER) { String[] s = GosperGliderGun; int rows = s.length; int cols = s[0].length(); int x0 = (L - cols) / 2; int y0 = (L + rows) / 2; for (int ix = 0; ix < cols; ix++) { for (int iy = 0; iy < rows; iy++) { if (s[iy].charAt(ix) - '0' == ALIVE) { int x = x0 + ix; int y = y0 - iy; if (x >= 0 && x <= L && y >= 0 && y <= L) newCell[y][x] = true; } } } } for (int y = 0; y <= L + 1; y++) for (int x = 0; x <= L + 1; x++) cell[x][y] = fossilCell[x][y] = false; updateCells(); takeCensus(); } int births; int adults; int deaths; boolean fossil; int fossils; void takeCensus () { births = adults = deaths = fossils = 0; for (int y = 1; y <= L; y++) { for (int x = 1; x <= L; x++) { if (newCell[x][y]) { if (cell[x][y]) ++adults; else ++births; } else { if (cell[x][y]) ++deaths; else if (fossilCell[x][y]) ++fossils; } } } } int t; boolean initialize; boolean userHasChangedCells; void timeStep () { if (initialize) initial(); if (userHasChangedCells) { updateCells(); userHasChangedCells = false; } ++t; for (int y = 1; y <= L; y++) { for (int x = 1; x <= L; x++) { int sumOfNeighbors = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (neighborhood[i][j]) if (cell[x + j - 1][y + i - 1]) ++sumOfNeighbors; if (cell[x][y]) { if (deathRule[sumOfNeighbors]) newCell[x][y] = false; } else { if (birthRule[sumOfNeighbors]) newCell[x][y] = true; } } } takeCensus(); } boolean stillLife () { for (int y = 0; y <= L + 1; y++) for (int x = 0; x <= L + 1; x++) if (cell[x][y] != newCell[x][y]) return false; return true; } void initial () { t = 0; setConfiguration(); initialize = false; } boolean needToClear; boolean small; boolean grow = true; boolean grid; boolean userHasChangedL; class Ecosystem extends Plot implements MouseListener, MouseMotionListener { int pixels = 400; Ecosystem () { setSize(pixels, pixels); addMouseListener(this); addMouseMotionListener(this); } int x0; int y0; int d; boolean scribble; int xScribble; int yScribble; public void paint () { setWindow(0, pixels, 0, pixels); d = (int) (pixels / (double) (L + 2)); if (d < 1) d = 1; int dRect = d - 1; if (small) x0 = y0 = (pixels - L - 2) / 2; else x0 = y0 = (pixels - (L + 2) * d) / 2; if (grid) { osg.clearRect(0, 0, pixels, pixels); setColor("gray"); int x1 = x0 + (L + 2) * d; if (small) x1 = x0 + (L + 2); for (int i = 0; i <= L + 2; i++) { int x = x0 + i * d; if (small) x = x0 + i; osg.drawLine(x, x0, x, x1); osg.drawLine(x0, x, x1, x); } grid = false; return; } if (scribble) { if (grow) setColor("red"); else setColor("black"); if (small) { osg.drawLine(x0 + xScribble, y0 + yScribble, x0 + xScribble, y0 + yScribble); } else { int x = x0 + xScribble * d; int y = y0 + yScribble * d; osg.fillRect(x, y, dRect, dRect); } scribble = false; return; } if (needToClear) { osg.clearRect(0, 0, pixels, pixels); needToClear = false; } // outline boundary if (!small) { if (fossil) setColor("magenta"); else setColor("yellow"); osg.drawRect(x0 - 1, y0 - 1, (L + 2) * d, (L + 2) * d); osg.drawRect(x0 + d - 1, y0 + d - 1, L * d, L * d); } for (int i = 0; i <= L + 1; i++) { for (int j = 0; j <= L + 1; j++) { if (newCell[i][j]) { if (cell[i][j] || i == 0 || i == L + 1 || j == 0 || j == L + 1) setColor("green"); else setColor("red"); } else { if (cell[i][j]) setColor("black"); else if (fossil && fossilCell[i][j]) setColor("lightGray"); else setColor("white"); } if (small) { osg.drawLine(x0 + j, y0 + i, x0 + j, y0 + i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -