我已经做了很长一段时间的实验,但我就是看不出我的错误是在检查周围的生命!你们能看看我的代码,看看我的错误在哪里吗?代码中的所有内容在检查周围生命之外都是完全有效的。
对于那些不熟悉生活的人,这里有一些生活游戏的规则:
https://bitstorm.org/gameoflife/
import java.util.*;
import java.lang.*;
import java.io.*;
class Life
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String userin = in.nextLine();
int x = 8;
int y = 8;
int [][] visualize;
if(userin.equals("glider"))
{
visualize = new int [][]{
//0 is used as a boundary, 1 represents a dead cell, 2 represents an alive cell
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 2, 1, 1, 0},
{0, 1, 1, 1, 1, 2, 1, 0},
{0, 1, 1, 2, 2, 2, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
for (int i = 0; i <= 7; i++)
{
for (int j = 0; j <= 7; j++)
{
if (visualize[i][j] == 2) {
System.out.print("*");
} else if (visualize[i][j] == 1)
{
System.out.print(".");
} else if (visualize[i][j] == 0)
{
System.out.print("_");
}
}
System.out.println();
}
nextGen(visualize, x, y);
} else if(userin.equals("own"))
{
visualize = new int [8][8];
for(int o = 1; o <= 6; o++) //Starting it a pos 1 means pos 0 is automaically filled with a "0", which is used as the boundary
{
visualize[o] = new int[x];
for(int p = 1; p <= 6; p++) //Starting it a pos 1 means pos 0 is automaically filled with a "0", which is used as the boundary
{
visualize[o][p] = in.nextInt();
}
System.out.println(0);
System.out.println();
}
for (int i = 0; i <= 7; i++)
{
for (int j = 0; j <= 7; j++)
{
if(visualize[i][j] == 2) {
System.out.print("*");
} else if (visualize[i][j] == 1)
{
System.out.print(".");
} else if (visualize[i][j] == 0)
{
System.out.print("_");
}
}
System.out.println();
}
nextGen(visualize, x, y);
}
else if(userin.equals("test"))
{
visualize = new int [][]{
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 2, 2, 1, 1, 1, 0},
{0, 1, 2, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
for (int i = 0; i <= 7; i++)
{
for (int j = 0; j <= 7; j++)
{
if (visualize[i][j] == 2) {
System.out.print("*");
} else if (visualize[i][j] == 1)
{
System.out.print(".");
} else if (visualize[i][j] == 0)
{
System.out.print("_");
}
}
System.out.println("_");
}
nextGen(visualize, x, y);
}
}
static void nextGen(int visualize[][], int x, int y)
{
int[][] life = new int[x][y];
int alive = 0;
//Starts at array-point 1,1
int startX = 1;
int startY = 1;
System.out.println();
System.out.println();
System.out.println("________");
System.out.print("_");
for(int repeat = startX; repeat <= 6; repeat++)
{
while(startY <= 6)
{
for(int k = startX; k <=6; k++)
{
for(int l = startY; l <=6; l++)
{
//BEGIN CHECK FOR SURROUNDING LIVES
if(!(visualize[startX + 1][startY] == 0))
{
if(visualize[startX + 1][startY] == 2) //Right 1
{
alive++;
}
}
if(!(visualize[startX][startY + 1] == 0))
{
if(visualize[startX][startY + 1] == 2) //Up 1
{
alive++;
}
}
if(!(visualize[startX + 1][startY + 1] == 0))
{
if(visualize[startX + 1][startY + 1] == 2) // Right 1, Up 1
{
alive++;
}
}
if(!(visualize[startX - 1][startY] == 0))
{
if(visualize[startX - 1][startY] == 2) //Left 1
{
alive++;
}
}
if(!(visualize[startX][startY - 1] == 0))
{
if(visualize[startX][startY - 1] == 2) // Down 1
{
alive++;
}
}
if(!(visualize[startX - 1][startY - 1] == 0))
{
if(visualize[startX - 1][startY - 1] == 2) //Left 1, Down 1
{
alive++;
}
}
if(!(visualize[startX + 1][startY - 1] == 0))
{
if(visualize[startX + 1][startY - 1] == 2) //Right 1, Down 1
{
alive++;
}
}
if(!(visualize[startX - 1][startY - 1] == 0))
{
if(visualize[startX - 1][startY - 1] == 2) //Left 1, Down 1
{
alive++;
}
}
//CHECKS IF THERE ARE EXACTLY 3 LIVES AROUND AN AREA. IF NOT, IT KILLS THAT AREA
if(alive == 3)
{
visualize[k][l] = 2;
} else {
visualize[k][l] = 1;
}
}
}
if (visualize[startX][startY] == 2) {
System.out.print("*");
} else if (visualize[startX][startY] == 1)
{
System.out.print(".");
}
//Performs the check going down the Y-axis
startY++;
}
System.out.println("_");
System.out.print("_");
//After
startX++;
startY = 1;
}
System.out.println("_______");
}
}