java生命游戏_java – 生命游戏分配

我的电路板正确检测到少于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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值