简体   繁体   中英

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

here's the code and i have ran out of ideas. i've tried changing the indenting, but it didn't work either. the purpose of the nim game is: "a player sets up 2 piles of blocks, the second player chooses whether to go first or second, they each take turns removing blocks (in this case instead of blocks i put - ) the players have to remove at least one or not more than 3 blocks from any pile, whichever player takes the last block wins.

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

As formatted your code has no capability to ask for player 2.

It will

  1. check for sum of all tiles > 0 and if so, print piles
  2. then it will ask for which pile to play
  3. then it will ask for tokens to remove and remove them
  4. then the program ends.

You are missing an outer loop that changes the player and continues until the games end condition (no more tiles left in piles) is met.

You also need some more safeguards in place to avoid ValueError if no integers are inputted and a general cleanup to heed The style guide for python .

A completed program could look like this:

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" )

And a possible game would output like that:

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

I use map(int, range(..)) to create the allowed strings to check the input agains.

I use ternary conditions for checks, see Does Python have a ternary conditional operator?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM