puts 'Qiita'
Pythonを使ってコードを組んでみました。
random 関数を使用した「格闘ゲーム」ユーザーが技を出して、勝ち負けを判定するゲームです。
ゲームのルール
プログラムがランダムで「蹴る」「殴る」「飛ぶ」「避ける」「フェイント」 から1つを選びます。
ユーザーが「蹴る」「殴る」「飛ぶ」「避ける」「フェイント」のいずれかを入力します。
プログラムが勝ち負けを判定し、結果を表示します。
ユーザーが「終了」と入力するまで何度もプレイできます。
格闘ゲーム
import random
acction = ['蹴る','殴る','飛ぶ','避ける','フェイント']
#格闘ゲーム
print("格闘ゲームを始めます!「蹴る」「殴る」「飛ぶ」「避ける」「フェイント」を入力してください。終了するには「終了」と入力してください。")
while True:
# コンピュータの手をランダムに選ぶ
computer_choice = random.choice(action)
user_input = input("あなたの技: ")
if user_input == '終了': # Tabをするのが重要 tabがされていないと構文をかいても使えない
print('ゲームを終了します')
break
if user_input not in action:
print("エラー: 「蹴る」「殴る」「飛ぶ」「避ける」「フェイント」のいずれかを入力してください。")
continue
print(f'コンピュータの技: {computer_choice}')
if user_input == computer_choice:
print("引き分けです!")
elif (user_input == "蹴る" and computer_choice == "殴る") or \
(user_input == "飛ぶ" and computer_choice == "蹴る") or \
(user_input == "避ける" and computer_choice == "殴る") or \
(user_input == "フェイント" and computer_choice == "蹴る"):
print("あなたの勝ちです!")
else:
print("コンピュータの勝ちです!")