Cellularautomation (cellular automata)

Source: Internet
Author: User

Cellularautomation (cellular automata)

Cellular automata (English: Cellular automaton), also known as lattice automata, cellular automata, is a discrete model, in the theory of calculation, mathematics and theoretical biology are related to the study. It consists of an infinite number of regular, hard squares, each of which is in a finite state. The entire grid can be any finite dimension. At the same time it is discrete. The state of each lattice at T is determined by the state of a set of finite lattices at the time of T-1 (which is called the neighborhood of that lattice). Each of the "neighbors" of the grid has been fixed. (one can be a neighbor.) Each time it evolves, each of the cells follows the same rules.

In terms of form, the cellular automata has three characteristics:

Parallel Computing (parallel computation): Each individual cell changes synchronously simultaneously
localized (local): Changes in the state of the cells are only affected by the surrounding cells.
consistent (homogeneous): All cells are governed by the same rules


Life Games

In the game of life, for any cell, the rules are as follows:
Each cell has two states-survival or death-and each cell interacts with eight cells that are centered around itself. (, black for survival, White for death)

When the current cell is alive, the cell becomes dead when there are less than 2 (2) surviving cells around it. (Simulation of the number of life is scarce)
When the current cell is alive, the cell remains intact when there are 2 or 3 surviving cells around it.
When the current cell is alive, the cell becomes dead when there are more than 3 surviving cells in the surrounding state. (excessive number of simulated life)
When the current cell is in the dead state, the cell becomes viable when there are 3 surviving cells around it. (Simulated reproduction)
The initial cell structure can be defined as a seed, and when all the cells in the seed are processed by the above rules, the first-generation cell map can be obtained. Continue to process the current cell map according to the rules, you can get the next generation of cell map, the cycle.

In this game, you can also set some more complex rules, such as the status of the current grid not only by the parents, but also to consider the situation of the grandfather generation. Players can also be the world's "God", arbitrarily set a cell of the dead and alive, to observe the impact on the world.

In the game, the chaotic cells will gradually evolve a variety of refined, tangible structures, these structures tend to be very good symmetry, and each generation is changing shape. Some shapes are locked and do not change over generations. Sometimes, some formed structures are destroyed by the "intrusion" of some disordered cells. But shape and order can often be generated from clutter.


There is a link that can play this game directly-game of life

Write it yourself below with unity.

Using unityengine;using system.collections;public enum state{died,living};p ublic class Cell{public state currentState;}  public class Earth:monobehaviour {public int width;public int height;public string seed;public bool Userandomseed;public float UpdateInterval = 1.0f;float refreshtime = -1f; [Range (0,]public) int randomfillpercent; cell[,] map; cell[,] maptmp;//use this for initializationvoid Start () {map = new cell[width,height];maptmp = new cell[width, height]; for (int i = 0; i < width; i++) for (int j = 0; j < height; J + +) {Map[i,j] = new Cell (); map[i, j].currentstate = State . Died;maptmp[i, j] = new Cell (); maptmp[i, j].currentstate = state.died;} Initearth ();} Update is called once per framevoid Update () {if (Time.time-refreshtime > UpdateInterval) {Updateearth (); Refreshtim e = Time.time;}} void Updateearth () {for (int x = 0; x < width; + +) {for (int y = 0; y < height; y++) {maptmp[x, y].currentstate = map[ X, Y].currentstate;int neighbourlivecells = GetsurroundinglivEcells (x, y); if (map[x, y].currentstate = = state.died && Neighbourlivecells = = 3) {maptmp[x, y].currentstate = Stat e.living;} if (map[x, y].currentstate = = state.living) {if (Neighbourlivecells < 2) {maptmp[x, y].currentstate = state.died;} else if (Neighbourlivecells > 3) {maptmp[x, y].currentstate = state.died;} Else{maptmp[x, y].currentstate = State.living;}}} for (int x = 0; x < width; + +) for (int y = 0; y < height; y++) {map[x, y].currentstate = Maptmp[x, y].currentstate;}  }int getsurroundinglivecells (int gridX, int gridY) {int count = 0;for (int neighbourx = gridX-1; Neighbourx <= GridX + 1;  neighbourx++) {for (int neighboury = gridY-1; Neighboury <= GridY + 1; neighboury++) {if (Neighbourx >= 0 && Neighbourx < width && Neighboury >= 0 && Neighboury < height) {if (Neighbourx! = GridX | | neighbo UrY! = GridY) {count + = Map[neighbourx, neighboury].currentstate = = state.living? 1:0;}}} return count;} void Initearth () {if (userandomseed) {seed = Time.time.ToString ();} System.Random pseudorandom = new System.Random (seed.  GetHashCode ()); for (int x = 0; x < width; + +) {for (int y = 0; y < height; y++) {if (x = = 0 | | x = = Width-1 | | y = = 0 | | y = = height-1) {map[x, y].currentstate = state.living;} Else{map[x, y].currentstate = (pseudorandom.next (0) < randomfillpercent)? State.Living:State.Died;}}}} void Ondrawgizmos () {if (map! = null) {for (int x = 0; x < width; + x + +) {for (int y = 0; y < height; y++) {if (map[x, y] ! = null) {Gizmos.color = (map[x, y].currentstate = = state.living)? Color.black:Color.white; Vector3 pos = new Vector3 (-WIDTH/2 + x +. 5f, 0,-height/2 + y +. 5f); Gizmos.drawcube (POS, Vector3.one);}}}}}


The code is actually implemented as described in the previous algorithm.

The initial cell distribution is generated using Unity's own random, then plotted in the Ondrawgizmos function. This is how it works:



You can set the initial state to classic glider, and modify the Initearth () function.

glidermap[20, 20].currentstate = state.living;map[20, 21].currentstate = state.living;map[20, 22].currentState = state.living;map[21, 22].currentstate = state.living;map[22, 21].currentstate = state.living;




Program generates terrain

Program generation has many methods, using cellular automata mainly used to generate dungeons and other similar terrain, commonly used in the Roguelike class of games. The main algorithm is also the use of cell survival rules, such as now provided that as long as there are more than four cells around the cell, the cell will survive, or die.

for (int x = 0; x < width; + +) {for (int y = 0; y < height; y++) {int neighbourwalltiles = Getsurroundingwallcount (x) y); if (Neighbourwalltiles > 4) map[x, y] = 1;else if (Neighbourwalltiles < 4) map[x, y] = 0;}}


