`
metaphy
  • 浏览: 339032 次
  • 性别: Icon_minigender_1
  • 来自: 大西洋底
社区版块
存档分类
最新评论

有趣的“生命游戏”

 
阅读更多
“生命游戏”
本世纪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
------------------------
分享到:
评论

相关推荐

    生命游戏(golly)v3.2安装包

    约翰·康威最常被专业人士和大众拿来讨论的成果,就是他在1970年发明的生命游戏,Game of Life。它的意义在于验证了某些科学家的宇宙观,即最简单的逻辑规则能产生出复杂有趣的活动。 康威生命游戏在方格网上进行,...

    game-of-life-n:在n维空间中表达Conway的生命游戏的框架

    生命的n游戏关于这是一个用任意大小的n多维数据集表达Conway的“生命游戏”的框架。 生命游戏n最初是在Reginald Braithwaite的JavaScriptAllongé的启发下使用ES2015进行函数式编程的一种练习。 该练习比我预期的更...

    非常有趣的移动接收板小游戏(C#编写)

    这是移动接收板小游戏,是我用C#编写的一个窗体应用程序WindowsFormsApplication,程序简介:左右方向键控制最下方接收板的移动方向,上方会随机下落各种物品,接收板触碰到不同物品分别会产生不同作用,这些物品...

    swift-gameoflife, 以SpriteKit为例,生命中的Conway游戏.zip

    swift-gameoflife, 以SpriteKit为例,生命中的Conway游戏 Swift Life用SpriteKit写的生命的conway游戏 直接跳转到代码的有趣部分:逻辑逻辑。显示单元格的大小。一些测试。作者Yonatan Bergman @yonbergman

    Typescript-Life:康威在 Typescript 中的生命游戏

    Conway's Life 并不是真正的游戏,它是一个元胞自动机,在我们看来,它模拟了人口的进化。 因此,没有进行任何“移动”。 相反,棋盘(或世界)被填充,然后应用规则来创建新的人口。 这一次又一次地进行,以便人口...

    game_of_life:“生命游戏”进化模拟器的简单有趣的实现

    游戏人生“生活游戏”进化模拟器的简单有趣实现

    py游戏源码-动作射击游戏.zip

    游戏叫做"Bunnies and Badgers",是一个动作射击游戏。在游戏中,你将控制一只可爱的兔子,与獾进行激烈的战斗。你需要使用鼠标控制兔子的移动,并射出...游戏画面清新可爱,操作简单有趣,是一款休闲娱乐的好选择!

    基于Java的动物拯救游戏

    此外,该游戏还可以作为一种娱乐方式,因为它提供了有趣的游戏机制和挑战。玩家需要在游戏中完成各种任务,例如营救被困的动物,收集食物和水,并与其他玩家竞争。这些任务不仅可以帮助玩家了解动物的生存环境,还...

    Scratch 音乐节奏游戏:Beat Bang.sb3

    Beat Bang 是一款音乐节奏棒球游戏,游戏中玩家将挑战一个个有趣的音乐关卡,玩家需要通过音乐的节奏来不断的完成歌曲的跳跃移动,小心触碰岩浆,它们会让你减少生命。 【↑↓ / WS / 鼠标点击】移动玩家。你有3点...

    Scratch打怪游戏作品:生命矿洞

    【空格】可选择“近战型”或“远攻型”英雄,【鼠标】攻击...此后仍有作品或有趣游戏、爆笑作品,请关注原作者,且点赞加收藏,记得推荐好友。下载即可游玩,快来下载吧!五星好评可以私信我,免费送资源!快来评论吧!

    锻炼游戏策划能力的40个方法

    如果你用最少的表达手段就能做出有趣的东西来,那么在拥有更好的表达手段时,它将更加有趣。 分析: 一个好的策划能够利用有限的资源作出有意思的游戏。比方说比较知名的打企鹅系列。这是策划的基本功,不可以忽略的...

    初中英语课堂中的游戏教学.doc

    游戏教学方法就是在教学中尽可能将枯燥的语言现象转变为学生乐于接受、生动有趣的游戏形式,为学生创造丰富的语言交际情景,使学生在玩中学、学中玩,是可以在初中英语课堂中应用的教学方法。 【关键词】 初中 英语...

    新颖的基于细胞自动机的图像分割算法:该方法基于康威生命游戏的第一个时代,并修改规则对图像进行分割-matlab开发

    2015 名称:新型基于元胞自动机的图像分割算法网址: https : //fr.mathworks.com/matlabcentral/fileexchange/50989-novel-cellular-automaton-based-image-segmentation-algorithm 这是一种基于康威生命游戏第一...

    Scratch 躲避游戏:大气层 音乐游戏

    角色数量:19,素材数量:76,积木数量:2425,音频数量:...此后仍有作品或有趣游戏、爆笑作品,请关注原作者,且点赞加收藏,记得推荐好友。下载即可游玩,快来下载吧!五星好评可以私信我,免费送资源!快来评论吧!

    MATLAB白细胞计数代码-simmunity-gameoflife:实施Conway的“生命游戏”以展示免疫系统

    Conway的“生命游戏”的实现,以展示免疫系统。 背景 该程序实现了约翰·霍顿·康威(John Horton Conway)发明的康威人生游戏的变体。 您可以阅读更多有关它的内容。 更多关于 。 更多关于我的。 该模拟是通过Java...

    JavaScript 武士格斗游戏源代码

    游戏玩起来很有趣;这是一款竞争激烈的格斗游戏,由两名玩家决定谁是最好的武士。当时间用完或玩家的生命值达到零时,游戏将决定获胜者。与您的朋友一起尝试这款游戏,享受这款简单的独立砍杀格斗游戏。 JavaScript ...

    Scratch 塔防游戏:连锁防御

    注意事项:攻速没有上限,当总攻速达到20/秒时,会将部分...此后仍有作品或有趣游戏、爆笑作品,请关注原作者,且点赞加收藏,记得推荐好友。下载即可游玩,快来下载吧!五星好评可以私信我,免费送资源!快来评论吧!

    Scratch闯关游戏作品:拉闸行动

    WASD或方向键移动,按E拾取武器,按C切换武器,按鼠标使用武器攻击,或查看“新手教程”指导。 一般玩家得到手枪和五个弹匣的...此后仍有各热门或有趣游戏,请关注原作者,且点赞加收藏,记得推荐好友。快点来玩吧!

    Sweet Candy Match 3 甜蜜糖果三消unity类糖果消除游戏项目源码

    它为那些被彩色糖果所吸引的人提供了一款令人兴奋、有趣且具有挑战性的游戏。 如果您是美味糖果益智游戏的忠实粉丝,《Sweet Candy》可以让您在一阵糖果狂潮中收集并匹配三个或更多令人垂涎欲滴的糖果。该游戏具有六...

    MATLAB中有趣的演示实例

    life :生命发展游戏 ◆ 三维效果图 klein1 :肤色三维效果图 tori4 :四个首尾相接的圆环 spharm2 : 球形和声 cruller :类似油饼的东西 xpklein : Klein瓶 bottle modes : L-形薄膜的12中模态 logo :...

Global site tag (gtag.js) - Google Analytics