嗨,各位程式碼冒險家!歡迎來到我的「順著感覺走!從零開始的 Python & Vibe Coding 遊戲創作」第二十二天。昨天,我們揭示了 AI 如何像一個機會主義者,在其回合中積極尋找並執行所有可行的卡牌配對。然而,一個真正具有挑戰性的 AI,在無計可施時,並不會坐以待斃。
今天,我們將深入 process_enemy_turn
方法的後半段,探討當 AI 手中沒有任何一張牌能與桌面配對時,它是如何啟動一套精密的評估機制,從而做出最具策略性的出牌決策,為未來的回合進行佈局。
在 process_enemy_turn
的 while
迴圈中,當 potential_matches
清單為空時,代表 AI 已經無法再進行任何配對。此時,它會進入一個全新的決策分支——策略性出牌。AI 的目標是從所有「無法配對」的手牌中,挑選出一張打到桌面上最有價值的牌。
這個決策的核心,正是 _evaluate_card_for_enemy_advanced
這個私有方法。
gamemanager.py
)# 位於 process_enemy_turn 方法內
# ...
# 如果沒有可配對的牌
else:
# 1. 篩選出所有無法配對的手牌
unmatched_hand_cards = [
(hand_idx, card) for hand_idx, card in enumerate(enemy.hand)
if not any(card.card_type == c.card_type for c in self.center_pile)
]
if unmatched_hand_cards:
best_card_to_play = None
best_card_index = -1
max_score = -1
# 2. 為每張無法配對的手牌進行評分
for hand_idx, card in unmatched_hand_cards:
score = self._evaluate_card_for_enemy_advanced(card, enemy, player)
if score > max_score:
max_score = score
best_card_to_play = card
best_card_index = hand_idx # 記錄在 unmatched_hand_cards 中的索引
# 3. 打出分數最高的牌
if best_card_to_play:
# 從原始手牌中找到正確的索引並打出
original_hand_index = unmatched_hand_cards[best_card_index]
card_to_play_on_table = enemy.hand.pop(original_hand_index)
self.center_pile.append(card_to_play_on_table)
self.add_to_log(f"敵方打出 {card_to_play_on_table.card_type.value}。")
# ...
break # 出牌後結束回合
_evaluate_card_for_enemy_advanced
深度解析這個方法是 AI 智慧的體現。它為每一張候選卡牌計算一個「分數」,這個分數並非固定不變,而是根據當前的戰局動態調整,旨在最大化自身利益,同時對玩家造成最大威脅。
gamemanager.py
)def _evaluate_card_for_enemy_advanced(self, card, enemy, player):
effect = SkillEffect.get(card.card_type, {})
score = 0
enemy_low_health_threshold = enemy.max_health * 0.3
player_low_health_threshold = player.max_health * 0.3
# 1. 傷害評估 (Damage)
if "damage" in effect:
damage = effect["damage"]
is_piercing = effect.get("pierce_shield", False)
if player.shield > 0:
if is_piercing:
score += damage * 2.0 # 穿透傷害價值極高
elif damage > player.shield:
score += (damage - player.shield) * 1.5 # 破盾傷害有加成
else:
score += damage * 0.5 # 未破盾傷害價值較低
else:
score += damage * 2.0 # 對無盾目標傷害價值高
# 斬殺加成
if player.health <= player_low_health_threshold and not player.shield:
score += damage * 3.0
# 2. 治療評估 (Heal)
if "heal" in effect:
heal_amount = effect["heal"]
if enemy.health <= enemy_low_health_threshold:
score += heal_amount * 3.0 # 自保時治療價值極高
else:
score += heal_amount * 1.0
# 3. 護盾評估 (Shield)
if "shield" in effect:
shield_amount = effect["shield"]
if enemy.health <= enemy_low_health_threshold:
score += shield_amount * 2.5 # 危急時護盾價值很高
else:
score += shield_amount * 1.0
# 4. 額外行動評估 (Extra Moves)
if "extra_moves" in effect:
score += effect["extra_moves"] * 2.0 # 額外行動代表未來潛力
return score
is_piercing
):當玩家有護盾時,穿透傷害被賦予 2.0 的高倍率,因為它能直接威脅生命值。player_low_health_threshold
) 且無護盾時,傷害卡牌的價值會被提升到最高的 3.0 倍率,模擬 AI 優先擊殺殘血對手的行為。enemy_low_health_threshold
) 時,治療 (heal
) 和護盾 (shield
) 卡牌會分別獲得 3.0 和 2.5 的超高倍率加成。這使得 AI 在逆境時會優先考慮自保,而不是盲目進攻。透過這套精心設計的評估系統,敵方 AI 的行為變得遠不止是隨機出牌。它能夠根據戰場的瞬息萬變,動態評估每一張牌的潛在價值,並在無法直接進攻時,做出最有利於長遠戰局的佈局決策。這種基於權重和情境判斷的設計,正是賦予 AI「靈魂」,讓《奇幻卡牌競技場》的對決充滿變數與挑戰的關鍵所在。
明天,我們將完整地串連起回合推進與遊戲結束的判斷機制,看看一場激烈的戰鬥是如何迎來最終的裁決。敬請期待!