【问题标题】:Game of Life Assignment生命分配游戏
【发布时间】:2014-01-10 02:46:34
【问题描述】:

我的板正确地检测到少于 3 个邻居的组并将其杀死,但似乎没有检测并生成具有 3 个邻居的单元。

有什么想法吗?

如果我没有提供足够的信息,请告诉我,我可以粘贴更多代码,但我认为这是所有相关部分。

提前感谢您提供的任何建议。

public boolean getCell(int row, int col) {
    boolean state = board[row][col];
    int neighbors = 0;
    for (int x = row-1; x <= row+1; x++) {
        for (int y = col-1; y <= col+1; y++) {
            // don't include this
            if ((x != row || y != col) && x != -1 && y != -1 
            && x != NROWSCOLS && y != NROWSCOLS) {
                if (board[x][y] == ALIVE){
                    neighbors ++;
                }
            }
        }
    }
    if (neighbors > 3 || neighbors < 2)
        state = DEAD;
    else if(neighbors == 3)
        state = ALIVE;
    return state;
}

这是请求的生命周期方法。

/** Process one life cycle of the cellular automaton
 * 
 */
public void lifeCycle() {
    for (int x = 0; x < NROWSCOLS ; x++) {
        for (int y = 0; y < NROWSCOLS; y++) {
            getCell(x,y);
        }
    }

    generations ++;
}

我已附上 LifeGUI 以供参考,但此代码是提供的,我不打算更改。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class LifeGUI extends JPanel {
    // game instance variables
    private Life board;        // game board

    // GUI components
    private JLabel generationsLived;          
    private JButton resetButton, cycleButton;   // reset control and cycle control
    private Cell[][] cells;         // board cells for display

    /** Construct new Life game with a graphical user interface */
    public LifeGUI()    {
        // create and initialize game board and display representation
        board = new Life();
        cells = new Cell[Life.NROWSCOLS][Life.NROWSCOLS];

        // set layout for game display
        setLayout(new BorderLayout());

        // Create board cells and add to display
        JPanel boardPanel = new JPanel();
        boardPanel.setLayout(new GridLayout(Life.NROWSCOLS, Life.NROWSCOLS));
        for (int row = 0; row < Life.NROWSCOLS; row++) {
            for (int col = 0; col < Life.NROWSCOLS; col++) {
                cells[row][col] = new Cell(Life.DEAD, row, col);
                boardPanel.add(cells[row][col]);
           }
        }
        add(boardPanel, BorderLayout.CENTER);


        // Set up 2 buttons
        // a reset button so it starts a new game when clicked
        // a cycle button to tell the Life automaton to live one cycle
        resetButton = new JButton("New Game");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                board.newGame();
                updateDisplay();
            }
        });

        cycleButton = new JButton("Live One Cycle");
        cycleButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                board.lifeCycle();
                updateDisplay();
            }
        });

        // Put the buttons and the generation count display on the screen
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(resetButton);
        buttonPanel.add(cycleButton);
        generationsLived = new JLabel("     Generations Lived: " , JLabel.RIGHT);
        buttonPanel.add(generationsLived);
        add(buttonPanel, BorderLayout.SOUTH);

        // show initial display
        updateDisplay();
    }

    /** Update display to match game state. */
    public void updateDisplay() {
        // update count display
        generationsLived.setText("     Generations Lived: " + board.getGenerationCount());

        // update board display
        for (int row = 0; row < Life.NROWSCOLS; row++) {
            for (int col = 0; col < Life.NROWSCOLS; col++) {
                cells[row][col].setState(board.getCell(row,col));
            }
        }
        repaint();
    }

    /** Create new game and a window to display it */
    private static void test() {
        JFrame f = new JFrame("The Game of Life");     // top-level window
        LifeGUI l = new LifeGUI();
        f.getContentPane().add(l);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600,600);
        f.validate();
        f.setVisible(true);
        f.toFront();
    }

    public static void main(String[] args) {
        // To support stand-alone application
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                LifeGUI.test();
            }
        });
    }
}

【问题讨论】:

  • 对于一个非常好的 GoL 实现,请参见:golly.sourceforge.net
  • 顺便说一句,你的意思是detects cell groups with less than **2** neighbors and kills them off,2 对吗?
  • 我知道这是错误的,但贾斯汀如果你能把你为游戏实现所写的所有代码都贴出来,有可能吗?我很想看看它!再次抱歉,我不知道该怎么问/在哪里问这个。
  • 这是一个相当古老的话题。我不再有这个工作了,但我怀疑谷歌搜索“生命游戏java源代码”或类似的东西会为你提供最好的服务。祝你好运。

标签: java conways-game-of-life


【解决方案1】:
public boolean getCell(int row, int col) {
    boolean state = board[row][col];
    int neighbors = 0;//you are summing the alive neighbours 
       //keep var declaration outside the iteration over them

    for (int x = Math.max(0,row-1); x < Math.min(row+2,NROWSCOLS); x++) {
        for (int y = Math.max(0,col-1); y < Math.min(col+2,NROWSCOLS); y++) {
            //using min and max to ensure x and y remain between 0 and NROWSCOLS 
            //so no IOBException
                if (board[x][y] == ALIVE){
                    neighbors ++;

                }
            }
        }
    }
    if (neighbors > 3 || neighbors < 2)//only do the check when you are finished counting the alive neighbours
        state = DEAD;
    else if(neighbors == 3)
        state = ALIVE;
    return state;
}

【讨论】:

  • 谢谢。我自己刚刚得出了一些非常相似的结论。现在我的董事会正确地检测到少于 3 个邻居的细胞组并将它们杀死,但似乎没有检测并产生具有 3 个邻居的细胞。我将更新最初的帖子以反映更改。
  • 我们还能看到 Life.lifeCycle 方法吗?我认为问题出在那儿
  • 我现在将其添加到原件中,但是是的,该方法目前绝对未开发。
  • 您不应该检查单元格本身的 ALIVE 状态,因为它不是邻居,因此只需在循环中添加另一个 if 语句: if(x == row && y == col) continue;,在检查邻居状态之前。
【解决方案2】:

检查你的循环不正确。

 for (int x = row-1; x <= row; x++)

它总是会进入 board[row] 索引。这超出了界限。其他循环也一样

【讨论】:

  • 检查你的循环逻辑。这是错误的。检查你的计数。你是从 n-1 开始到 n+1 为什么?
【解决方案3】:

请检查一下

    at Life.getCell(Life.java:63)

问题出在 Life.java 的第 63 行

我不知道问题是否存在,但请检查这一行:

if (x != row || y != col && x != -1 && y != -1 
            && x != NROWSCOLS && y != NROWSCOLS) {

因为操作符的优先级没有做你认为它正在做的事情, 请添加一些括号。

这样

if ((x != row || y != col) && x != -1 && y != -1 
            && x != NROWSCOLS && y != NROWSCOLS) {

【讨论】:

    【解决方案4】:

    ||如果不添加 perenthisis,将无法按预期工作。

    当前代码会这样做:

     if (x != row || (y != col && x != -1 && y != -1 && x != NROWSCOLS && y != NROWSCOLS))
    

    你可能想要这个

    if ((x != row || y != col) &amp;&amp; x != -1 &amp;&amp; y != -1 &amp;&amp; x != NROWSCOLS &amp;&amp; y != NROWSCOLS)

    或者这个

     if (x != row && y != col && x != -1 && y != -1 && x != NROWSCOLS && y != NROWSCOLS)
    

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多