康威生命游戏(Conway's Game of Life)的一种实现

The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.
http://en.wikipedia.org/wiki/Conway's_Game_of_Life
The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead.
Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent.
At each step in time, the following transitions occur:

1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.

2. Any live cell with two or three live neighbours lives on to the next generation.

3. Any live cell with more than three live neighbours dies, as if by overcrowding.

4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

 

//This code was written last year, but some issues fixed recently to comply with Conway's original rule.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

const char *Icon[] =
{
    "  ",/**/
    ""
};//图素

class Cell{
private:
    int currStatus_;
    int nextStatus_;
public:
    Cell()
    {
        currStatus_ = 0;
        nextStatus_ = ((rand() % 2) && (rand() % 2));
    }
    void showCell()
    {
        currStatus_ = nextStatus_;
        printf("%s", Icon[currStatus_]);
    }
    void setAlive()
    {
        nextStatus_ = 1;
    }
    void setKilled()
    {
        nextStatus_ = 0;
    }
    int isAlive()
    {
        return currStatus_;
    }
};

class Map{
private:
    Cell *worldMatrix_[19][19];
    int dayCount_;
    int totalAlive_;
public:
    Map()
    {
        dayCount_ = 1;
        totalAlive_ = 0;
        for (int i = 0; i < 19; i++)
        {
            for (int j = 0; j < 19; j++)
                worldMatrix_[i][j] = new Cell;
        }
    }
    ~Map()
    {
        for (int i = 0; i < 19; i++)
        {
            for (int j = 0; j < 19; j++)
                delete worldMatrix_[i][j];
        }
    }
    void displayMap()
    {
        system("cls");
        totalAlive_ = 0;
        for (int i = 0; i < 19; i++)
        {
            for (int j = 0; j < 19; j++)
            {
                worldMatrix_[i][j]->showCell();
                totalAlive_ += worldMatrix_[i][j]->isAlive();
            }
            printf("\r\n");
        }
        printf("第%d天\t地图中有%d个细胞\r\n", dayCount_, totalAlive_);
    }
    void startIterating()
    {
        int count = 0;
        for (int i = 0; i < 19; i++)
        {
            for (int j = 0; j < 19; j++)
            {
                count = 0;
                if ((i - 1 >= 0) && (worldMatrix_[i - 1][j]->isAlive()))//i-1 , j
                    count++;
                if ((i + 1 < 19) && (worldMatrix_[i + 1][j]->isAlive()))//i+1 , j
                    count++;
                if ((j - 1 >= 0) && (worldMatrix_[i][j - 1]->isAlive()))//i , j-1
                    count++;
                if ((j + 1 < 19) && (worldMatrix_[i][j + 1]->isAlive()))//i , j+1
                    count++;
                if ((i - 1 >= 0) && (j - 1 >= 0) && (worldMatrix_[i - 1][j - 1]->isAlive()))//i-1 , j-1
                    count++;
                if ((i - 1 >= 0) && (j + 1 < 19) && (worldMatrix_[i - 1][j + 1]->isAlive()))//i-1 , j+1
                    count++;
                if ((i + 1 < 19) && (j - 1 >= 0) && (worldMatrix_[i + 1][j - 1]->isAlive()))//i+1 , j-1
                    count++;
                if ((i + 1 < 19) && (j + 1 < 19) && (worldMatrix_[i + 1][j + 1]->isAlive()))//i+1 , j+1
                    count++;
                if (count == 3)
                {
                    worldMatrix_[i][j]->setAlive();
                }
                else if (count == 2)
                {
                    if (worldMatrix_[i][j]->isAlive())
                    {
                        worldMatrix_[i][j]->setAlive();
                    }
                    else
                    {
                        worldMatrix_[i][j]->setKilled();
                    }
                }
                else
                {
                    worldMatrix_[i][j]->setKilled();
                }
            }
        }
        dayCount_++;
    }
};

int main(void)
{
    SetWindowText(GetForegroundWindow(), "Life Game");
    srand([]()->unsigned int
    {
        _asm
        {
            rdtsc;
        }
    }());
    Map map;
    while (true)
    {
        Sleep(1000);
        map.displayMap();
        map.startIterating();
    }
    return 0;
}

转载于:https://www.cnblogs.com/intervention/p/4054094.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值