# Name: # Section: # nims.py def play_nims(pile, max_stones): ''' An interactive two-person game; also known as Stones. @param pile: the number of stones in the pile to start @param max_stones: the maximum number of stones you can take on one turn ''' ## Basic structure of program (feel free to alter as you please): # while [pile is not empty]: # while [player 1's answer is not valid]: # [ask player 1] # [execute player 1's move] # # while [player 2's answer is not valid]: # [ask player 2] # [execute player 2's move] # # print "Game over" print 'Welcome to the game of Nims.' print 'There are', pile, 'stones left' while pile > 0: for player in ['Player 1', 'Player 2']: valid = False # until player 1 gives a valid input, we contiually loop while not valid: take = raw_input(str('How many stones does ' + player + ' take? ')) # check if input was a number if not take.isdigit(): print 'Input must be an integer.' continue take = int(take) # check if input was allowed if take > max_stones: print 'You are not allowed to take more than', max_stones, 'stones' elif take < 1: print 'You must take a non-negative number of stones' elif take > pile: print 'There are not enough stones left in the pile. Only', pile, 'left in the pile' else: valid = True pile -= take # check if player won, break out of for loop if necessary if pile == 0: print player, 'won!' print 'Game Over' break print 'There are', pile, 'stones left' if pile == 0: break