Chat· edited
Pythonコード作ってみた
すimport time
import random
def chat_game():
print("====================================")
print("🤖 チャットゲーム『ナンバーズ・AI』")
print("====================================\n")
time.sleep(0.5)
print("AI: 起動シークエンス完了。")
time.sleep(0.5)
print("AI: あなたに私の考えている『1から50までの数字』を当ててもらうゲームを開始します。")
time.sleep(0.5)
print("AI: チャンスは「5回」です。外すたびに私のバッテリーが減っていきます。")
time.sleep(0.5)
print("AI: それでは、最初の数字を入力してください。")
# AIがランダムに数字を決める
target_number = random.randint(1, 50)
max_attempts = 5
attempts = 0
while attempts < max_attempts:
remaining = max_attempts - attempts
# ユーザーの入力をチャット風に受付
try:
user_input = input(f"\nあなた(残りチャンス {remaining}回): ")
except (KeyboardInterrupt, EOFError):
print("\nAI: 通信が切断されました。ゲームを終了します。")
return
# 入力チェック
if not user_input.strip().isdigit():
print("AI: エラー。半角の「数字」を入力してください。")
continue
guess = int(user_input)
attempts += 1
# チャット風の演出
print("AI: 計算中...")
time.sleep(0.6)
if guess == target_number:
print(f"\nAI: 正解です!私が考えていた数字は『{target_number}』でした。")
time.sleep(0.5)
print("AI: システムを正常に維持できました。あなたの勝利です。お見事!")
break
elif guess < target_number:
print("AI: 残念。それよりも【もっと大きい数字】です。")
else:
print("AI: 残念。それよりも【もっと小さい数字】です。")
else:
# 5回間違えた場合
print("\n====================================")
print("AI: バッテリーが0になりました。システムシャットダウン。")
time.sleep(0.5)
print(f"AI: ちなみに、正解の数字は『{target_number}』でした...")
print("--- GAME OVER ---")
print("====================================")
if name == "main":
chat_game()
aiが数を当てるゲームに付き合ってくれるコードです。https://www.online-python.com/ とかで使ってみてね