• 康威生命游戏(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("
    ");
            }
            printf("第%d天	地图中有%d个细胞
    ", 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;
    }
  • 相关阅读:
    ansible部署apache
    yum换源,rpm包下载,源码包安装
    zabbix 监控apache
    分块大法 -- 优雅的暴力
    [每日一题]:建立联系 -- 最小生成树
    [每日一题]:P1016 旅行家的预算 -- 反悔贪心
    [每日一题]:[NOIP2010]关押罪犯 -- 并查集
    Python基础: 元组的基本使用
    Python基础: 列表的基本使用
    Python基础:分支、循环、函数
  • 原文地址:https://www.cnblogs.com/intervention/p/4054094.html
Copyright © 2020-2023  润新知