Java中的生活游戏GUI-表达式总是错误的
我试图实现一个基本版本的生活游戏,它的工作,但今天我试图为它制作一个图形用户界面,我有问题 问题是我有一个Java中的生活游戏GUI-表达式总是错误的,java,Java,我试图实现一个基本版本的生活游戏,它的工作,但今天我试图为它制作一个图形用户界面,我有问题 问题是我有一个if语句来检查游戏规则之一。IDE告诉我它总是返回false if (!grid[row][col].isAlive() && aliveNeighbours == 3) 我检查了我的方法,做了各种测试,但可能我太累了,看不到问题所在 下面是完整的课程: import java.awt.*; import java.awt.event.ActionEvent; import
if
语句来检查游戏规则之一。IDE告诉我它总是返回false
if (!grid[row][col].isAlive() && aliveNeighbours == 3)
我检查了我的方法,做了各种测试,但可能我太累了,看不到问题所在
下面是完整的课程:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class Main extends JFrame {
int size = 50;
Cell[][] grid = new Cell[size][size];
//Swing components
JPanel p = new JPanel();
JPanel top = new JPanel();
JButton startButton = new JButton("Start");
JButton randomButton = new JButton("Randomize");
JPanel center = new JPanel();
public static void main(String[] args){
new Main();
}
public Main(){
super("Game of Life GUI");
setSize(1200,900);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p.setLayout(new BorderLayout());
center.setLayout(new GridLayout(size, size));
setupButtons();
createFirstGrid();
top.add(startButton);
top.add(randomButton);
p.add(top, BorderLayout.NORTH);
p.add(center, BorderLayout.CENTER);
add(p);
setVisible(true);
}
void startLoop(){
Timer timer = new Timer();
System.out.println("Started");
timer.schedule(new TimerTask() {
@Override
public void run() {
step();
}
}, 1000, 1000);
}
void step() {
Cell[][] newGrid = grid;
for (int row = 1; row < grid.length - 1; row++) {
for (int col = 1; col < grid[row].length - 1; col++) {
int aliveNeighbours = getAliveNeighbours(grid, row, col);
if (grid[row][col].isAlive() && aliveNeighbours == 2 || aliveNeighbours == 3){
newGrid[col][row].setAliveStatus(true);
} else if (!grid[row][col].isAlive() && aliveNeighbours == 3){
newGrid[col][row].setAliveStatus(true);
} else if (aliveNeighbours < 2 || aliveNeighbours > 3) {
newGrid[col][row].setAliveStatus(false);
}
}
}
grid = newGrid;
}
private int getAliveNeighbours(Cell[][] grid, int row, int col){
int aliveCells = 0;
for (int x = row - 1; x <= row + 1; x++){
for (int y = col - 1; y <= col; y++){
if (x < 0 || x >= grid.length || y<0 || y >= grid.length){
continue;
}
if (grid[x][y].equals(grid[row][col])){
continue;
}
if (grid[x][y].isAlive()){
aliveCells++;
}
}
}
System.out.println(aliveCells);
return aliveCells;
}
private void setupButtons() {
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startLoop();
}
});
randomButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int initialAliveCells = ((size/2) * (size/5)); //totally random, but it makes up a good amount of alive points
if (initialAliveCells % 2 != 0){ //need a pair number to form couples
initialAliveCells--;
}
Random randomSeed = new Random();
ArrayList<Integer> initialIndexes = new ArrayList<Integer>();
for (int p = 0; p <initialAliveCells; p++){
initialIndexes.add(randomSeed.nextInt(size));
}
for (int x = 1; x <= initialIndexes.size(); x += 2){
grid[initialIndexes.get(x - 1)][initialIndexes.get(x)].setAliveStatus(true);
}
}
});
}
private void createFirstGrid(){
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
grid[row][col] = new Cell();
grid[row][col].setAliveStatus(false);
center.add(grid[row][col]);
}
}
}
}
有问题的方法是getAliveNeighbours方法:
private int getAliveNeighbours(Cell[][] grid, int row, int col){
int aliveCells = 0;
for (int x = row - 1; x <= row + 1; x++){
for (int y = col - 1; y <= col; y++){
if (x < 0 || x >= grid.length || y<0 || y >= grid.length){ //This check shouldn't be needed as i never pass cells in the border
continue;
}
if (grid[x][y].equals(grid[row][col])){ //this is to avoid counting the current cell
continue;
}
if (grid[x][y].isAlive()){
aliveCells++;
}
}
}
System.out.println(aliveCells);
return aliveCells;
}
private int getAliveNeighbours(单元格[][]网格,int行,int列){
int aliveCells=0;
对于(int x=row-1;x而言,错误消息具有欺骗性。问题实际上存在于前面的if
语句中:
&&
的优先级高于| |
。添加括号将| |
条件分组在一起
if (grid[row][col].isAlive() && (aliveNeighbours == 2 || aliveNeighbours == 3))
如果没有它们,则解析为:
if ((grid[row][col].isAlive() && aliveNeighbours == 2) || aliveNeighbours == 3)
通过这种方式解释,编译器注意到,else if(!grid[row][col].isAlive()&&aliveNeighbours==3)
无法触发。当aliveNeighbors==3
时,如果则代码将始终输入第一个if
,如果则从不输入else。这就是它抱怨的原因。但是else if
不是问题所在,而是上面的if
经验教训:错误并不总是出现在标记的行上。有时你必须查找以找到真正的问题
if (grid[row][col].isAlive() && (aliveNeighbours == 2 || aliveNeighbours == 3))
if ((grid[row][col].isAlive() && aliveNeighbours == 2) || aliveNeighbours == 3)