首页 > 其他分享 >生命模拟

生命模拟

时间:2024-10-21 17:33:30浏览次数:1  
标签:Body 生命 items Statu Add new bodys 模拟

界面:

<DockPanel Background="#EEEEEE">
    <WrapPanel DockPanel.Dock="Top">
        <Border Background="Green" Width="20" Height="20" VerticalAlignment="Center"/>
        <TextBlock Text="新生" VerticalAlignment="Center"/>
        <Border Background="DarkGreen" Width="20" Height="20" VerticalAlignment="Center"/>
        <TextBlock Text="不变" VerticalAlignment="Center"/>
        <Border Background="Gray" Width="20"  Height="20" VerticalAlignment="Center"/>
        <TextBlock Text="低密度死亡" VerticalAlignment="Center"/>
        <Border Background="DarkGray" Width="20" Height="20" VerticalAlignment="Center"/>
        <TextBlock Text="高密度死亡" VerticalAlignment="Center"/>
        <Border Width="50"/>
        <TextBlock Text="行数:"/>
        <TextBox x:Name="txtRow" Text="90" Width="50"/>
        <TextBlock Text="列数:"/>
        <TextBox x:Name="txtCell" Text="150" Width="50"/>
        <Button Content="初始化" Click="InitLifWorld"/>
        <TextBlock Text="周期(ms):"/>
        <TextBox x:Name="txtEllips" Text="300" Width="50"/>
        <Button x:Name="btnLifStart" Content="开始" Click="BtnStartLifeWorld"/>
        <Button x:Name="btnLifStop" Content="停止" Click="BtnStopLifeWorld" IsEnabled="False"/>
        <TextBlock x:Name="txtStatu" Text="已停止"/>
    </WrapPanel>
    <Grid>
        <UniformGrid x:Name="LifeWorld" HorizontalAlignment="Center" VerticalAlignment="Center">

        </UniformGrid>
    </Grid>
</DockPanel> 

 

代码:

private void BtnStartLifeWorld(object sender, RoutedEventArgs e)
{
    try
    {
        ellipes = Convert.ToInt32(this.txtEllips.Text);
        StartLifeWorld();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

private void InitLifWorld(object sender, RoutedEventArgs e)
{
    try
    {
        maxRow = Convert.ToInt32(this.txtRow.Text);
        maxCell = Convert.ToInt32(this.txtCell.Text);
        this.LifeWorld.Rows = maxRow;
        this.LifeWorld.Columns = maxCell;
        this.LifeWorld.Children.Clear();
        bodys = new Body[maxRow, maxCell];

        for (int r = 0; r < maxRow; r++)
        {
            for (int c = 0; c < maxCell; c++)
            {
                bodys[r, c] = new Body(r, c, LifeStatu.Old);
                bodys[r, c].Background = Brushes.Black;
                this.LifeWorld.Children.Add(bodys[r, c]);
            }
        }
    }catch(Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

private void BtnStopLifeWorld(object sender, RoutedEventArgs e)
{
    Stop();
}

int ellipes = 300;
int maxRow = 100;
int maxCell = 150;
Body[,] bodys = null;
List<Body> editItems = null;
bool isStop = false;
public void StartLifeWorld()
{
    this.txtStatu.Text = "正进行";
    this.btnLifStart.IsEnabled = false;
    this.btnLifStop.IsEnabled = true;
    Task.Run(() =>
             {
                 try
                 {
                     isStop = false;
                     while (!isStop)
                     {
                         Thread.Sleep(ellipes);
                         Dispatcher.Invoke(() => ElliseTime());
                     }
                 }
                 catch(Exception ex)
                 {
                     MessageBox.Show(ex.ToString());
                 }
             });
}

public void ElliseTime()
{
    editItems = new List<Body>();
    for (int r = 0; r < maxRow; r++)
    {
        for (int c = 0; c < maxCell; c++)
        {
            var roundItems = GetRoundItems(bodys[r,c]);
            int whiteCount = roundItems.Count(f => f.Statu == LifeStatu.Born || f.Statu == LifeStatu.Live);
            var statu = GetStatu(whiteCount, bodys[r,c].Statu);
            var b = bodys[r, c];
            if(statu != b.Statu)
                editItems.Add(new Body(b.Row, b.Cell, statu));
        }
    }
    if(editItems.Count <= 0)
    {
        Stop();
        return;
    }
    foreach (var item in editItems)
    {
        bodys[item.Row, item.Cell].ChangeStatu(item.Statu);
    }
}

void Stop()
{
    isStop = true;
    this.txtStatu.Text = "已停止";
    this.btnLifStart.IsEnabled = true;
    this.btnLifStop.IsEnabled = false;
}

/// <summary>
/// 获取当前个体周围所有相邻个体
/// </summary>
/// <param name="body"></param>
/// <returns></returns>
public List<Body> GetRoundItems(Body body)
{
    var items = new List<Body>();
    int row = body.Row;
    int cell = body.Cell;
    //从左到右,从上到下
    ////靠上边
    if (row == 0)
    {
        //靠左上角
        if (cell == 0)
        {
            items.Add(new Body(1, 0, bodys[1,0].Statu));
            items.Add(new Body(1, 1, bodys[1,1].Statu));
            items.Add(new Body(0, 1, bodys[0,1].Statu));
        }
        ////靠上边
        else if (cell > 0 && cell < maxCell - 1)
        {
            int r = row;
            int c = cell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row + 1;
            c = cell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row + 1;
            c = cell;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row + 1;
            c = cell + 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row;
            c = cell + 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
        }
        //右上角
        else if (cell == maxCell - 1)
        {
            int r = 0;
            int c = maxCell - 2;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = 1;
            c = maxCell - 2;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = 1;
            c = maxCell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
        }
    }
    //靠下方
    else if (row == maxRow - 1)
    {
        //靠右下角
        if (cell == maxCell - 1)
        {
            int r = maxRow - 2;
            int c = maxCell - 2;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = maxRow - 1;
            c = maxCell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = maxRow - 1;
            c = maxCell - 2;
            items.Add(new Body(r, c, bodys[r,c].Statu));
        }
        ////左下角
        else if (cell == 0)
        {
            int r = row - 1;
            int c = 0;
            items.Add(new Body(r, c, bodys[r, c].Statu));
            r = row - 1;
            c = 1;
            items.Add(new Body(r, c, bodys[r, c].Statu));
            r = row;
            c = 1;
            items.Add(new Body(r, c, bodys[r, c].Statu));
        }
        //下方
        else if (cell < maxCell - 1)
        {
            int r = row - 1;
            int c = cell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row - 1;
            c = cell;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row - 1;
            c = cell + 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row;
            c = cell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row;
            c = cell + 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
        }
    }
    else
    {
        ////靠左边
        if (cell == 0)
        {
            int r = row - 1;
            int c = 0;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row - 1;
            c = 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row;
            c = 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row + 1;
            c = 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row + 1;
            c = 0;
            items.Add(new Body(r, c, bodys[r,c].Statu));
        }
        ////靠右边
        else if (cell == maxCell - 1)
        {
            int r = row - 1;
            int c = cell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row - 1;
            c = cell;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row;
            c = cell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row + 1;
            c = cell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row + 1;
            c = cell;
            items.Add(new Body(r, c, bodys[r,c].Statu));
        }
        else
        {
            //上
            int r = row - 1;
            int c = cell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row - 1;
            c = cell;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row - 1;
            c = cell + 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));

            //中
            r = row;
            c = cell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row;
            c = cell + 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));

            //下
            r = row + 1;
            c = cell - 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row + 1;
            c = cell;
            items.Add(new Body(r, c, bodys[r,c].Statu));
            r = row + 1;
            c = cell + 1;
            items.Add(new Body(r, c, bodys[r,c].Statu));
        }
    }
    return items;
}


