2011-05-28 75 views
3

我的電路板能夠正確檢測出少於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; 
} 

這裏是要求的lifeCycle方法。

/** 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(); 
      } 
     }); 
    } 
} 
+0

對於一個非常好的GoL的FPGA實現,請參閱: HTTP://golly.sourceforge。 net/ – Johan 2011-05-28 22:48:33

+0

順便說一句,你的意思是說'用小於** 2 **的鄰居來檢測單元組並且殺死它們,2對嗎? – Johan 2011-05-28 22:50:07

+0

我知道這是錯誤的,但賈斯汀是否有可能把你編寫的所有代碼放在遊戲的實現中?我很想看看它!再次抱歉,我不知道如何/其他地方問這個問題。 – 2012-02-07 20:47:12

回答

2
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; 
} 
+0

謝謝。我自己也剛剛得出一些非常相似的結論。現在,我的電路板能夠正確檢測到少於3個鄰居的小區組,並將它們關閉,但似乎沒有檢測到並生出3個鄰居的小區。 我會更新初始文章以反映更改。 – Justin 2011-05-28 20:47:17

+0

我們是否也可以看到Life.lifeCycle方法?我認爲問題在那裏 – 2011-05-28 20:51:06

+0

現在我將它添加到原來的,但是是的,目前肯定沒有開發該方法。 – Justin 2011-05-28 20:53:57

1

檢查你的循環是不正確的。

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

它會一直走到board [row]索引處。這是出界的。其他循環同樣的事情

+0

檢查你的循環邏輯。這是錯誤的。檢查你的計數。你是從 n-1開始到n + 1爲什麼? – 2011-05-28 20:08:41

1

請檢查該

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) { 
1

的||沒有添加perenthisis將不會按預期工作。

當前的代碼將做到這一點:

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

或本

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