• 生命游戏


    生命游戏,又称生命棋,是英国数学家约翰·何顿·康威1970年发明的细胞自动机

    它最初于1970年10月在《科学美国人》(杂志上马丁·葛登能的“数学游戏”专栏出现。

    生命游戏中,对于任意位置,规则只有三条:(如图,黑色为细胞,白色为无细胞)

    1. 当周围有3个细胞时,该位置产生细胞
    2. 当周围有2个细胞时,该位置维持原样
    3. 其他情况,该位置变为无细胞

    3个细胞时,该位置产生细胞,称作复活

    1个细胞时,该细胞会因为孤独而死

    4个或者过多时,会因为太过拥挤而死。

    下面我给出了实现,不过好像有点问题,有兴趣的可以一起讨论。

    一个cell类,代表细胞,一个container类,基于Panel的面板,用户绘制细胞。主界面中有个定时器,3秒钟重新对所有的细胞重新计算一次。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Drawing;
     6 
     7 namespace ThinkingModel
     8 {
     9     class Cell
    10     {
    11         private int m_x;
    12         private int m_y;
    13         private int m_width;
    14         private Color m_color;
    15 
    16         private readonly Color ALIVE_COLOR = Color.Green;
    17         private readonly Color DEAD_COLOR = Color.DarkGray;
    18 59 
    60         public void draw(Graphics gc)
    61         {
    62             Brush tbrush = new SolidBrush(Color.Black);
    63             Brush fbrush = new SolidBrush(m_color);
    64             int penWidth = m_width / 10;
    65             Pen aPen = new Pen(tbrush, penWidth);
    66             gc.DrawRectangle(aPen, m_x, m_y, m_width, m_width);
    67             gc.FillRectangle(fbrush, m_x + penWidth, m_y + penWidth, m_width - penWidth, m_width - penWidth);
    68         }
    69 
    70         public bool isDead()
    71         {
    72             return m_color == DEAD_COLOR;
    73         }
    74 
    75         public bool isAlive()
    76         {
    77             return m_color == ALIVE_COLOR;
    78         }
    79 
    80         public void setDead()
    81         {
    82             m_color = DEAD_COLOR;
    83         }
    84 
    85         public void setAlive()
    86         {
    87             m_color = ALIVE_COLOR;
    88         }
    89     }
    90 }
     

    工程文件下载:https://files.cnblogs.com/linbirg/GameLife.rar

  • 相关阅读:
    linux 学习(二)防火墙
    linux学习(一)开始
    ajax和sap以及网络安全
    仿苹果导航菜单js问题
    基本类型和引用类型调用是的区别(Object.create)
    箴言
    思维的宽度
    笔记
    循环传值_闭包
    一个问题的解法(兔子三个月之后每月都生兔子的问题)
  • 原文地址:https://www.cnblogs.com/linbirg/p/2640226.html
Copyright © 2020-2023  润新知