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;
    }
  • 相关阅读:
    设计模式责任链模式(COR)
    设计模式享元模式(Flyweight)
    设计模式外观模式(Facade)
    设计模式桥接模式(Bridge)
    设计模式适配器(Adapter)
    设计模式解释器模式(Interpreter)
    Ext出来个3.0.1版本,不过不能免费下载了,郁闷
    150天成为JAVA高级程序员?
    Drools业务逻辑框架
    HQL中In的问题详解
  • 原文地址:https://www.cnblogs.com/intervention/p/4054094.html
Copyright © 2011-2022 走看看