CodeCombat 逻辑组合与优化

布尔运算符 NOT / 短路求值 / 混合运算(42-46关)

第 42 关

平常的一天 (A Normal Day)

战术思路分析:短路求值

以前,我们总要写两层 `if` 语句来检查目标:`if enemy: if enemy.type == "munchkin":`。这很繁琐!

编程优化:AND 的短路特性

在 `enemy and enemy.type == "munchkin"` 中:
1. 计算机先检查 `enemy` 是否存在(是否为 `None`)。
2. **如果 `enemy` 是 `None`(即不存在),** 整个表达式立刻被判定为 `False`,**右侧的 `enemy.type` 代码不会被执行**,从而避免了程序崩溃!

IF Target AND Target.Prop: Run Code

Python 程序解法

1
while True:
2
enemy = hero.findNearestEnemy()
3
# 使用与(AND)以后,我们只会在敌人存在时检查类型。
4
if enemy and enemy.type == "munchkin":
**短路求值:** 检查存在 AND 检查类型
5
hero.attack(enemy)
攻击矮人
6
# 寻找最近的物品
7
item = hero.findNearestItem()
8
# 如果存在且类型为"coin",则收集该物品。
9
if item and item.type == "coin":
短路求值:检查存在 AND 检查类型
10
hero.moveXY(item.pos.x, item.pos.y)
收集硬币
第 43 关

逻辑之路 (The Logic Path)

战术思路分析:三元组逻辑

巫师的路口是布尔逻辑控制的。你需要根据逻辑表达式的结果(`True` 走上,`False` 走下)来选择道路。

三大家族运算符

  • AND: **都**为真才为真。 (`secretA and secretB`)
  • OR: **有一个**为真即为真。 (`secretA or secretB`)
  • NOT: **取反**。 (`not secretB`)

A and B
A or B
not B

Python 程序解法

1
hero.moveXY(14, 24)
移动到巫师处
2
secretA = hero.findNearestFriend().getSecretA()
获取 Secret A
3
secretB = hero.findNearestFriend().getSecretB()
获取 Secret B
4
# 第一路口:AND
5
secretC = secretA and secretB
计算 A **AND** B
6
if secretC:
如果 C 为 True,走上面 (33)
7
hero.moveXY(20, 33)
8
else:
否则,走下面 (15)
9
hero.moveXY(20, 15)
10
hero.moveXY(26, 24)
11
# 第二路口:OR
12
secretD = secretA or secretB
计算 A **OR** B
13
if secretD:
如果 D 为 True,走上面 (33)
14
hero.moveXY(32, 33)
15
else:
16
hero.moveXY(32, 15)
17
hero.moveXY(38, 24)
18
# 第三路口:NOT
19
secretE = not secretB
计算 **NOT** B
20
if secretE:
如果 E 为 True,走上面 (33)
21
hero.moveXY(44, 33)
22
else:
23
hero.moveXY(44, 15)
24
hero.moveXY(50, 24)
第 44 关

逻辑之圈 (The Logic Circle)

战术思路分析:连续判断

这次布尔值有三个 (`A`, `B`, `C`),你需要判断更长的逻辑表达式。

链式 AND 和 OR

- **`AND` 链:** `A and B and C` (所有都必须为真)
- **`OR` 链:** `A or B or C` (任何一个为真即可)
**注意:** Python 可以直接处理多于两个变量的 `and` 和 `or` 运算。

(A and B and C)
(A or B or C)

Python 程序解法

1
hero.moveXY(20, 24)
移动到巫师旁
2
secretA = hero.findNearestFriend().getSecretA()
获取 A, B, C 三个值
3
secretB = hero.findNearestFriend().getSecretB()
4
secretC = hero.findNearestFriend().getSecretC()
5
# 第一个路口:AND 链
6
secretD = secretA and secretB and secretC
计算 A **AND** B **AND** C
7
if secretD:
如果 D 为 True,走上面 (33)
8
hero.moveXY(30, 33)
9
else:
否则走下面 (15)
10
hero.moveXY(30, 15)
11
# 第二个路口:OR 链
12
secretE = secretA or secretB or secretC
计算 A **OR** B **OR** C
13
if secretE:
如果 E 为 True,走左边 (20)
14
hero.moveXY(20, 24)
15
else:
否则走右边 (40)
16
hero.moveXY(40, 24)
17
# 第三个路口:AND/OR 混合,复用之前的结果
18
secretF = secretA and secretB and secretC and secretD and secretE
计算 A AND B AND C AND D AND E
19
if secretF:
如果 F 为 True,走上面 (33)
20
hero.moveXY(30, 33)
21
else:
否则走下面 (15)
22
hero.moveXY(30, 15)
第 45 关

逻辑结论 (The Logical Conclusion)

战术思路分析:优先级

这是最复杂的布尔挑战,涉及到所有三个运算符,你需要使用**括号**来控制运算顺序!

优先级顺序 (高到低)

1. **圆括号** `()`
2. **NOT** (`not`)
3. **AND** (`and`)
4. **OR** (`or`)

(A and B) or C

Python 程序解法

1
hero.moveXY(24, 16)
移动到 Eszter
2
secretA = hero.findNearestFriend().getSecretA()
获取 A, B, C 三个值
3
secretB = hero.findNearestFriend().getSecretB()
4
secretC = hero.findNearestFriend().getSecretC()
5
# Tamas: (A AND B) OR C
6
tam = (secretA and secretB) or secretC
确保 AND 先执行
7
hero.moveXY(19, 26)
8
hero.say(tam)
9
# Zsofi: (A OR B) AND C
10
zso = (secretA or secretB) and secretC
确保 OR 先执行
11
hero.moveXY(26, 36)
12
hero.say(zso)
13
# Istvan: (A OR C) AND (B OR C)
14
ist = (secretA or secretC) and (secretB or secretC)
确保每个括号内的 OR 先执行
15
hero.moveXY(37, 34)
16
hero.say(ist)
17
# Csilla: (A AND B) OR (B AND NOT C)
18
csi = (secretA and secretB) or (secretB and (not secretC))
括号保证了 NOT > AND > OR 的顺序
19
hero.moveXY(40, 22)
20
hero.say(csi)
第 46 关

组合挑战. 非与或 (NOT AND OR)

战术思路分析:综合决策

这是对所有布尔运算符的总测试!

  • **物品条件(OR):** 收集 `potion` **或** `lightstone`。
  • **敌人条件(NOT AND):** 攻击 **非** `burl` **且** 距离 `< 15` 的敌人。

NOT AND 结构

我们使用 `!= "burl"` (不等于) 来实现 `NOT` 的功能,然后用 `AND` 来连接距离条件。

Attack: (Type != Burl) and (Close)
Collect: (Potion or Lightstone)

Python 程序解法

1
while True:
2
item = hero.findNearestItem()
3
if item:
4
if item.type == "potion" or item.type == "lightstone":
收集:类型是药水 **OR** 光石
5
hero.moveXY(item.pos.x, item.pos.y)
移动
6
enemy = hero.findNearestEnemy()
7
if enemy:
8
distance = hero.distanceTo(enemy)
计算距离
9
if enemy.type != "burl" and distance < 15:
攻击:非树精 **AND** 距离小于 15
10
hero.attack(enemy)
攻击