为什么我的代码没有超过 "how many tokens would you like to take"? nim 游戏 python 3

why isn't my code going past "how many tokens would you like to take"? nim game python 3

这是代码,我 运行 没有想法。我试过改变缩进,但也没用。 nim 游戏的目的是:“一个玩家设置 2 堆积木,第二个玩家选择先走还是后走,他们每个人轮流移除积木(在这种情况下,我放的不是积木 - ) 玩家必须从任意堆中移除至少一个或不超过 3 个方块,最后拿走方块的玩家获胜。

from math import *
from random import randint
pile1 = randint(1,20)
pile2 = randint(1,20)

player= 1
print("it's,"+str(player ) +' playing')
while pile1+pile2>0 : 
  if pile1>0: 
    print ('pile 1 :'+'-'*pile1) 
  if pile2>0: 
    print('pile 2:'+'-'*pile2)   
  break
which_pile=int(input('On which pile would you like to play ? : '))
while which_pile !=1 and which_pile != 2: 
  which_pile=int(input('On which pile would you like to play ? : '))
tokens=int(input('How many tokens would you like to take ? : '))
if which_pile==1:
  while tokens<1 or tokens>min(3,pile1):
      tokens=int(input('How many tokens would you like to take ? : '))
      pile1-=tokens
else:
   while tokens<1 or tokens>min(3,pile2):
      tokens=int(input('How many tokens would you like to take ? : '))
      pile2-=tokens

按照格式,您的代码无法请求播放器 2。

  1. 检查所有方块的总和 > 0,如果是,打印堆
  2. 然后会询问要玩哪一堆
  3. 然后它会要求令牌删除并删除它们
  4. 然后程序结束

您缺少一个外部循环,该循环会更改玩家并继续直到满足游戏结束条件(不再有更多的瓷砖堆成一堆)。

如果没有输入整数,您还需要采取更多保护措施来避免 ValueError,并需要进行一般清理以注意 The style guide for python

完成的程序可能如下所示:

from math import *
from random import randint
pile1 = randint(1,20)
pile2 = randint(1,20)

player = "1"
while True:
    print(f"\n\nPlayer {player}'s turn:")

    print (f"  pile 1: {'-' * pile1}")
    print (f"  pile 2: {'-' * pile2}\n")

    if pile1 == 0 or pile2 == 0:
        print("  Only one pile left.")
        which_pile = "2" if pile1 == 0  else "1"
    else:
        # use strings to compare, no need to care about ValueErrors by int()
        which_pile = input('  On which pile would you like to play? ')
        while which_pile not in ("1", "2"): 
            which_pile = input('  On which pile would you like to play? ')

    available_tokens = pile1 if which_pile == "1" else pile2

    tokens = input('  How many tokens would you like to take ? ')

    # make sure you do not take more then 3 (or the amount in the pile)
    while tokens not in map(str, range(1, min(4, available_tokens+1))):
        tokens = input('  How many tokens would you like to take ? ')

    # only playe you really need an int - and its ensured above it
    # can be converted to an integer
    tokens = int(tokens)

    if which_pile == "1":
        pile1 -= tokens
    else:
        pile2 -= tokens

    if pile1 + pile2 == 0:
        break # exit while True loop

    # change player
    player = "2" if player == "1" else "1"

print(f"Player {player} wins" )

一个可能的游戏会这样输出:

Player 1's turn:
  pile 1: -----
  pile 2: --

  On which pile would you like to play? 1
  How many tokens would you like to take ? 3    

Player 2's turn:
  pile 1: --
  pile 2: --

  On which pile would you like to play? 1
  How many tokens would you like to take ? 2


Player 1's turn:
  pile 1:
  pile 2: --

  Only one pile left.
  How many tokens would you like to take ? 1


Player 2's turn:
  pile 1:
  pile 2: -

  Only one pile left.
  How many tokens would you like to take ? 1
Player 2 wins

我使用 map(int, range(..)) 创建允许的字符串以再次检查输入。

我使用三元条件进行检查,参见Does Python have a ternary conditional operator?