“生命游戏”
本世纪70年代,人们曾疯魔一种被称作“生命游戏”的小游戏,这种游戏相当简单。假设有一个像棋盘一样的方格网,每个方格中放置一个生命细胞,生命细胞只有两种状态:“生”或“死”。游戏规则如下:
1、如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生,即该细胞若原先为死,则转为生,若原先为生,则保持不变;
2、如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
3、在其它情况下,该细胞为死,即该细胞若原先为生,则转为死,若原先为死,则保持不变。
依此规则进行迭代变化,使细胞生生死死,会得到一些有趣的结果。该游戏之所以被称为“生命游戏”,是因为其简单的游戏规则,反映了自然界中的生存规律:如果一个生命,其周围的同类生命太少的话,会因为得不到帮助而死亡;如果太多,则会因为得不到足够的资源而死亡。
某一次的output
本世纪70年代,人们曾疯魔一种被称作“生命游戏”的小游戏,这种游戏相当简单。假设有一个像棋盘一样的方格网,每个方格中放置一个生命细胞,生命细胞只有两种状态:“生”或“死”。游戏规则如下:
1、如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生,即该细胞若原先为死,则转为生,若原先为生,则保持不变;
2、如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
3、在其它情况下,该细胞为死,即该细胞若原先为生,则转为死,若原先为死,则保持不变。
依此规则进行迭代变化,使细胞生生死死,会得到一些有趣的结果。该游戏之所以被称为“生命游戏”,是因为其简单的游戏规则,反映了自然界中的生存规律:如果一个生命,其周围的同类生命太少的话,会因为得不到帮助而死亡;如果太多,则会因为得不到足够的资源而死亡。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
public class LifeGame {
// Sample file name
private static final String SAMPLE_FILE_NAME = "c:/lifegame_data.txt";
// random
private static final Random RAND = new Random();
//Area width and height
private int width , height;
//Area
private boolean area[][];
public LifeGame() {}
/**
* init an area with random state of perc/total lives
*
* @param width
* @param height
* @param perc
* @param total
*/
public void load (int width, int height, int perc, int total) {
if (width < 1 || height < 1 || perc < 0 || total < 1 || perc > total)
throw new IllegalArgumentException ();
this.width = width;
this.height = height;
area = new boolean[height][width];
for (int h = 0 ; h < height; h++) {
for (int w = 0; w < width; w++){
area[h][w] = randLive(perc, total);
}
}
}
/**
* init an area with around 1/3 lives
* @param width
* @param height
*/
public void load (int width, int height){
load (width, height, 1, 3) ;
}
/**
* Random live
* @return
*/
private boolean randLive(int perc, int total) {
int r = RAND.nextInt(total);
return perc > r;
}
/**
* Init the Life game with a file, for example
1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1
1 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
* @param file
*/
public void load (File file){
try {
BufferedReader in = new BufferedReader (new FileReader(file));
String line = null;
while ((line = in.readLine())!=null && !(line = line.trim()).equals("")) {
height ++;
String[] data = line.split("\\s");
if (height == 1) {
width = data.length;
}
}
area= new boolean[height][width];
in.close();
in = new BufferedReader (new FileReader(file));
line = null;
int h = -1;
while ((line = in.readLine())!=null && !(line = line.trim()).equals("")) {
h++;
String[] data = line.split("\\s");
for (int w =0; w <data.length; w++){
if (data[w].trim().equals("1")){
area[h][w] = true;
}
}
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Generation
*/
public void generation(int gen) {
if (gen < 1) throw new IllegalArgumentException();
for (int g = 0 ; g < gen ; g++){
boolean [][] newarea = new boolean[height][width];
for (int h =0; h<height; h++) {
for (int w = 0 ; w < width; w++){
int lives = 0;
if (h-1 >=0 && w-1>=0 && area[h-1][w-1]) lives ++;
if (h-1 >= 0 && area[h-1][w]) lives ++;
if (h-1 >= 0 && w+1 < width && area[h-1][w+1]) lives ++;
if (w-1 >= 0 && area[h][w-1]) lives ++;
if (w+1 < width && area[h][w+1]) lives ++;
if (w-1 >= 0 && h+1 < height && area[h+1][w-1]) lives ++;
if (h+1 < height && area[h+1][w]) lives ++;
if (w+1 < width && h+1 < height && area[h+1][w+1]) lives ++;
if (lives == 3) {
newarea[h][w] = true;
} else if (lives == 2) {
newarea[h][w] = area[h][w];
} else {
newarea[h][w] = false;
}
}
}
area = newarea;
// print the new state after each 1 generation
System.out.println ("Generation: "+ (g+1));
print();
}
}
/**
* Generation, default for 1 time
*/
public void generation() {
generation (1);
}
/**
* Print
*/
public void print() {
int lives = 0 ;
for (int h = 0; h < height; h++){
for (int w = 0; w < width; w ++){
if (area[h][w]){
lives ++ ;
System.out.print("● ");
} else {
System.out.print("○ ");
}
}
System.out.println("");
}
System.out.println("Lives = " + lives + ", total = " + height * width );
System.out.println ("------------------------");
}
/**
* @param args
*/
public static void main(String[] args) {
LifeGame lifegame = new LifeGame();
lifegame.load (10, 10);
lifegame.print(); // print the original state
lifegame.generation(6);
}
}
某一次的output
● ○ ○ ○ ● ○ ○ ● ○ ○
○ ○ ○ ○ ○ ● ● ○ ○ ○
● ○ ○ ○ ● ○ ○ ○ ● ○
● ● ● ● ○ ● ○ ● ● ●
● ○ ○ ○ ● ○ ● ○ ● ○
○ ○ ● ○ ○ ○ ○ ● ○ ●
○ ○ ○ ● ○ ○ ● ● ● ○
○ ○ ○ ● ● ○ ○ ● ○ ●
● ○ ○ ○ ● ○ ● ● ○ ○
○ ○ ○ ● ○ ○ ○ ○ ○ ○
Lives = 36, total = 100
------------------------
Generation: 1
○ ○ ○ ○ ○ ● ● ○ ○ ○
○ ○ ○ ○ ● ● ● ● ○ ○
● ○ ● ● ● ○ ○ ○ ● ●
● ○ ● ● ○ ● ● ○ ○ ●
● ○ ○ ○ ● ● ● ○ ○ ○
○ ○ ○ ● ○ ● ○ ○ ○ ●
○ ○ ● ● ● ○ ● ○ ○ ●
○ ○ ○ ● ● ○ ○ ○ ○ ○
○ ○ ○ ○ ● ● ● ● ● ○
○ ○ ○ ○ ○ ○ ○ ○ ○ ○
Lives = 37, total = 100
------------------------
Generation: 2
○ ○ ○ ○ ● ○ ○ ● ○ ○
○ ○ ○ ○ ○ ○ ○ ● ● ○
○ ○ ● ○ ○ ○ ○ ○ ● ●
● ○ ● ○ ○ ○ ● ● ● ●
○ ● ● ○ ○ ○ ○ ○ ○ ○
○ ○ ● ○ ○ ○ ○ ○ ○ ○
○ ○ ● ○ ○ ○ ○ ○ ○ ○
○ ○ ● ○ ○ ○ ○ ○ ● ○
○ ○ ○ ● ● ● ● ● ○ ○
○ ○ ○ ○ ○ ● ● ● ○ ○
Lives = 27, total = 100
------------------------
Generation: 3
○ ○ ○ ○ ○ ○ ○ ● ● ○
○ ○ ○ ○ ○ ○ ○ ● ○ ●
○ ● ○ ○ ○ ○ ● ○ ○ ○
○ ○ ● ● ○ ○ ○ ● ○ ●
○ ○ ● ● ○ ○ ○ ● ● ○
○ ○ ● ● ○ ○ ○ ○ ○ ○
○ ● ● ● ○ ○ ○ ○ ○ ○
○ ○ ● ○ ● ● ● ● ○ ○
○ ○ ○ ● ● ○ ○ ○ ● ○
○ ○ ○ ○ ○ ○ ○ ● ○ ○
Lives = 28, total = 100
------------------------
Generation: 4
○ ○ ○ ○ ○ ○ ○ ● ● ○
○ ○ ○ ○ ○ ○ ● ● ○ ○
○ ○ ● ○ ○ ○ ● ● ○ ○
○ ● ○ ● ○ ○ ● ● ○ ○
○ ● ○ ○ ● ○ ○ ● ● ○
○ ○ ○ ○ ● ○ ○ ○ ○ ○
○ ● ○ ○ ○ ● ● ○ ○ ○
○ ● ○ ○ ○ ● ● ● ○ ○
○ ○ ○ ● ● ○ ○ ○ ● ○
○ ○ ○ ○ ○ ○ ○ ○ ○ ○
Lives = 26, total = 100
------------------------
Generation: 5
○ ○ ○ ○ ○ ○ ● ● ● ○
○ ○ ○ ○ ○ ○ ○ ○ ○ ○
○ ○ ● ○ ○ ● ○ ○ ● ○
○ ● ○ ● ○ ● ○ ○ ○ ○
○ ○ ● ● ● ● ● ● ● ○
○ ○ ○ ○ ● ○ ● ● ○ ○
○ ○ ○ ○ ● ○ ○ ● ○ ○
○ ○ ● ○ ○ ○ ○ ● ○ ○
○ ○ ○ ○ ● ● ● ● ○ ○
○ ○ ○ ○ ○ ○ ○ ○ ○ ○
Lives = 27, total = 100
------------------------
Generation: 6
○ ○ ○ ○ ○ ○ ○ ● ○ ○
○ ○ ○ ○ ○ ○ ● ○ ● ○
○ ○ ● ○ ● ○ ○ ○ ○ ○
○ ● ○ ○ ○ ○ ○ ○ ● ○
○ ○ ● ○ ○ ○ ○ ○ ● ○
○ ○ ○ ○ ○ ○ ○ ○ ○ ○
○ ○ ○ ● ○ ● ○ ● ● ○
○ ○ ○ ● ● ○ ○ ● ● ○
○ ○ ○ ○ ○ ● ● ● ○ ○
○ ○ ○ ○ ○ ● ● ○ ○ ○
Lives = 22, total = 100
------------------------