作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的板正确地检测到邻居少于 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();
}
});
}
}
最佳答案
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;
}
关于java - 生命游戏作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6164026/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!