import random
state = {
"titCount": 10,
"breedCooldown": 0,
"bfEnergy": 100,
"bfPrey": 0,
"rsEnergy": 100,
"rsPrey": 0
}
def try_eat(energy, prey, species):
if energy < 30 and prey > 0:
prey -= 1
energy = min(energy + 50, 200)
print(f"🍗 {species} 吃掉一只储备的北长尾山雀,体力 +50(当前体力:{energy},剩余储备:{prey})")
return energy, prey
def hunt(titCount, energy, prey, species):
if energy < 10:
print(f"😴 {species} 太累了,无法捕猎。")
return titCount, energy, prey, False
hunt_result = random.choice(\[1,1,1,1,1,1,2,2,2,2\])
if hunt_result == 1:
if energy < 20:
print(f"⚡ {species} 体力不足,捕猎失败。")
energy = max(0, energy - 10)
return titCount, energy, prey, False
if titCount > 0:
titCount -= 1
prey += 1
energy = max(0, energy - 20)
print(f"🦅 {species} 捕猎成功!北长尾山雀 -1,猎物 +1,体力 -20(当前体力:{energy})")
return titCount, energy, prey, True
else:
print(f"❌ 已经没有北长尾山雀可捕了,{species} 捕猎失败。")
return titCount, energy, prey, False
else:
energy = max(0, energy - 10)
fail_reason = random.choice(\[1, 2\])
if fail_reason == 1:
print(f"🐾 {species} 扑了个空……(当前体力:{energy})")
else:
print(f"💖 {species} 被北长尾山雀萌到了,没忍心捕猎!(当前体力:{energy})")
return titCount, energy, prey, False
def try_breed(state):
if state\["titCount"\] < 3 and state\["breedCooldown"\] == 0:
increase = random.randint(2, 5)
state\["titCount"\] += increase
state\["breedCooldown"\] = 3
print(f"🐣 北长尾山雀开始繁殖!数量增加了 {increase} 只(当前总数:{state\['titCount'\]})")
def handle_conflict(state):
if state\["titCount"\] == 1 and state\["bfEnergy"\] >= 10 and state\["rsEnergy"\] >= 10:
print("⚔️ 黑足猫和锈斑豹猫同时盯上同一只北长尾山雀,发生争斗!")
state\["bfEnergy"\] = max(0, state\["bfEnergy"\] - 15)
state\["rsEnergy"\] = max(0, state\["rsEnergy"\] - 15)
state\["titCount"\] -= 1
print(f"💥 山雀趁机逃跑,双方各消耗15体力。山雀数量 -1(剩余:{state\['titCount'\]})")
return True
return False
def simulate_round(state):
# 繁殖
try_breed(state)
if state\["breedCooldown"\] > 0:
state\["breedCooldown"\] -= 1
# 随机顺序
order = \["bf", "rs"\]
if random.random() < 0.5:
order = \["rs", "bf"\]
print(f"\\n🎲 行动顺序:{'黑足猫' if order\[0\]=='bf' else '锈斑豹猫'} 先,{'黑足猫' if order\[1\]=='bf' else '锈斑豹猫'} 后")
# 进食
state\["bfEnergy"\], state\["bfPrey"\] = try_eat(state\["bfEnergy"\], state\["bfPrey"\], "黑足猫")
state\["rsEnergy"\], state\["rsPrey"\] = try_eat(state\["rsEnergy"\], state\["rsPrey"\], "锈斑豹猫")
# 冲突检查
if handle_conflict(state):
return
# 第一个捕食者
if order\[0\] == "bf":
state\["titCount"\], state\["bfEnergy"\], state\["bfPrey"\], \_ = hunt(state\["titCount"\], state\["bfEnergy"\], state\["bfPrey"\], "黑足猫")
else:
state\["titCount"\], state\["rsEnergy"\], state\["rsPrey"\], \_ = hunt(state\["titCount"\], state\["rsEnergy"\], state\["rsPrey"\], "锈斑豹猫")
if state\["titCount"\] <= 0:
print("⚠️ 北长尾山雀已暂时消失,第二个捕食者无法捕猎。")
return
# 第二个捕食者
if order\[1\] == "bf":
state\["titCount"\], state\["bfEnergy"\], state\["bfPrey"\], \_ = hunt(state\["titCount"\], state\["bfEnergy"\], state\["bfPrey"\], "黑足猫")
else:
state\["titCount"\], state\["rsEnergy"\], state\["rsPrey"\], \_ = hunt(state\["titCount"\], state\["rsEnergy"\], state\["rsPrey"\], "锈斑豹猫")
print("=== 初始状态 ===", state)
for i in range(1, 21):
print(f"\\n========== 第 {i} 回合 ==========")
simulate_round(state)
if state\["titCount"\] <= 0:
print("⚠️ 北长尾山雀已经灭绝,模拟结束。")
break
if state\["bfEnergy"\] <= 0 and state\["rsEnergy"\] <= 0:
print("⚠️ 所有捕食者都饿死了,模拟结束。")
break
print("\n=== 最终状态 ===", state)
2月17日 04:44