zoukankan      html  css  js  c++  java
  • 康威生命游戏(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;
    }
  • 相关阅读:
    微信——获取用户基本信息及openid 、access_token、code
    Java中的标记接口(zz)
    深入理解Java的注解(Annotation):注解处理器(3)
    深入理解Java的注解(Annotation):自定义注解入门(2)
    深入理解Java的注解(Annotation):基本概念(1)
    TCP 粘包及其解决方案(zz)
    TCP,UDP,IP包头格式及说明(zz)
    python 如何将JSON数据原封不动的转为字符串(顺序不能变动)?
    mysql:functional dependency
    什么是“几何级数”?什么是“算数级数”?有啥区别?
  • 原文地址:https://www.cnblogs.com/intervention/p/4054094.html
Copyright © 2011-2022 走看看