康威生命游戏程序
最初,有一个网格,其中包含一些可能是活的或死的细胞。我们的任务是根据以下规则生成下一代细胞:
- 任何少于两个活邻居的活细胞都会死亡,好像是由于人口不足造成的。
- 任何有两三个活邻居的活细胞都可以活到下一代。
- 任何有超过三个活邻居的活细胞都会死亡,就像人口过剩一样。
- 任何只有三个活邻居的死细胞都会变成活细胞,就像通过繁殖一样。
例子:
'*' 代表一个活细胞,而 '.'代表一个死细胞。
Input : ..........
...**.....
....*.....
..........
..........
Output: ..........
...**.....
...**.....
..........
..........
..........
Input : ..........
...**.....
....*.....
..........
..........
...**.....
..**......
.....*....
....*.....
..........
Output: ..........
...**.....
...**.....
..........
..........
..***.....
..**......
...**.....
..........
..........
这是生命游戏的简单Java实现。 Grid 初始化为 0 代表死细胞,1 代表活细胞。 generate()函数循环遍历每个单元格并计算其邻居。基于该值,实施上述规则。以下实现忽略了边缘单元,因为它应该在无限平面上播放。
C
#include
#include
//change row and column value to set the canvas size
int row = 5;
int col = 4;
//creates row boundary
int row_line(){
printf("\n");
for(int i=0; i =row || j>=col)){
continue;
}
if(a[i][j]==1){
count++;
}
}
}
return count;
}
int main(){
int a[row][col], b[row][col];
int i,j;
int neighbour_live_cell;
//generate matrix canvas with random values (live and dead cells)
for(i=0; i|
Java
// A simple Java program to implement Game of Life
public class GameOfLife
{
public static void main(String[] args)
{
int M = 10, N = 10;
// Initializing the grid.
int[][] grid = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
// Displaying the grid
System.out.println("Original Generation");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (grid[i][j] == 0)
System.out.print(".");
else
System.out.print("*");
}
System.out.println();
}
System.out.println();
nextGeneration(grid, M, N);
}
// Function to print next generation
static void nextGeneration(int grid[][], int M, int N)
{
int[][] future = new int[M][N];
// Loop through every cell
for (int l = 0; l < M; l++)
{
for (int m = 0; m < N; m++)
{
// finding no Of Neighbours that are alive
int aliveNeighbours = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
if ((l+i>=0 && l+i=0 && m+j 3))
future[l][m] = 0;
// A new cell is born
else if ((grid[l][m] == 0) && (aliveNeighbours == 3))
future[l][m] = 1;
// Remains the same
else
future[l][m] = grid[l][m];
}
}
System.out.println("Next Generation");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (future[i][j] == 0)
System.out.print(".");
else
System.out.print("*");
}
System.out.println();
}
}
}
Python3
# A simple Python program to implement Game of Life
# driver program
# Function to print next generation
def nextGeneration(grid, M, N):
future = [[0 for i in range(N)] for j in range(M)]
# Loop through every cell
for l in range(M):
for m in range(N):
# finding no Of Neighbours that are alive
aliveNeighbours = 0
for i in range(-1,2):
for j in range(-1,2):
if ((l+i>=0 and l+i=0 and m+j 3)):
future[l][m] = 0
# A new cell is born
elif ((grid[l][m] == 0) and (aliveNeighbours == 3)):
future[l][m] = 1
# Remains the same
else:
future[l][m] = grid[l][m]
print("Next Generation")
for i in range(M):
for j in range(N):
if (future[i][j] == 0):
print(".",end="")
else:
print("*",end="")
print()
M,N = 10,10
# Initializing the grid.
grid = [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 ],
[ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
]
# Displaying the grid
print("Original Generation")
for i in range(M):
for j in range(N):
if(grid[i][j] == 0):
print(".",end = "")
else:
print("*",end = "")
print()
print()
nextGeneration(grid, M, N)
# This code is contributed by shinjanpatra
C#
// A simple C# program to implement
// Game of Life
using System;
public class GFG {
public static void Main()
{
int M = 10, N = 10;
// Initializing the grid.
int[,] grid = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
// Displaying the grid
Console.WriteLine("Original Generation");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (grid[i,j] == 0)
Console.Write(".");
else
Console.Write("*");
}
Console.WriteLine();
}
Console.WriteLine();
nextGeneration(grid, M, N);
}
// Function to print next generation
static void nextGeneration(int [,]grid,
int M, int N)
{
int[,] future = new int[M,N];
// Loop through every cell
for (int l = 1; l < M - 1; l++)
{
for (int m = 1; m < N - 1; m++)
{
// finding no Of Neighbours
// that are alive
int aliveNeighbours = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
aliveNeighbours +=
grid[l + i,m + j];
// The cell needs to be subtracted
// from its neighbours as it was
// counted before
aliveNeighbours -= grid[l,m];
// Implementing the Rules of Life
// Cell is lonely and dies
if ((grid[l,m] == 1) &&
(aliveNeighbours < 2))
future[l,m] = 0;
// Cell dies due to over population
else if ((grid[l,m] == 1) &&
(aliveNeighbours > 3))
future[l,m] = 0;
// A new cell is born
else if ((grid[l,m] == 0) &&
(aliveNeighbours == 3))
future[l,m] = 1;
// Remains the same
else
future[l,m] = grid[l,m];
}
}
Console.WriteLine("Next Generation");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (future[i,j] == 0)
Console.Write(".");
else
Console.Write("*");
}
Console.WriteLine();
}
}
}
// This code is contributed by Sam007.
Javascript
输出:
Original Generation
..........
...**.....
....*.....
..........
..........
...**.....
..**......
.....*....
....*.....
..........
Next Generation
..........
...**.....
...**.....
..........
..........
..***.....
..**......
...**.....
..........
..........