Python help - Игра Нима - PullRequest
       19

Python help - Игра Нима

0 голосов
/ 04 ноября 2019

Питонский новичок здесь пытается пристрелить мою игру из NIM-кода. В основном код удаляет камни 1, 2 или 3, пока их не осталось. Я пытаюсь предотвратить попадание пользователя и ИИ в негативы (если ИИ остался один камень / ИИ / пользователь не сможет удалить два или три камня.) Вот мой код:

import random
Stones = random.randint(15, 30)
User = 0
YourTurn = True

print("This is a game where players take turns taking stones from a pile of stones. The player who 
takes the last stone loses.")
print("The current stone count is:", Stones)

while True:
     while YourTurn == True and Stones > 0:
    User = int(input("How many stones do you want to remove?"))
    if User == 1:
        Stones -= 1
        print("You removed 1 stone! The current stone count is:", Stones)
        YourTurn = not True            
    elif User == 2:
        Stones -= 2
        print("You removed 2 stone! The current stone count is:", Stones)    
        YourTurn = not True                       
    elif User == 3:
        Stones -= 3
        YourTurn = not True
        print("You removed 3 stone! The current stone count is:", Stones)
    else:
        print("You can only remove a maximum of 3 stones.")

    while YourTurn == False and Stones > 0:
        AI = random.randint(1, 3)
        if AI == 1:
            Stones -= 1
            print("The A.I removed 1 stone! The current stone count is:", Stones)
            YourTurn = not False
        elif AI == 2:
            Stones -= 2
            print("The A.I removed 2 stone! The current stone count is:", Stones)
            YourTurn = not False
        elif AI == 3:
            Stones -= 3
            print("The A.I removed 3 stone! The current stone count is:", Stones)
            YourTurn = not False

if Stones <= 0:
    if YourTurn == True:
        print("The A.I took the last stone it lost. You won the game!")
        break
    elif YourTurn == False:
        print("You took the last stone you lost. The A.I won the game!")
        break

Я понятия не имею, как заставить код не переходить в негативы, а операторы if и elif, которые у меня были до этого, игнорировались кодом. Буду признателен за любую помощь.

1 Ответ

0 голосов
/ 04 ноября 2019
import random

stonesLeft = random.randint(15, 30)
stonesToRemove = 0
userTurn = True

print("This is a game where players take turns taking stones from a pile of stones. The player who takes the last stone loses. The current stone count is: ", stonesLeft)

while stonesLeft > 0:
    while userTurn == True and stonesLeft > 0:

        stonesToRemove = int(input("How many stones do you want to remove?"))

        if stonesToRemove > 3:
            print( "You can't remove more than three stones at a time! 
                The current stone count is: " + str(stonesLeft) )     
        elif stonesLeft - stonesToRemove < 0:
            print("There aren't that many stones left!") #Give user error!  
        else:
            stonesLeft -= stonesToRemove
            print( "You removed " + str(stonesToRemove) + 
                " stone(s)! The current stone count is: " + str(stonesLeft) )    

            userTurn = False                       

    while userTurn == False and stonesLeft > 0:

        aiRemoves = random.randint( 1, min(3, stonesLeft) ) #Take smaller value between 3 and the stones that are left
        stonesLeft -= aiRemoves

        print( "The A.I. removed " + str(aiRemoves) + 
            " stone(s)! The current stone count is: " + str(stonesLeft) )    

        userTurn = True 

if userTurn == True:
    print("The A.I took the last stone, it lost. You won the game!")
else:
    print("You took the last stone, you lost. The A.I. won the game!")
...