import random
hands = ["", "", "", ""]
winner_flags = [True, True, True, True]
# じゃんけんを続けるかどうか判定する
# 勝ち残った人が1人ならFalse, 2人以上ならTrueを返す
def check_continue(flags):
winner_count = 0
for flag in flags:
if flag == True:
winner_count += 1
if winner_count == 1:
return False
else:
return True
# じゃんけんの結果、勝った手を判定する
# 勝った手「グー」「チョキ」「パー」のいずれかを返す
# あいこのときは""(空の文字列)を返す
def get_winner_hand(check_hands):
hand_gu = False
hand_choki = False
hand_pa = False
winner = ""
for hand in check_hands:
if hand == "グー":
hand_gu = True
elif hand == "チョキ":
hand_choki = True
elif hand == "パー":
hand_pa = True
if hand_gu == True and hand_choki == True and hand_pa == False:
#グーとチョキのとき
winner = "グー"
elif hand_gu == False and hand_choki == True and hand_pa == True:
#チョキとパーのとき
winner = "チョキ"
elif hand_gu == True and hand_choki == False and hand_pa == True:
#グーとパーのとき
winner = "パー"
return winner
print("じゃんけんぽん")
while check_continue(winner_flags):
winners = ""
for i in range(4):
if winner_flags[i] == True:
if i == 0:
winners += "あなた "
else:
winners += "コンピュータ" + str(i) + " "
print("==========")
print("じゃんけんは" + winners + "で行います。")
print("==========")
#hands[0]:自分の手、hands[1]~hands[3]:コンピュータ1~3の手
hands = ["", "", "", ""]
if winner_flags[0] == True:
hands[0] = input("自分の手を「グー」「チョキ」「パー」で入力してください==>")
for i in range(1, 4):
if winner_flags[i] == True:
hands[i] = ["グー", "チョキ", "パー"][random.randint(0, 2)]
for i in range(4):
if hands[i] != "":
if i == 0:
print("あなたの手:" + hands[i])
else:
print("コンピュータ" + str(i) + "の手:" + hands[i])
winner_hand = get_winner_hand(hands)
if winner_hand == "":
#あいこのとき
print("あいこでしょ!")
else:
for i in range(4):
if hands[i] != winner_hand:
winner_flags[i] = False
if winner_flags[0] == True:
print("あなたの勝ちです。")
else:
for i in range(1, 4):
if winner_flags[i] == True:
print("コンピュータ" + str(i) + "の勝ちです。")