今回の学び
・勝利判定には少なくとも2つの書き方ができる
①
elif (user_hand == "rock" and computer_hand == "scissors") or \
(user_hand == "paper" and computer_hand == "rock") or \
(user_hand == "scissors" and computer_hand == "paper"):
print("You win!")
--> バックスラッシュ(\)でelifの条件を改行して書けるので、可読性が上がる。
②
elif(user_hand, computer_hand) in [("rock", "scissors"), ("paper", "rock"), ("scissors", "paper")]:
print("You win!")
--> 組み合わせをリストに格納することで、何度もuser_hand == ...と書かなくて良くなる。
・game_stages = 1を加えて、一回戦目でプレイをキャンセルした場合のメッセージをシンプルにした。(その前だと、ゲームをプレイしてなくてもyour hand...とか表示されていた)
また、game_stages += 1の箇所でX回戦目ごとにgame_stagesが+1されていくようにした。
・__name__ == __main__について
ファイルを直接実行した場合、__name__は自動的に__main__に設定される
・defで関数を作成し、それをmain()の中で呼び出している
感覚的にいうと、機能を持たせたブロックを作成しておいて、メインとなるゲームの要所要所で必要な関数を呼び出している感じ。
序盤に書いたコード
----------
#Day 6 Rock, Paper, Scissors Game
import random
options = ["rock", "paper", "scissors"]
# while Trueでユーザーがnを選択するまで入力を求める
while True:
# ユーザーの入力を受け取る部分を作成
user_hand = input("Choose Rock, Paper, Scissors: ").lower()
if user_hand not in options and user_hand != "q":
print(f"Opps! Invalid choice. Please choose one of the following: {', '.join(options)}.")
print("Or press Q to quit.")
continue
elif user_hand == "q":
print("Alirght! End game, have a nice day!")
break
# コンピュータの手をランダムに選択
computer_hand = random.choice(options)
# 勝敗を判定
if user_hand == computer_hand:
print("It's a tie!")
elif (user_hand == "rock" and computer_hand == "scissors") or \
(user_hand == "paper" and computer_hand == "rock") or \
(user_hand == "scissors" and computer_hand == "paper"):
print("You win!")
print(f"You chose {user_hand} and Computer chose {computer_hand}.")
else:
print("You lose!")
print(f"You chose {user_hand} and Computer chose {computer_hand}.")
# 再度プレイするか尋ねる
play_again = input("Would you like to play again? Enter y/n: ").lower()
if play_again != "y":
print("Thank you for playing! Have a good day.")
break
----------
最終版(まだ改善できますよ!とChatGPTに改善案をもらったけれど、一旦ここで区切ることにした)
----------
#Day 6 Rock, Paper, Scissors Game
import random
# ①ユーザーの入力を受け取る関数
def get_user_choice():
while True:
user_hand = input("Choose Rock, Paper, Scissors (or Q to quit): ").lower()
if user_hand in ["rock", "paper", "scissors", "q"]:
return user_hand
print("Oops! Invalid choice. Please choose one of the following: rock, paper, scissors. Or press Q to quit.")
# ②試合結果とスコア加算に関する関数
def play_round(user_hand, computer_hand):
print(f"You chose {user_hand}, Computer chose {computer_hand}.")
#あいこの場合
if user_hand == computer_hand:
print("It's a tie!")
return 0
# elifの()内の組み合わせが、in[(),(),()]の中にあるかチェックし、あればあなたの勝ちと表示
elif(user_hand, computer_hand) in [("rock", "scissors"), ("paper", "rock"), ("scissors", "paper")]:
print("You win!")
return 1
# それ以外の組み合わせ=負け手なので、You loseと表示
else:
print("You lose...")
return -1
# ③ゲームの流れと構成(この関数の中で①と②を呼び出す)
def main():
options = ["rock", "paper", "scissors"]
player_score = 0
computer_score = 0
game_stages = 1
# ユーザーに対戦回数を尋ねる
while True:
try:
number_of_rounds = int(input("How many rounds would you like to play?\n Enter: "))
if number_of_rounds > 0:
break
else:
print("Please enter a positive number.")
except ValueError:
print("Invalid input. Please enter a number.")
for _ in range(number_of_rounds):
user_hand = get_user_choice()
if user_hand == "q":
if game_stages == 1:
print("Alright! Ending the game. Have a nice day!")
break
elif game_stages >= 2:
print(f"Alright! Ending the game.\nYour score was: {player_score} and Computer score was: {computer_score}.")
break
computer_hand = random.choice(options)
game_result = play_round(user_hand, computer_hand)
game_stages += 1
if game_result == 1:
player_score += 1
elif game_result == -1:
computer_score += 1
print(f"Score: You {player_score} - Computer {computer_score}\n")
if game_stages > 1:
if player_score > computer_score:
print("You won the game!")
elif player_score < computer_score:
print("You lost the game...")
elif player_score == computer_score:
print("It's a draw!")
else:
print("Hope to see you again!")
if __name__ == "__main__":
main()