By constantly iterating, you can get a map. Of course, the first thing is to randomly distribute some cells on the canvas. Iterate 5 times and eventually generate the map as follows




Code Listing

Using unityengine;using System.collections;public class Mapgenerator:monobehaviour {public int width;public int height; public string seed;public bool Userandomseed; [Range (0, +)]public int randomfillpercent;int[,] map;//use this for initializationvoid Start () {Generatemap ();} Update is called once per framevoid Update () {if (Input.getmousebuttondown (0)) {generatemap ();}} void Generatemap () {map = new int[width, height]; Randomfillmap (); for (int i = 0; i < 5; i++) {Smoothmap ();}} void Randomfillmap () {if (userandomseed) {seed = Time.time.ToString ();} System.Random pseudorandom = new System.Random (seed.  GetHashCode ()); for (int x = 0; x < width; + +) {for (int y = 0; y < height; y++) {if (x = = 0 | | x = = Width-1 | | y = = 0 | | y = = height-1) {map[x, y] = 1;} Else{map[x, Y] = (pseudorandom.next (0) < randomfillpercent)? 1:0;}}} void Smoothmap () {for (int x = 0; x < width; + +) {for (int y = 0; y < height; y++) {int neighbourwalltiles = Getsurrou Ndingwallcount (x, y); if (neiGhbourwalltiles > 4) map[x, y] = 1;else if (Neighbourwalltiles < 4) map[x, y] = 0;}}} int getsurroundingwallcount (int gridX, int gridY) {int wallcount = 0;for (int neighbourx = gridX-1; Neighbourx <= grid X + 1;  neighbourx++) {for (int neighboury = gridY-1; Neighboury <= GridY + 1; neighboury++) {if (Neighbourx >= 0 && Neighbourx < width && Neighboury >= 0 && Neighboury < height) {if (Neighbourx! = GridX | | neighbo UrY = GridY) {Wallcount + = Map[neighbourx, Neighboury];}} else{wallcount++;}}} return wallcount;}  void Ondrawgizmos () {if (map! = null) {for (int x = 0; x < width; + x + +) {for (int y = 0; y < height; y++) {Gizmos.color = (map[x, y] = = 1)? Color.black:Color.white; Vector3 pos = new Vector3 (-WIDTH/2 + x +. 5f, 0,-height/2 + y +. 5f); Gizmos.drawcube (POS, Vector3.one);}}}}



Reference

Cellular automata wiki-https://zh.wikipedia.org/wiki/%e7%b4%b0%e8%83%9e%e8%87%aa%e5%8b%95%e6%a9%9f

Game of life-http://www.bitstorm.org/gameoflife/

Procedural Cave Generation Project icon-https://unity3d.com/learn/tutorials/projects/procedural-cave-generation-tutorial

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cellularautomation (cellular automata)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.