如何通过数学知识简化复杂的条件语句

时间:2013-08-29 01:40:41

标签: logic conditional business-logic

我有一个复杂的业务逻辑,我相信布尔代数,有些东西应该有能力简化逻辑。谁能通过解决以下问题告诉我如何做到这一点?

问题来自游戏X3:Reunion。 Here is the universe map,如果有帮助。

在宇宙中,有通过扭曲门连接在一起的扇区。从一个扇区到另一个扇区,你可以飞到那里或跳到那里(速度更快)。如果你跳到另一个扇区,你应该确保在经线门附近没有太多人群(否则你会撞到其他人)。你必须有跳跃装置和足够的能量才能跳跃。

有一些孤立的部门只能通过跳跃来达到。

问题是,鉴于船舶及其当前的部门和目的地部门,确定船舶应该如何到达该部门(通过跳跃或飞行)。另外,如果两个扇区相邻,请不要费心去跳。

我们设置:

a: two sectors are adjacent
b: have jump device and enough energy
c: destination sector is not isolated
d: not too crowd near the gate of the destination

if(!a && b && d) 
  jump now
end

if(!a && b && !d)
  can jump but wait until there is not too crowd near the gate of the destination. 
  Should recheck before the jump, because the ship may be losing the energy
  that makes it not enough to jump.
end

if(!b && c)
  fly to destination
end

您可以将上述if-end语句转换为if-else if -... else-end语句吗?而且我想知道最终的“其他”是什么意思。我不仅需要结果,还需要接近结果的程序。

非常感谢!

1 个答案:

答案 0 :(得分:1)

你的前两个条件都依赖于!a&& b,因此它们之间的差异由d的值决定。最后一个条件只取决于!b&& ç

我会这样做:

if (!b && c) { fly }
else if (!a && b) {
    if (d) { jump now } else { wait }
}

或者你可以先把它放在'b'上:

if (b) {
    if (!a) {
        if (d) { jump now } else { wait }
    }
}
else {
    if (c) { fly }
}