Issue with Game of Life

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

I'm working on a Java implementation of Conway's game of life as a personal project for myself. So far it works but the rules are coming out wrong. The expected patterns aren't showing up as well as it should. Is there something wrong with my code?

import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class Cell extends JComponent implements MouseListener {    private int row, col;   private boolean isLiving;    public Cell(int r, int c) {     this.row = r;     this.col = c;     this.addMouseListener(this);   }    public void isAlive(int neighbors) {     if (this.isLiving) {       if (neighbors  3) {         this.isLiving = false;       }     } else {       if (neighbors == 3) {         this.isLiving = true;       }     }   }    public boolean isLiving() {     return this.isLiving;   }    public void paintComponent(Graphics g) {     if (this.isLiving) {       g.fillRect(0, 0, 10, 10);     } else {       g.drawRect(0, 0, 10, 10);     }   }    public void mouseClicked(MouseEvent e) {     this.isLiving = !this.isLiving;   }    public void mouseEntered(MouseEvent e) {   }    public void mouseExited(MouseEvent e) {   }    public void mousePressed(MouseEvent e) {   }    public void mouseReleased(MouseEvent e) {   } } 

回答1:

I suspect1 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.

This example shows typical Game of Life behavior.

  1. Note that "suspicions ain't answers", so it is best to post an SSCCE.

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random;  public class GameOfLife extends JPanel {      private 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() {             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  3) {                 alive = false;             }         } else {             if (neighbors == 3) {                 alive = true;             }         }         return alive;     }      public void setAlive(boolean alive) {         isLiving = alive;     }      public boolean isLiving() {         return this.isLiving;     }      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  0 ? jj - 1 : s - 1);                         int btm = (jj  0 ? ii - 1 : s - 1);                         int rgt = (ii 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!