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!
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
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!](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F80693d1d-bb9d-4c3e-ba65-51f03ba654df%2F9f58b713-039d-493e-b34a-746c9b121e7c%2Fpz08wjc_processed.png&w=3840&q=75)
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 6 steps with 5 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
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.Recommended textbooks for you
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education