public LifeStatu GetStatu(int whiteCount, LifeStatu statu)
{
    int a = 3;
    if (whiteCount == a)
    {
        if (statu == LifeStatu.Old || statu == LifeStatu.Young)
            return LifeStatu.Born;
        else
            return LifeStatu.Live;
    }
    else if (whiteCount == a - 1)
    {
        if (statu == LifeStatu.Live || statu == LifeStatu.Born)
            return LifeStatu.Live;
    }
    else if (whiteCount < a - 1)
    {
        if (statu == LifeStatu.Live || statu == LifeStatu.Born)
            return LifeStatu.Young;
    }
    else if (whiteCount > a)
    {
        if (statu == LifeStatu.Live || statu == LifeStatu.Born)
            return LifeStatu.Old;
    }
    return statu;
    //黑格当周围有2个,则变白
    //当周围超过3个则变黑
    //当周围少于2个或有3个则不变

    //if (whiteCount > 3)
    //    return LifeStatu.Old;
    //if (whiteCount == 3)
    //{
    //    if (statu == LifeStatu.Old || statu == LifeStatu.Young)
    //        return LifeStatu.Born;
    //}

    //switch (statu)
    //{
    //    case LifeStatu.Born: return LifeStatu.Live;
    //    case LifeStatu.Live: 
    //    case LifeStatu.Young:
    //    case LifeStatu.Old:
    //    default:
    //        return LifeStatu.Old;
    //}

}

}
//
public enum LifeStatu
    {
        /// <summary>
        /// 诞生
        /// </summary>
        Born,
        /// <summary>
        /// 活着
        /// </summary>
        Live,
        /// <summary>
        /// 低密度而亡
        /// </summary>
        Young,
        /// <summary>
        /// 高密度而亡
        /// </summary>
        Old
    }
    public class Body : Button
    {
        public LifeStatu Statu { get; set; }
        public int Row { get; set; }
        public int Cell { get; set; }

        public Body(int row, int cell, LifeStatu statu)
        {
            this.Width = 10;
            this.Height = 10;
            this.Style = null;
            this.BorderThickness = new Thickness(0);
            this.Click += (s, e) => ChangeStatu((int)Statu >= 0 ? (Statu == LifeStatu.Old ? LifeStatu.Born :(LifeStatu)((int)Statu + 1)) : LifeStatu.Born);

            this.Row = row;
            this.Cell = cell;
            ChangeStatu(statu);
        }

        public void ChangeStatu(LifeStatu statu)
        {
            Dispatcher.Invoke(() =>
            {
                this.Statu = statu;
                switch (statu)
                {
                    case LifeStatu.Born:
                        this.Background = Brushes.White;
                        break;
                    case LifeStatu.Live:
                        this.Background = Brushes.White;
                        break;
                    case LifeStatu.Young:
                        this.Background = Brushes.Black;
                        break;
                    case LifeStatu.Old:
                        this.Background = Brushes.Black;
                        break;
                    default:
                        break;
                }
            });
        }
    }

 

