There are 22 stones. How many would you like? 3 There are 19 stones. The computer takes 2 stones. There are 17 stones. How many would you like? 3 There are 14 stones. The computer takes 2 stones. There are 12 stones. How nany would you like? 3 There are 9 stones. The computer takes 3 stones. There are 6 stones. How many would you like? 3 There are 3 stones. The computer takes 3 stones. The player beats the computer!

Operations Research : Applications and Algorithms
4th Edition
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Wayne L. Winston
Chapter19: Probabilistic Dynamic Programming
Section: Chapter Questions
Problem 5RP
icon
Related questions
Question
100%

The game of Nim starts with a random number of stones between 15 and 30. Two players alternate turns and on each turn may take either 1, 2, or 3 stones from the pile. The player forced to take the last stone loses. Create a nim application in python that allows the user to play against the computer. In this version of the game, the application generates the number of stones to begin with, the number of stones the computer takes, and the user goes first. The Nim application code should: 

  • prevent the user and the computer from taking an illegal number of stones. For example, neither should be allowed to take three stones when there are only 1 or 2 left.

  • include an is_valid_entry() function to check user input.

  • include a draw_stones() function that generates a random number from 1 to 3 for the number of stones the computer draws.

 

  • include separate functions to handle the user’s turn and the computer’s turn.
My problem is that the code is not outputting like it's supposed to. I've attached the proper output to this question. If you could fix the output and make it more efficient/improve it that would be awesome! :)
 
 
import random

 

player1 = "player"
player2 = "Computer"
plyrStonesTakes=0
gameover=False
stonesNumber=random.randint(15,30)



def draw_stones(): #generates a random number from 1 to 3 for the number of stones the computer draws
    compStonesTakes=random.randint(1,3)
    global stonesNumber
    while compStonesTakes>stonesNumber:
        compStonesTakes=random.randint(1,3)
    stonesNumber-=compStonesTakes
    return stonesNumber

 

def removingStonesHuman(): #Calculates the total stones left after the player removes their stones
    global stonesNumber
    stonesNumber-=plyrStonesTakes
    return stonesNumber

 

def is_valid_entry(): #Check if plyrStonesTakes is legal and diplays respective message
    global plyrStonesTakes
    legalMove=False
    while not legalMove:
        plyrStonesTakes=int(input("How many stones would you like? "))
        if  plyrStonesTakes>3 or plyrStonesTakes<1:
            print("Illegal move. Please enter a number between 1 and 3.")
        else:
            legalMove=True
    while plyrStonesTakes>stonesNumber:
        print("The number entered is greater than the remaining number of stones.")
        plyrStonesTakes=int(input("How many stones would you like?"))
    return plyrStonesTakes
   
def checkWinner(player): #checks if anyone has won the game
    if stonesNumber <=0:
        print(player," wins.")
        global gameover
        gameover=True
        return gameover

 

while gameover==False:
    print("Computers turn. There are",draw_stones(), "stones.")
    checkWinner(player1)
    if gameover==True:
        break
    is_valid_entry()        
    print("The number of stones left: ",removingStonesHuman())
    checkWinner(player2)

 

There are 22 stones. How many would you like? 3
There are 19 stones. The computer takes 2 stones.
There are 17 stones. How many would you like? 3
There are 14 stones. The computer takes 2 stones.
There are 12 stones. How many would you like? 3
There are 9 stones. The computer takes 3 stones.
There are 6 stones. How many would you like? 3
There are 3 stones. The computer takes 3 stones.
The player beats the computer!
Transcribed Image Text:There are 22 stones. How many would you like? 3 There are 19 stones. The computer takes 2 stones. There are 17 stones. How many would you like? 3 There are 14 stones. The computer takes 2 stones. There are 12 stones. How many would you like? 3 There are 9 stones. The computer takes 3 stones. There are 6 stones. How many would you like? 3 There are 3 stones. The computer takes 3 stones. The player beats the computer!
Expert Solution
steps

Step by step

Solved in 6 steps with 5 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Operations Research : Applications and Algorithms
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT