pythonを使って簡単なゲームを作成する


ゲームの内容

コンピュータとじゃんけんをするゲーム
1がグー、2がチョキ、3がパーを意味する。(小文字で入力する)ゲームを続ける場合はyes、終了する場合はnoを入力する。

ゲームのソースコード

  1. print("コンピュータと一緒にじゃんけんをしよう")
  2. print("1がグー、2がチョキ、3がパーです*小文字で入力してください")
  3. print("ゲームを続ける場合はyes、終了する場合はnoを入力してください")
  4. import random
  5. def get_user_choice():
  6.     user_choice = input("じゃんけん:(1) グー (2) チョキ (3) パー: ")
  7.     if user_choice in ["1", "2", "3"]:
  8.         return int(user_choice)
  9.     else:
  10.         print("無効な選択です。1から3の数字を選んでください。")
  11.         return get_user_choice()
  12. def get_computer_choice():
  13.     return random.randint(1, 3)
  14. def determine_winner(user_choice, computer_choice):
  15.     if user_choice == computer_choice:
  16.         return "引き分け"
  17.     elif (
  18.         (user_choice == 1 and computer_choice == 2) or
  19.         (user_choice == 2 and computer_choice == 3) or
  20.         (user_choice == 3 and computer_choice == 1)
  21.     ):
  22.         return "勝ち"
  23.     else:
  24.         return "負け"
  25. while True:
  26.     user_choice = get_user_choice()
  27.     computer_choice = get_computer_choice()
  28.     
  29.     print(f"あなたの選択: {user_choice}")
  30.     print(f"コンピュータの選択: {computer_choice}")
  31.     
  32.     result = determine_winner(user_choice, computer_choice)
  33.     print(f"結果: {result}")
  34.     
  35.     play_again = input("もう一度プレイしますか?(yes/no): ")
  36.     if play_again.lower() != "yes":
  37.         break

反省

pythonについてまだ理解できていない点が多くあったため、ChatGPTを用いて作成した。
次回の課題はプログラミングの基本となる部分を理解して取り組みたい。

*同じ班の人と連絡が取れなかったため、班で決めたゲームではないです。