Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I <em>suspect</em><sup>1</sup> the problem with your code is that it is setting the cells to living or dead as soon as the neighbors are checked. That caused my early variants of this code to fail. That change of state has to be delayed until the entire grid (biosphere) has been checked.</p> <p>This example shows typical Game of Life behavior.</p> <ol> <li>Note that "suspicions ain't answers", so it is best to post an SSCCE.</li> </ol> <p><img src="https://i.stack.imgur.com/rJkh2.png" alt="Game of Life"></p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random; public class GameOfLife extends JPanel { private final int row, col; private boolean isLiving; public static Random random = new Random(); public GameOfLife(int r, int c) { this.row = r; this.col = c; MouseListener listener = new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { isLiving = !isLiving; repaint(); } }; this.addMouseListener(listener); isLiving = random.nextBoolean(); } public boolean isAlive(int neighbors) { boolean alive = false; if (this.isLiving) { if (neighbors &lt; 2) { alive = false; } else if (neighbors == 2 || neighbors == 3) { alive = true; } else if (neighbors &gt; 3) { alive = false; } } else { if (neighbors == 3) { alive = true; } } return alive; } public void setAlive(boolean alive) { isLiving = alive; } public boolean isLiving() { return this.isLiving; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (this.isLiving) { g.fillRect(0, 0, getWidth() - 1, getHeight() - 1); } else { g.drawRect(0, 0, getWidth() - 1, getHeight() - 1); } } public static void main(String[] args) { final int s = 40; final GameOfLife[][] biosphere = new GameOfLife[s][s]; final JPanel gui = new JPanel(new GridLayout(s, s, 2, 2)); for (int ii = 0; ii &lt; s; ii++) { for (int jj = 0; jj &lt; s; jj++) { GameOfLife cell = new GameOfLife(ii, jj); cell.setPreferredSize(new Dimension(10, 10)); gui.add(cell); biosphere[ii][jj] = cell; } } ActionListener al = (ActionEvent ae) -&gt; { boolean[][] living = new boolean[s][s]; for (int ii = 0; ii &lt; s; ii++) { for (int jj = 0; jj &lt; s; jj++) { int top = (jj &gt; 0 ? jj - 1 : s - 1); int btm = (jj &lt; s - 1 ? jj + 1 : 0); int lft = (ii &gt; 0 ? ii - 1 : s - 1); int rgt = (ii &lt; s - 1 ? ii + 1 : 0); int neighbors = 0; if (biosphere[ii][top].isLiving()) { neighbors++; } if (biosphere[ii][btm].isLiving()) { neighbors++; } if (biosphere[lft][top].isLiving()) { neighbors++; } if (biosphere[lft][btm].isLiving()) { neighbors++; } if (biosphere[lft][jj].isLiving()) { neighbors++; } if (biosphere[rgt][jj].isLiving()) { neighbors++; } if (biosphere[rgt][top].isLiving()) { neighbors++; } if (biosphere[rgt][btm].isLiving()) { neighbors++; } living[ii][jj] = biosphere[ii][jj].isAlive(neighbors); } } for (int ii = 0; ii &lt; s; ii++) { for (int jj = 0; jj &lt; s; jj++) { biosphere[ii][jj].setAlive(living[ii][jj]); } } gui.repaint(); }; Timer timer = new Timer(50, al); timer.start(); JOptionPane.showMessageDialog(null, gui); timer.stop(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload