简易尼姆游戏

from math import log2
from random import randint,choice

def everystep(n):
    half = n / 2
    m = 1
    possible = []
    while True:
        rest = 2**m - 1
        if rest >= n:
            break
        if rest >= half:
            possible.append(n-rest)
        m=m+1
    if possible:
        return choice(possible)
    return randint(1,int(half))
def smartNimuGame(n):
    while n>1:
        print("Now it's your turn, and we have {0} left.".format(n))
        while True:
            try:
                num = int(input('How many do you want to  take:'))
                assert 1 <= num <= n//2
                break
            except:
                print('Error. Must be between 1 and {0}'.format(n//2))
        n-=num
        if n==1:
            return 'I fail.'
        n -= everystep(n)
    else:
        return 'Ýou fail'

print(smartNimuGame(randint(1,100)))

你可能感兴趣的:(Python)