标签:Body,生命,items,Statu,Add,new,bodys,模拟
From: https://www.cnblogs.com/RedSky/p/14363451.html

相关文章

  • 代理与模拟登录
    01代理反爬:封IP1.什么是封IP我们用程序访问人家网站,请求次数一下很多不像人在访问,有些网站就会封掉你的IP封了以后,当前的IP就不能访问这个网站,爬不了这个数据2.所有网站都会封IP吗?不会,是否封IP是看网站的设置策略来的,他不会把这个告诉我们,也不是所有的网站会封IP,具体......
  • 探索ArkWeb的奥秘:架构了解与生命周期管理
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。ArkWeb(方舟Web)是华为鸿蒙Harmon......
  • Vue学习之路10----生命周期
    (以下图片来自官网)<template><div>{{num}}</div><button@click="num++">add</button></template><scriptsetupname="App">import{ref,onBeforeMount,onMounted,onBeforeUpdate,onUpdated,onBefore......
  • CSP 模拟 52
    B最短路先建出最短路DAG,保证最短路径唯一,所以建出来的DAG是一棵树。考虑一个点的答案可以由谁来更新,假设当前点为点\(u\),它的dfs序控制范围是\(l,r\)。首先,它可以由子树外除了父亲的节点来更新,形如ans[u]=dis[v]+w,它也可以由子树内的节点更新,路径形如1->zc->v->u,要求中......
  • Qt编写的modbus模拟器/支持网络和串口以及websocket/支持网络rtu
    一、使用说明1.1设备模拟-Com第一步,填写要模拟的设备地址,0表示自动处理,也就是收到什么地址就应答什么地址。第二步,填写对应的串口号和波特率。第三步,单击打开串口,成功后会变成关闭串口字样。单击清空数据会将左侧打印栏的信息清空。右侧一堆微调框用于模拟对应设备多个寄......
  • CSP2024 前集训:多校A层冲刺NOIP2024模拟赛08
    前言先痛骂没良心出题人,T1\(n\sqrtn\)多大你刚好给多大,一点不多给,T2才是签到题,因为放了T2位置打了暴力就去想T3了,我是唐氏,谁让你T1、T2swap的?T3实则三道题。但是还是感觉T1更简单啊,\(5e4\)搁哪儿摆着呢一眼\(O(n\sqrtn)\),甚至空间也是这么多,太明显了。挂分挂......
  • 模拟六补题报告
    一、题目报告比赛中第一题AC,第二题0分(时间超限),第三题AC,第四题0分,比赛后全部AC。二、赛中概况首先做得第一题,第一题特别简单,用了3分钟左右;然后是第二题,三、题目正解T1 挑选苹果(apple)时间限制:1秒        内存限制:128M题目描述小可手里有n个苹果,大小为a1,......
  • vue(vue.js)—生命周期(1)
    原文链接:vue(vue.js)—生命周期(1)–每天进步一点点vue也有自己的生命周期。数据初始化的生命周期如下:beforeCreate、created、beforeMount、mounted1.beforeCreate此时,无法通过vm访问data中的数据,methods中的方法。2.created此时,可以通过vm访问到data中的数据,methods中配......
  • 多校A层冲刺NOIP2024模拟赛09
    多校A层冲刺NOIP2024模拟赛09考试唐完了,T2、T4都挂了100分,人麻了。排列最小生成树给定一个\(1,2,\dots,n\)的排列\(p_1,p_2,\dots,p_n\)。构造一个\(n\)个点的完全无向图,节点编号分别是\(1,2,\dots,n\)。节点i和节点j之间的边边权为\(|pi−pj|×|i......
  • 生命游戏串行代码实现(Java)
    目录生命游戏介绍一、效果展示1.初始界面2.启动游戏二、代码实现三、代码解释1.常量设置2.图形化3.计算“生死”情况与统计邻居细胞数量结语生命游戏介绍        生命游戏,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机。        一......