print("=====拿棋子游戏【加强AI版】=====") print("规则:一共21颗棋子,每次拿1~3颗,拿到最后一颗获胜") total = 21
while True: print(f"\n剩余棋子:{total}") # 玩家回合 while True: try: player = int(input("请拿棋子(1‑3):")) if 1 <= player <= 3 and player <= total: break else: print("只能拿1‑3颗,不能超过剩余棋子!") except ValueError: print("请输入数字!")
total -= player if total <= 0: print("🎉恭喜,你拿到最后一颗,你赢了!") break
# AI:优先抢占4的倍数制胜点 want = total % 4 if want == 0: take = random.randint(1, min(3, total)) else: take = want
print(f"电脑拿走 {take} 颗") total -= take if total <= 0: print("💻电脑拿到最后一颗,你输了!") break