生命游戏的规则:
生命游戏中,对于任意细胞:
每个细胞有两种状态:存活或死亡。每个细胞与以自身为中心的周围八格细胞产生互动。
1.当前细胞为存活状态时,当周围的活细胞低于2个时, 该细胞因孤独而死亡;
2.当前细胞为存活状态时,当周围有2个或3个活细胞时, 该细胞保持原样;
3.当前细胞为存活状态时,当周围有3个以上活细胞时,该细胞因资源匮乏而死亡;
4.当前细胞为死亡状态时,当周围有3个活细胞时,该细胞变成存活状态(模拟繁殖)。**
①一般情形:
②滑翔者:每4个回合"它"会向右下角走一格。虽然细胞早就是不同的细胞了,但它能保持原本的形态:
③轻量级飞船:它的周期是4,每2个回合会向右边走一格:
④脉冲星:它的周期为3,看起来像一颗周期爆发的星星:
程序运行java源代码:
import java.util.Scanner;public class lifeGame {static final int ROWLEN = 10, COLLEN = 10, DEAD = 0, ALIVE = 1;static int[][] cell = new int[ROWLEN][COLLEN];//定义临时二维空间数组存储变化的状态:static int[][] celltemp = new int[ROWLEN][COLLEN];public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("生命游戏开始: ");//二维空间中放置细胞初始化数量initCell();//显示初始状态showCell();//是否进行下一次演化String flag;do {//开始生命游戏startCell();System.out.println("是否进行下一次演化: y/n");flag = input.next();showCell();} while (flag.equalsIgnoreCase("y"));}private static void initCell() {int row, col;Scanner input = new Scanner(System.in);for (row = 0; row < 10; row++) {for (col = 0; col < 10; col++) {cell[row][col] = DEAD;//全部初始化为死状态}}System.out.println("请输入一组活细胞的坐标位置,输入(-1,-1)代表结束");while (true) {System.out.println("请输入一组活细胞的坐标位置:");row = input.nextInt();col = input.nextInt();if (0 <= row && row < ROWLEN && 0 <= col && col < COLLEN) {cell[row][col] = ALIVE;} else if (row == -1 && col == -1) {break;} else {System.out.println("输入坐标超过范围");}}}private static void startCell() {int row, col, sum, count = 0;for (row = 0; row < ROWLEN; row++) {for (col = 0; col < COLLEN; col++) {//每个单元格的细胞周围活着的细胞switch (sumCell(row, col)) {case 2:celltemp[row][col] = cell[row][col];break;case 3:celltemp[row][col] = ALIVE;break;default:celltemp[row][col] = DEAD;}}}//temp放回cellfor (row = 0; row < ROWLEN; row++) {for (col = 0; col < COLLEN; col++) {cell[row][col] = celltemp[row][col];if (celltemp[row][col] == ALIVE) {count++;//演化后当前存活的细胞数量}}}showCell();System.out.println("当前状态下存活的细胞数量为:" + count);}//统计四周的细胞个数static int sumCell(int row, int col) {int count = 0;int c, r;//上一行,下一行,左边列,右边列for (r = row - 1; r < row + 1; r++) {//从上一行循环到下一行for (c = col - 1; c < col + 1; c++) {if (r < 0 || r >= ROWLEN || c < 0 || c >= COLLEN) {continue;}if (cell[r][c] == ALIVE) {count++;}}}if (cell[row][col] == ALIVE) {count--;}return count;}//统计四周的细胞个数private static void showCell() {int row, col;System.out.printf("\n细胞状态\n");System.out.printf("┌-");//第一行的左上角for (col = 0; col < COLLEN - 2; col++) {//第一行System.out.printf("-┬-");}System.out.printf("┐\n");//第一行的右上角for (row = 0; row < ROWLEN; row++) {System.out.printf("|");//行的最左边的线for (col = 0; col < COLLEN; col++) {//各单元格细胞的生成状态switch (cell[row][col]) {case ALIVE:System.out.printf("●|");break;case DEAD:System.out.printf("○|");break;}}System.out.println();//换行//输出行与行之间的水平分割线:if (row < ROWLEN - 1) {System.out.printf("├-");for (col = 0; col < COLLEN - 2; col++) {System.out.printf("-┼-");//输出一行水平分割线}System.out.printf("┤\n");}}//输出最后一行:System.out.printf("└-");for (col = 0; col < COLLEN -2 ; col++) {System.out.printf("-┴-");}System.out.printf("┘\n");}
}