动态if语句? [英] Dynamic if-statement?

查看:222
本文介绍了动态if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作生活克隆的游戏(无法链接维基,因为它是向下)。基本功能已完成,但我想给用户选择同时定义自己的规则。生活规则的标准的游戏是:

I'm working on a Game of Life clone (can't link wiki, as it's down). The basic functionality is done, but I want to give the user the option to also define his own rules. The standard game of life rules are:

细胞与2或3的邻居保持住

Cell with 2 or 3 neighbors keeps living.

与0-1和4-8的邻居细胞死亡。

Cell with 0-1 and 4-8 neighbors dies.

死细胞变活的。

这是容易与2 if语句来做。但是,用户也可以定义是这样的:

This is easy to do with 2 if-statements. But a user could also define something like:

单元1-3或5-7的邻居保持住。

Cell with 1-3 or 5-7 neighbors keeps living.

与其他一些细胞死亡。有2个或4个邻居

Cell with another number dies.

死细胞变活的。

如何能我实现这if语句?我可以使用某种形式的列表来核对

How can I implement this in if-statements? Can I use some sort of list to check against?

编辑:我在这里看到了一些伟大的解决方案。必须确定什么最适合我的代码。对于任何人依然回答:我能妥善处理用户输入,不用担心;)

I'm seeing some great solutions here. Will have to determine what fits my code best. For anyone still answering: I can properly handle user input, do not worry about that ;)

我可能有一个窗口,在这里我想问如何不少邻居活路?多少邻居成为半死不活的时候?,具有一定的文本框。这些也将检查是否输入是有效的。

I will probably have a window where I ask How many neighbors to stay alive?, How many neighbours to become alive when dead?, with some textboxes. These will also check if the input is valid.

(这是Unity3D C#)

(This is C# in Unity3D)

推荐答案

我只检查这些条件,使细胞死亡。我会组织我的功能沿线:

I would only check those conditions that would make a cell dead. I would structure my function along the line:

bool CalculateIfDead(Cell c, int[] deadlyCountRules)
{       
   foreach(int n in deadlyCountRules)
   {
      if(c.NeighbourCount == n)
      {
         return true;
      }
   }
   return false;
}

这篇关于动态if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