我试图为老师解决人生游戏问题。该游戏的规则是:
任何少于两个活邻居的活细胞都会死亡,就像由人口不足引起的一样。任何活细胞,有两三个活邻居的生命,传给下一代。任何具有三个以上活邻居的活细胞都会死亡,就像过度拥挤一样。任何正好有三个活邻居的死细胞都会变成活细胞,就像通过繁殖一样。
我的代码有两个问题 - 首先,我的主类似乎不起作用。其次,我通过许多如果不是其他语句来执行问题。是否有更简洁的方法来编写我的 getNeighbors() 方法的异常?
谢谢!
import java.util.Random;
public class GameOfLife {
final static int ROWS = 6;
final static int COLUMNS = 7;
String[][] simulator;
private Random randomGenerator;
public GameOfLife() {
simulator = new String[ROWS][COLUMNS];
randomGenerator = new Random();
}
public void fillSpot (int row, int column) {
simulator [row][column] = "O";
}
private void deleteSpot (int row, int column) {
simulator[row][column] = "";
}
// Do I need the above methods? really?
public void randomSimulation() {
for (int i = 0; i <= ROWS; i++) {
for (int j = 0; j <= COLUMNS; j++) {
int random = randomGenerator.nextInt(1);
if (random == 1) {
fillSpot(i,j);
}
}
}
}
private void getNeighbors (int row, int column) {
int neighbors = 0;
if (row < ROWS && row > 0 && column < COLUMNS && column > 0) {
for (int i = row - 1; i <= row + 1; i++) {
for (int j = column - 1; j <= column + 1; j++) {
String temp = simulator[i][j];
if (temp.contains("O")) {
neighbors++;
}
}
}
}
if (row > ROWS || column > COLUMNS || row < 0 || column < 0) {
}
else if (row == ROWS && column < COLUMNS && column != 0) {
for (int i = row - 1; i <= ROWS; i++) {
for (int j = column - 1; j <= column + 1; j++) {
String temp = simulator[i][j];
if (temp.contains("O")) {
neighbors++;
}
}
}
}
else if (row < ROWS && column == COLUMNS && row != 0) {
for (int i = row - 1; i <= row + 1; i++) {
for (int j = column - 1; j <= COLUMNS; j++) {
String temp = simulator[i][j];
if (temp.contains("O")) {
neighbors++;
}
}
}
}
else if (row == 0 && column < COLUMNS && column != 0) {
for (int i = 0; i <= row + 1; i++) {
for (int j = column - 1; j <= COLUMNS + 1; j++) {
String temp = simulator[i][j];
if (temp.contains("O")) {
neighbors++;
}
}
}
}
else if (row == 0 && column == COLUMNS) {
for (int i = row; i <= row + 1; i++) {
for (int j = column - 1; j <=COLUMNS; j++) {
String temp = simulator[i][j];
if (temp.contains("O")) {
neighbors++;
}
}
}
}
else if (column == 0 && row < ROWS && row != 0) {
for (int i = row - 1; i <= row + 1; i++) {
for (int j = column; j <= COLUMNS + 1; j++) {
String temp = simulator[i][j];
if (temp.contains("O")) {
neighbors++;
}
}
}
}
else {
for (int i = row; i <= row + 1; i++) {
for (int j = column; j <= column + 1; j++) {
String temp = simulator[i][j];
if (temp.contains("O")) {
neighbors++;
}
}
}
}
// for row == 0 && column == 0
if (simulator [row][column].contains("O")) {
neighbors--;
}
simulator[row][column] += " " + neighbors;
}
//There are wayyy too manyy clauses here for me to be comfortable. There's got to be a way to do this cleaner
private void nextGenPlanning() {
for (int i = 0; i <= ROWS; i++) {
for (int j = 0; j <= COLUMNS; j++) {
getNeighbors(i,j);
}
}
}
private void nextGen() {
nextGenPlanning();
for (int i = 0; i <= ROWS; i++) {
for (int j = 0; j <= COLUMNS; j++) {
String temp = simulator[i][j];
if (temp.charAt(temp.length()) <= 1 || temp.charAt(temp.length()) >= 4) {
deleteSpot(i,j);
}
else {
fillSpot (i,j);
}
}
}
}
public String toString() {
String string = "";
for (int i = 0; i < ROWS; i++) {
string = string + "|";
for (int j = 0; j < COLUMNS; j++) {
String temp = simulator[i][j];
string = string + temp.charAt(0);
}
string = string + "|n";
}
return string;
}
public String simulate (int numOfTrials) {
String string = "";
for (int i = 0; i <= numOfTrials; i++) {
nextGen();
string += toString();
}
return string;
}
public void main (String [] args) {
randomSimulation();
System.out.println(simulate(2));
}
}
首先,你有:
public void main (String [] args) {
randomSimulation();
System.out.println(simulate(2));
}
您应该具备:
public static void main (String[] args) {
GameOfLife game = new GameOfLife();
game.randomSimulation();
System.out.println(game.simulate(2));
}
其次,对于getNeighbors,首先要考虑"get"方法通常返回一个值。如果要计算邻居的数量,请考虑:
public int getNeighbors(int x, int y) {
int neighbors = 0;
int leftX = Math.max(x-1, 0);
int rightX = Math.min(x+1, COLUMNS);
int topY = Math.max(y-1, 0);
int bottomY = Math.min(y+1, ROWS);
for (int i=leftX; i < rightX; i++) {
for (int j=topY; j < bottomY; j++) {
if (simulator[i][j].contains('O')) { // Notice I'm using a char here, see my next comment
neighbors++;
}
}
}
return neighbors;
}
第三,如果模拟器中的每个空格只包含一个字符值,我建议对模拟器使用 char[][]
而不是 String[][]
。在 Java 中有一些关于字符串的事情你不需要被绊倒 - 例如,在 Java 中,你不能使用 == 比较字符串的值(你需要使用 String 的 equals()
方法)。
首先,你的主类应该是Public static void main(String[] args)
的,你可以使用开关大小写,除非你真的确定是否可以使用if(blabla == blablabla &(这意味着和)blaba == babalaba)