#!/usr/bin/env python3
# ゲームの環境情報
class World:
def __init__(self, stones, player):
self.stones = stones
self.player = player
# メッセージ分離方式
Messages = {"init" : "石の数 (10以上) :",
"number-of-stones" : "石の数: {0}\n",
"prompt" : "プレイヤー{0}の番です\n何個取る (1〜3個) ?",
"winner" : "プレイヤー{0}の勝ち\n"}
# 入力の最小値と最大値
Min, Max = 0, 4
# Eval
def world_go(x, env):
stones = env.stones - x
player = 2 if env.player == 1 else 1
return World(stones, player)
# Eval(短縮版)
##def world_go(x, env):
## return World(env.stones - x, 2 if env.player == 1 else 1)
# Read
def read(env, prompt):
while True:
num = int(input(prompt.format(env.player)))
if num > Min or num < Max:
return num
# REPL 用補助関数
# ゲーム終了判定
def is_game_end(env):
return env.stones < 2
# ゲーム勝者表示
def winner(env):
player = env.player
if env.stones == 0:
return player
else:
return 2 if player == 1 else 1
# 初期化
def init():
while True:
try: num = int(input(Messages["init"]))
except ValueError: continue
if num > 9:
return World(num, 1)
# ゲーム実行
if __name__ == "__main__":
env = init()
while True:
print(Messages["number-of-stones"].format(env.stones))
if is_game_end(env):
print(Messages["winner"].format(winner(env)))
break
else:
try: env = world_go(read(env, Messages["prompt"]), env)
except ValueError: env
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uMwoKIyDjgrLjg7zjg6Djga7nkrDlooPmg4XloLEKY2xhc3MgV29ybGQ6CiAgICBkZWYgX19pbml0X18oc2VsZiwgc3RvbmVzLCBwbGF5ZXIpOgogICAgICAgIHNlbGYuc3RvbmVzID0gc3RvbmVzCiAgICAgICAgc2VsZi5wbGF5ZXIgPSBwbGF5ZXIKCiMg44Oh44OD44K744O844K45YiG6Zui5pa55byPCk1lc3NhZ2VzID0geyJpbml0IiA6ICLnn7Pjga7mlbAgKDEw5Lul5LiKKSA6IiwKICAgICAgICAgICAgIm51bWJlci1vZi1zdG9uZXMiIDogIuefs+OBruaVsDogezB9XG4iLAogICAgICAgICAgICAicHJvbXB0IiA6ICLjg5fjg6zjgqTjg6Tjg7x7MH3jga7nlarjgafjgZlcbuS9leWAi+WPluOCiyAoMeOAnDPlgIspID8iLAogICAgICAgICAgICAid2lubmVyIiA6ICLjg5fjg6zjgqTjg6Tjg7x7MH3jga7li53jgaFcbiJ9CgojIOWFpeWKm+OBruacgOWwj+WApOOBqOacgOWkp+WApApNaW4sIE1heCA9IDAsIDQKCiMgRXZhbApkZWYgd29ybGRfZ28oeCwgZW52KToKICAgIHN0b25lcyA9IGVudi5zdG9uZXMgLSB4CiAgICBwbGF5ZXIgPSAyIGlmIGVudi5wbGF5ZXIgPT0gMSBlbHNlIDEKICAgIHJldHVybiBXb3JsZChzdG9uZXMsIHBsYXllcikKCiMgRXZhbCjnn63nuK7niYgpCiMjZGVmIHdvcmxkX2dvKHgsIGVudik6CiMjICAgIHJldHVybiBXb3JsZChlbnYuc3RvbmVzIC0geCwgMiBpZiBlbnYucGxheWVyID09IDEgZWxzZSAxKQoKIyBSZWFkCmRlZiByZWFkKGVudiwgcHJvbXB0KToKICAgIHdoaWxlIFRydWU6CiAgICAgICAgbnVtID0gaW50KGlucHV0KHByb21wdC5mb3JtYXQoZW52LnBsYXllcikpKQogICAgICAgIGlmIG51bSA+IE1pbiBvciBudW0gPCBNYXg6CiAgICAgICAgICAgIHJldHVybiBudW0KCiMgUkVQTCDnlKjoo5zliqnplqLmlbAKIyDjgrLjg7zjg6DntYLkuobliKTlrpoKZGVmIGlzX2dhbWVfZW5kKGVudik6CiAgICByZXR1cm4gZW52LnN0b25lcyA8IDIKCiMg44Ky44O844Og5Yud6ICF6KGo56S6CmRlZiB3aW5uZXIoZW52KToKICAgIHBsYXllciA9IGVudi5wbGF5ZXIKICAgIGlmIGVudi5zdG9uZXMgPT0gMDoKICAgICAgICByZXR1cm4gcGxheWVyCiAgICBlbHNlOgogICAgICAgIHJldHVybiAyIGlmIHBsYXllciA9PSAxIGVsc2UgMQoKIyDliJ3mnJ/ljJYKZGVmIGluaXQoKToKICAgIHdoaWxlIFRydWU6CiAgICAgICAgdHJ5OiBudW0gPSBpbnQoaW5wdXQoTWVzc2FnZXNbImluaXQiXSkpCiAgICAgICAgZXhjZXB0IFZhbHVlRXJyb3I6IGNvbnRpbnVlCiAgICAgICAgaWYgbnVtID4gOToKICAgICAgICAgICAgcmV0dXJuIFdvcmxkKG51bSwgMSkKCiMg44Ky44O844Og5a6f6KGMCgppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOgogICAgZW52ID0gaW5pdCgpCiAgICB3aGlsZSBUcnVlOgogICAgICAgIHByaW50KE1lc3NhZ2VzWyJudW1iZXItb2Ytc3RvbmVzIl0uZm9ybWF0KGVudi5zdG9uZXMpKQogICAgICAgIGlmIGlzX2dhbWVfZW5kKGVudik6CiAgICAgICAgICAgIHByaW50KE1lc3NhZ2VzWyJ3aW5uZXIiXS5mb3JtYXQod2lubmVyKGVudikpKQogICAgICAgICAgICBicmVhawogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHRyeTogZW52ID0gd29ybGRfZ28ocmVhZChlbnYsIE1lc3NhZ2VzWyJwcm9tcHQiXSksIGVudikKICAgICAgICAgICAgZXhjZXB0IFZhbHVlRXJyb3I6IGVudgo=