LifeGame

3 篇文章 0 订阅

生命游戏其实是一个零玩家游戏,英文名叫Game of Life,也称生命棋。它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞。一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。如果相邻方格活着的细胞数量过多,这个细胞会因为资源匮乏而在下一个时刻死去;相反,如果周围活细胞过少,这个细胞会因太孤单而死去。

游戏的规则就是:当一个方格周围有2或3个活细胞时,方格中的活细胞在下一个时刻继续存活;即使这个时刻方格中没有活细胞,在下一个时刻也会“诞生”活细胞。在这个游戏中,还可以设定一些更加复杂的规则,例如当前方格的状况不仅由父一代决定,而且还考虑祖父一代的情况。你还可以作为这个世界的上帝,随意设定某个方格细胞的死活,以观察对世界的影响。

出于对称性考虑,矩阵最右边与最左边相邻,最上边与最下边相邻,这样整个矩阵就模拟了一个奇怪的“球体”。

代码如下

/*
 * lifegame.cpp
 * Copyright (C) 2014 - Jiang
 *
 * lifegame.cpp is free software; you can redistribute it and/or modify
 * it under the terms of the Just-For-Fun Public License  or How Casually License as published 
 *
 * lifegame.cpp is distributed while I don't know whether it is useful.  
 *
 * See the Just-For-Fun Public License for more details, 
 * BUT You may not receive a copy of the Just-For-Fun Public License
 * along with lifegame.cpp. So, please see <http://www.ligelaige.com/just-for-fun/license/>.
 */

/*
 *             File Name: lifegame.cpp
 * Operating Environment: ubuntu linux
 *                Author: Jiang
 *                 Email: ligelaige@gmail.com
 *          Created Time: 2014-04-08 16:59:37.465070 
 */
/*
 * g++ lifegame.cpp -std=c++11 -o lifegame
 */

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstdio>

using namespace std;

class Cell
{
public:
	Cell (int row = 30, int col = 79, int num = 0) : RowLength(row), ColLength(col), count(num)//行 列 数据
	{
		cell = new int* [row];
		cellSum = new int* [row];		
		for (int i = 0; i < RowLength; ++i)
		{
			cell[i] = new int[col];
			cellSum[i] = new int [col];	
			for (int j = 0; j < ColLength; ++j)
			{
				cell[i][j] = 0;
				cellSum[i][j] = 0;
			}
		}
				
		int n = 0;
		srand((int)time(0));
		
		while (n < count)
		{			
			int i = rand() % RowLength;
			int j = rand() % ColLength; 
			
			n += ALIVE - cell[i][j];
			cell[i][j] = ALIVE;			
		}
	}
	
	int OutCell()
	{
		cout << "the num of cell: " << count << endl;
		char status[2][5] = {"○ ", "● "};
		for (int i = 0; i < RowLength; ++i)
		{			
			for (int j = 0; j < ColLength; ++j)
			{			
				cout << status[cell[i][j]];
				LinSum(i, j);
			}
			cout << "\n";
		}
		return count;
	}
	
	void CellFunc()
	{	
		count = 0;	
		for(int row = 0; row < RowLength; ++row)
		{
			for(int col = 0; col < ColLength; ++col)
			{
			
				cell[row][col] = cellSum[row][col]==2 ? cell[row][col] :
						 cellSum[row][col]==3 ? ALIVE : DEAD;
						 
				count += cell[row][col];					 				
			}
		}				
	}
	
private:
        const int DEAD = 0;
        const int ALIVE = 1;

	int RowLength, ColLength, count;
	int** cell;
	int** cellSum;
	
	void LinSum(int row, int col)
	{
		int count = 0;	
		for (int r = row-1; r < row+2; ++r)
		{
			for (int c = col-1; c < col+2; ++c)
			{
				count += cell[(r+RowLength) % RowLength][(c+ColLength) % ColLength];
			}
		}	
		cellSum[row][col] = count - cell[row][col];
	}
};

int main (void)
{
	int row = 30,col = 70;
	int count = row * col / 4;
	Cell cell = Cell(row, col, count);
	
	do
	{
		count = cell.OutCell();
		cell.CellFunc();
		getchar();		
	}while (count > 3);
	
	return 0;
}


补充

C#版完整代码下载 http://download.csdn.net/detail/huangjinqiang/7168363


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I cannot create a Python game by myself, but I can provide you with the basic structure and rules of the game of Life. The Game of Life is a cellular automaton game that was invented by John Horton Conway in 1970. It is played on a grid of cells, where each cell can be either alive or dead. The rules are as follows: 1. Any live cell with fewer than two live neighbors dies (underpopulation). 2. Any live cell with two or three live neighbors lives on to the next generation. 3. Any live cell with more than three live neighbors dies (overpopulation). 4. Any dead cell with exactly three live neighbors becomes a live cell (reproduction). To create a Python implementation of the Game of Life, you can use the following steps: 1. Create a grid of cells using a 2D list or numpy array. 2. Initialize each cell randomly as alive or dead. 3. Create a loop that will iterate through each cell in the grid. 4. For each cell, count the number of live neighbors. 5. Apply the rules of the game to determine whether the cell should live or die in the next generation. 6. Update the grid with the next generation of cells. 7. Repeat steps 4-6 for a set number of iterations or until the game reaches a stable state. Here is an example implementation of the Game of Life in Python: ``` import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation # Set the size of the grid and the number of iterations n = 100 iterations = 100 # Initialize the grid randomly grid = np.random.choice([0, 1], size=(n, n)) # Define a function to update the grid for each iteration def update(frame_number, grid, n): # Create a new grid to hold the next generation of cells new_grid = np.zeros((n, n)) # Loop through each cell in the grid for i in range(n): for j in range(n): # Count the number of live neighbors num_neighbors = (grid[(i-1)%n][(j-1)%n] + grid[(i-1)%n][j] + grid[(i-1)%n][(j+1)%n] + grid[i][(j-1)%n] + grid[i][(j+1)%n] + grid[(i+1)%n][(j-1)%n] + grid[(i+1)%n][j] + grid[(i+1)%n][(j+1)%n]) # Apply the rules of the game if grid[i][j] == 1 and (num_neighbors < 2 or num_neighbors > 3): new_grid[i][j] = 0 elif grid[i][j] == 0 and num_neighbors == 3: new_grid[i][j] = 1 else: new_grid[i][j] = grid[i][j] # Update the grid with the next generation of cells grid[:] = new_grid[:] # Plot the updated grid plt.imshow(grid, cmap='binary') # Create the animation fig = plt.figure() ani = animation.FuncAnimation(fig, update, frames=iterations, fargs=(grid, n), interval=50, blit=False) # Show the animation plt.show() ``` This code will create a random grid of cells and update it for a set number of iterations, following the rules of the Game of Life. The animation will show the evolution of the grid over time.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值