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!

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education