为了各位能够更直观的知道自己写对了没有,所以我把固定的图给你,让你做参照
游戏规则:以一个17*17的正方形矩阵为例,每一个细胞都不会移动,但是会根据周围存活的细胞而决定自己的生死
1、细胞过少:当这一状态周围存活细胞数小于2时,该细胞下一状态死亡。
2、细胞稳定:当这一状态周围存活细胞数等于2时,该细胞下一状态保持原样。
3、细胞繁殖:当这一状态周围存活细胞数等于3时,该细胞下一状态复活并存活。
4、细胞过剩:当这一状态周围存活细胞数大于3时,该细胞下一状态死亡。
以下白色的为死细胞,黑色的为活细胞
我把[0]代表死细胞,[@]代表活细胞
import java.util.Arrays;
import java.util.Scanner;
class Life
{
private int length;
private int width;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public void FirstVal(int[][] arr)//这些是初始化,不要被吓到,你也可以直接使用rand方法随机生成
{
arr[2][4] = 7;
arr[2][5] = 7;
arr[2][6] = 7;
arr[2][10] = 7;
arr[2][11] = 7;
arr[2][12] = 7;
arr[4][2] = 7;
arr[4][7] = 7;
arr[4][9] = 7;
arr[4][14] = 7;
arr[5][2] = 7;
arr[5][7] = 7;
arr[5][9] = 7;
arr[5][14] = 7;
arr[6][2] = 7;
arr[6][7] = 7;
arr[6][9] = 7;
arr[6][14] = 7;
arr[7][4] = 7;
arr[7][5] = 7;
arr[7][6] = 7;
arr[7][10] = 7;
arr[7][11] = 7;
arr[7][12] = 7;
arr[9][4] = 7;
arr[9][5] = 7;
arr[9][6] = 7;
arr[9][10] = 7;
arr[9][11] = 7;
arr[9][12] = 7;
arr[10][2] = 7;
arr[10][7] = 7;
arr[10][9] = 7;
arr[10][14] = 7;
arr[11][2] = 7;
arr[11][7] = 7;
arr[11][9] = 7;
arr[11][14] = 7;
arr[12][2] = 7;
arr[12][7] = 7;
arr[12][9] = 7;
arr[12][14] = 7;
arr[14][4] = 7;
arr[14][5] = 7;
arr[14][6] = 7;
arr[14][10] = 7;
arr[14][11] = 7;
arr[14][12] = 7;
}
public int[][] state(int[][] arr)
{
int[][] arr1 = new int[this.length][this.width];
for(int x = 0; x<arr.length; x++)
{
for(int y = 0; y<arr[x].length; y++)
{
arr1[x][y] = arr[x][y];
}
}
for(int n = 1; n<length-1; n++)
{
for(int i = 1; i<width-1; i++)
{
int count = 0;
if(arr[n-1][i-1] == 7)
{
count++;
}
if(arr[n-1][i] == 7)
{
count++;
}
if(arr[n-1][i+1] == 7)
{
count++;
}
if(arr[n][i-1] == 7)
{
count++;
}
if(arr[n][i+1] == 7)
{
count++;
}
if(arr[n+1][i-1] == 7)
{
count++;
}
if(arr[n+1][i] == 7)
{
count++;
}
if(arr[n+1][i+1] == 7)
{
count++;
}
if(count < 2 || count > 3)
{
arr1[n][i] = 0;
}
if(count == 2)
{
arr1[n][i] = arr1[n][i];
}
if(count == 3)
{
arr1[n][i] = 7;
}
}
}
for(int x = 0; x<this.length; x++)
{
for(int y = 0; y<this.width; y++)
{
if(arr[x][y] == 7)
{
System.out.print("[@]");
}
else
{
System.out.print("[0]");
}
}
System.out.println();
}
return arr1;
}
public int recept(int length, int width,int[][] arr)
{
int count = 0;
if(arr[length-1][width-1] == 7)
{
count++;
}
if(arr[length-1][width] == 7)
{
count++;
}
if(arr[length-1][width+1] == 7)
{
count++;
}
if(arr[length][width-1] == 7)
{
count++;
}
if(arr[length][width+1] == 7)
{
count++;
}
if(arr[length+1][width-1] == 7)
{
count++;
}
if(arr[length+1][width] == 7)
{
count++;
}
if(arr[length+1][width+1] == 7)
{
count++;
}
if(count < 2 || count > 3)
{
arr[length][width] = 0;
}
if(count == 2)
{
arr[length][width] = arr[length][width];
}
if(count == 3)
{
arr[length][width] = 7;
}
return arr[length][width];
}
}
public class LifeGame {
//0——代表死亡 1——代表存活
//1.周围存活细胞数<2,下一状态死亡
//2.周围存活细胞=2,下一状态不变
//3.周围存活细胞=3,该细胞下一状态立即复活并存活
//4.周围细胞>3,该细胞下一状态死亡
//总结:周围细胞数<2 或者 >3死亡 =2状态不变 =3存活
public static void main(String[] args) {
Life li = new Life();
li.setLength(17);
int length = li.getLength();
li.setWidth(17);
int width = li.getWidth();
int[][] bol = new int[length][width];
li.FirstVal(bol);
while(true)
{
System.out.println("=================================");
System.out.println("=================================");
System.out.println("=================================");
System.out.println("=================================");
System.out.println("=================================");
bol = li.state(bol);
Scanner sc = new Scanner(System.in);//这个代码的意思是输入暂停
int n = sc.nextInt();
}
}
}