#! /usr/bin/env python3
import random, sys
# メッセージ分離方式: 表示用メッセージは本体から分けておく(メインテナンスの為)
msg = {'start': 'じゃんけんスタート',
'prompt': '自分の手を入力してください\n0:グー,1:チョキ,2:パー',
'win': 'あなたの勝ちです',
'lose': 'あなたの負けです',
'continue?': '続けますか?',
'wrong': '入力が間違っています',
'y-or-n': 'YesかNoを入力して下さい(y/n)',
'hand': 'コンピュータの手: {}',
0: 'グー',
1: 'チョキ',
2: 'パー'}
# 環境データ
class Env(object):
def __init__(self, lose = 0, win = 0, *hand):
self.lose = lose
self.win = win
self.hand = hand
def __repr__(self):
return f'{self.win}勝{self.lose}敗'
# 入力関数(read)
def read(env):
# 使いまわしの為 my_hand に代入
my_hand = input(msg['continue?' if env.win == 5 or env.lose == 5 else 'prompt'])
if my_hand.isalpha() or my_hand in map(str, (0, 1, 2)):
return my_hand
else:
# 想定外の入力の際、例外を投げる
raise ValueError
# プログラム本体: 計算処理だけ司り、入力も出力もしない
def result(my_hand, env):
if env.win == 5 or env.lose == 5:
# 使いまわしの為 my_hand に改めて代入
my_hand = my_hand.lower()
if my_hand == 'y' or my_hand == 'yes':
return Env()
elif my_hand == 'n' or my_hand == 'no':
sys.exit()
else:
# 判定結果を二回使いまわしするので変数に代入
hand = random.randint(0, 2)
hand_diff = (int(my_hand) - hand + 3) % 3
return Env(env.lose + 1 if hand_diff == 1 else env.lose,
env.win + 1 if hand_diff == 2 else env.win,
hand)
# 出力関数(print)
def display(env):
# コンピュータの手を表示
print(msg['hand'].format(msg[env.hand[0]]))
# 三項演算子的な書き方
print(msg['win'] if env.win == 5
else msg['lose'] if env.lose == 5
else env)
return env
# プログラム実行部
if __name__ == '__main__':
print(msg['start'])
env = Env()
while True:
try:
# env の値を更新する必要があるんで代入する
env = display(result(read(env), env))
# 以下、例外処理
except ValueError:
print(msg['wrong'])
except AttributeError:
print(msg['y-or-n'])
IyEgL3Vzci9iaW4vZW52IHB5dGhvbjMKCmltcG9ydCByYW5kb20sIHN5cwoKIyDjg6Hjg4Pjgrvjg7zjgrjliIbpm6LmlrnlvI86IOihqOekuueUqOODoeODg+OCu+ODvOOCuOOBr+acrOS9k+OBi+OCieWIhuOBkeOBpuOBiuOBjyjjg6HjgqTjg7Pjg4bjg4rjg7Pjgrnjga7ngropCm1zZyA9IHsnc3RhcnQnOiAn44GY44KD44KT44GR44KT44K544K/44O844OIJywKICAgICAgICdwcm9tcHQnOiAn6Ieq5YiG44Gu5omL44KS5YWl5Yqb44GX44Gm44GP44Gg44GV44GEXG4wOuOCsOODvCwxOuODgeODp+OCrSwyOuODkeODvCcsCiAgICAgICAnd2luJzogJ+OBguOBquOBn+OBruWLneOBoeOBp+OBmScsCiAgICAgICAnbG9zZSc6ICfjgYLjgarjgZ/jga7osqDjgZHjgafjgZknLAogICAgICAgJ2NvbnRpbnVlPyc6ICfntprjgZHjgb7jgZnjgYs/JywKICAgICAgICd3cm9uZyc6ICflhaXlipvjgYzplpPpgZXjgaPjgabjgYTjgb7jgZknLAogICAgICAgJ3ktb3Itbic6ICdZZXPjgYtOb+OCkuWFpeWKm+OBl+OBpuS4i+OBleOBhCh5L24pJywKICAgICAgICdoYW5kJzogJ+OCs+ODs+ODlOODpeODvOOCv+OBruaJizoge30nLAogICAgICAgMDogJ+OCsOODvCcsCiAgICAgICAxOiAn44OB44On44KtJywKICAgICAgIDI6ICfjg5Hjg7wnfQoKIyDnkrDlooPjg4fjg7zjgr8KY2xhc3MgRW52KG9iamVjdCk6CiAgICBkZWYgX19pbml0X18oc2VsZiwgbG9zZSA9IDAsIHdpbiA9IDAsICpoYW5kKToKICAgICAgICBzZWxmLmxvc2UgPSBsb3NlCiAgICAgICAgc2VsZi53aW4gPSB3aW4KICAgICAgICBzZWxmLmhhbmQgPSBoYW5kCiAgICBkZWYgX19yZXByX18oc2VsZik6CiAgICAgICAgcmV0dXJuIGYne3NlbGYud2lufeWLnXtzZWxmLmxvc2V95pWXJwoKIyDlhaXlipvplqLmlbAocmVhZCkKZGVmIHJlYWQoZW52KToKICAgICMg5L2/44GE44G+44KP44GX44Gu54K6IG15X2hhbmQg44Gr5Luj5YWlCiAgICBteV9oYW5kID0gaW5wdXQobXNnWydjb250aW51ZT8nIGlmIGVudi53aW4gPT0gNSBvciBlbnYubG9zZSA9PSA1IGVsc2UgJ3Byb21wdCddKQogICAgaWYgbXlfaGFuZC5pc2FscGhhKCkgb3IgbXlfaGFuZCBpbiBtYXAoc3RyLCAoMCwgMSwgMikpOgogICAgICAgIHJldHVybiBteV9oYW5kCiAgICBlbHNlOgogICAgICAgICMg5oOz5a6a5aSW44Gu5YWl5Yqb44Gu6Zqb44CB5L6L5aSW44KS5oqV44GS44KLCiAgICAgICAgcmFpc2UgVmFsdWVFcnJvcgoKIyDjg5fjg63jgrDjg6njg6DmnKzkvZM6IOioiOeul+WHpueQhuOBoOOBkeWPuOOCiuOAgeWFpeWKm+OCguWHuuWKm+OCguOBl+OBquOBhApkZWYgcmVzdWx0KG15X2hhbmQsIGVudik6CiAgICBpZiBlbnYud2luID09IDUgb3IgZW52Lmxvc2UgPT0gNToKICAgICAgICAjIOS9v+OBhOOBvuOCj+OBl+OBrueCuiBteV9oYW5kIOOBq+aUueOCgeOBpuS7o+WFpQogICAgICAgIG15X2hhbmQgPSBteV9oYW5kLmxvd2VyKCkKICAgICAgICBpZiBteV9oYW5kID09ICd5JyBvciBteV9oYW5kID09ICd5ZXMnOgogICAgICAgICAgICByZXR1cm4gRW52KCkKICAgICAgICBlbGlmIG15X2hhbmQgPT0gJ24nIG9yIG15X2hhbmQgPT0gJ25vJzoKICAgICAgICAgICAgc3lzLmV4aXQoKQogICAgZWxzZToKICAgICAgICAjIOWIpOWumue1kOaenOOCkuS6jOWbnuS9v+OBhOOBvuOCj+OBl+OBmeOCi+OBruOBp+WkieaVsOOBq+S7o+WFpQogICAgICAgIGhhbmQgPSByYW5kb20ucmFuZGludCgwLCAyKQogICAgICAgIGhhbmRfZGlmZiA9IChpbnQobXlfaGFuZCkgLSBoYW5kICsgMykgJSAzCiAgICAgICAgcmV0dXJuIEVudihlbnYubG9zZSArIDEgaWYgaGFuZF9kaWZmID09IDEgZWxzZSBlbnYubG9zZSwKICAgICAgICAgICAgICAgICAgIGVudi53aW4gKyAxIGlmIGhhbmRfZGlmZiA9PSAyIGVsc2UgZW52LndpbiwKICAgICAgICAgICAgICAgICAgIGhhbmQpCgojIOWHuuWKm+mWouaVsChwcmludCkKZGVmIGRpc3BsYXkoZW52KToKICAgICMg44Kz44Oz44OU44Ol44O844K/44Gu5omL44KS6KGo56S6CiAgICBwcmludChtc2dbJ2hhbmQnXS5mb3JtYXQobXNnW2Vudi5oYW5kWzBdXSkpCiAgICAjIOS4iemghea8lOeul+WtkOeahOOBquabuOOBjeaWuQogICAgcHJpbnQobXNnWyd3aW4nXSBpZiBlbnYud2luID09IDUKICAgICAgICAgIGVsc2UgbXNnWydsb3NlJ10gaWYgZW52Lmxvc2UgPT0gNQogICAgICAgICAgZWxzZSBlbnYpCiAgICByZXR1cm4gZW52CgojIOODl+ODreOCsOODqeODoOWun+ihjOmDqAppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgcHJpbnQobXNnWydzdGFydCddKQogICAgZW52ID0gRW52KCkKICAgIHdoaWxlIFRydWU6CiAgICAgICAgdHJ5OgogICAgICAgICAgICAjIGVudiDjga7lgKTjgpLmm7TmlrDjgZnjgovlv4XopoHjgYzjgYLjgovjgpPjgafku6PlhaXjgZnjgosKICAgICAgICAgICAgZW52ID0gZGlzcGxheShyZXN1bHQocmVhZChlbnYpLCBlbnYpKQogICAgICAgICAgICAjIOS7peS4i+OAgeS+i+WkluWHpueQhgogICAgICAgIGV4Y2VwdCBWYWx1ZUVycm9yOgogICAgICAgICAgICBwcmludChtc2dbJ3dyb25nJ10pCiAgICAgICAgZXhjZXB0IEF0dHJpYnV0ZUVycm9yOgogICAgICAgICAgICBwcmludChtc2dbJ3ktb3ItbiddKQo=