Question

Problem: Pick Up Sticks For this assignment you will be creating an interface for two humans...

Problem: Pick Up Sticks

For this assignment you will be creating an interface for two humans to play a simple game of "Pick Up Sticks". Here's how the game is played in the "real world":

  • The game begins with a number of sticks on a table (between 10 and 100)
  • Each player, in turn, takes between 1-3 sticks off the table.
  • The player to take the last stick loses.

Your job is to build a virtual version of the game using Python. The program is broken into a series of parts to help you understand how to put together a game with many different components. Each part builds on the previous one, so you should complete them in order and turn in your the final part as your solution.

Part 1: Single Player Game

Begin by asking the user for a number of sticks to be used in the game. Only accept numbers between 10 and 100 - if the user supplies a number outside of this range you should re-prompt them.

Next, continually announce to the user how many sticks are on the table and ask the user to enter in a number of sticks to take away. Only accept choices of 1,2 or 3 sticks - anything else should cause an error message to be displayed. Once a valid number of sticks has been entered you should deduct that # of sticks from the total number of sticks in the game. When you reach 0 sticks left the game is over.

Here is a sample running of the program:

How many sticks (10-100)? 999
Sorry, that's too many sticks. Try again
How many sticks (10-100)? -10
Sorry, that's too few sticks. Try again.
How many sticks (10-100)? 10
Great Let's play ...

There are 10 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 999
Sorry, that's not a valid number.

There are 10 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 0
Sorry, that's not a valid number.

There are 10 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 3

There are 7 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 2

There are 5 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 3

There are 2 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 3
Sorry, that would bring the total # of sticks below 0. Try again.

There are 2 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 2

There are no sticks left on the table!  Game over.          

Part 2: Two Player Game

Note: Part 2 builds on Part 1, so make a copy of the code you wrote for Part 1 to get started. You will be turning in both parts at the end of the assignment.

As you can see, the single player version of this game isn't very fun. To make things more interesting we are now going to add in an element of competition. For this part you will be implementing a two player game where players take turns taking sticks from the table. The same rules apply - each player can only take 1, 2 or 3 sticks per turn. The player who takes the last stick off of the table is the loser.

Here's a sample running of the game:

How many sticks (10-100)? 999
Sorry, that's too many sticks. Try again
How many sticks (10-100)? 10
Great Let's play ...

Turn: Player 1
There are 10 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 999
Sorry, that's not a valid number.

Turn: Player 1
There are 10 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 0
Sorry, that's not a valid number.

Turn: Player 1
There are 10 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 3

Turn: Player 2
There are 7 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 2

Turn: Player 1
There are 5 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 3

Turn: Player 2
There are 2 sticks on the table.
How many sticks do you want to take (1, 2 or 3)? 1

Turn: Player 1
There is 1 stick on the table.
How many sticks do you want to take (1, 2 or 3)? 0
Sorry, that's not a valid number.

Turn: Player 1
There is 1 stick on the table.
How many sticks do you want to take (1, 2 or 3)? 1

There are no sticks left on the table!  Game over.  
Player 2 wins!          

Here are some hints to get you started:

  • You may want to use a variable to keep track of the players and who's turn it is
  • In your game you only want to switch turns when the player takes a valid # of sticks
  • Do not allow players to take more than the total # of sticks on the table (i.e. if there are 2 sticks left the player should not be able to take 3 sticks)
  • The player who takes the last stick is the loser.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Part 1:

"""
Python version : 3.6
Python program to simulate the game of pick up stick with one player
"""

# input the number of sticks on the table
numSticks = int(input('How many sticks (10-100)? '))

# validate number of sticks on table, if invalid re-prompt until valid  
while numSticks < 10 or numSticks > 100:
   if numSticks > 100:
       print("Sorry, that's too many sticks. Try again")
   else:
       print("Sorry, that's too few sticks. Try again.")
          
   numSticks = int(input('How many sticks (10-100)? '))
  
print("Great Let's play ...")
  
# loop that continues until there are no sticks on the table  
while numSticks > 0:
   # display the number of sticks on the table
   print('\nThere are ',numSticks,' sticks on the table.')
  
   # input number of sticks picked up  
   sticksPicked = int(input('How many sticks do you want to take (1, 2 or 3)? '))
  
   # validate number of sticks picked up is between [1,3]  
   if sticksPicked < 1 or sticksPicked > 3:
       print("Sorry, that's not a valid number.")
   # validate number of sticks picked up is not greater than number of sticks present in the table
   elif sticksPicked > numSticks:
       print("Sorry, that would bring the total # of sticks below 0. Try again.")
   else: # valid number of sticks picked up, remove that number from the table
       numSticks -= sticksPicked
  
print('\nThere are no sticks left on the table! Game over.')
#end of program

Code Screenshot:

Python version : 3.6 Python program to simulate the game of pick up stick with one player 4 5 6 7 8 9 # input the number of s

Output:

How many sticks (10-100)? 999 Sorry, thats too many sticks. Try again How many sticks (10-100)? -10 Sorry, thats too few st

Part 2:

"""
Python version : 3.6
Python program to simulate the game of pick up stick with two players
"""

player1 = True # player1 starts the game

# input the number of sticks on the table
numSticks = int(input('How many sticks (10-100)? '))

# validate number of sticks on table, if invalid re-prompt until valid      
while numSticks < 10 or numSticks > 100:
   if numSticks > 100:
       print("Sorry, that's too many sticks. Try again")
   else:
       print("Sorry, that's too few sticks. Try again.")
          
   numSticks = int(input('How many sticks (10-100)? '))
  
print("Great Let's play ...")
  
# loop that continues until there are no sticks on the table      
while numSticks > 0:
   print('')
   # display the players turn
   if player1 :
       print('Turn: Player 1')
   else:
       print('Turn: Player 2')
  
   # display the number of sticks on the table  
   print('There are ',numSticks,' sticks on the table.')
      
   # input number of sticks picked up      
   sticksPicked = int(input('How many sticks do you want to take (1, 2 or 3)? '))
      
   # validate number of sticks picked up is between [1,3]      
   if sticksPicked < 1 or sticksPicked > 3:
       print("Sorry, that's not a valid number.")
   # validate number of sticks picked up is not greater than number of sticks present in the table
   elif sticksPicked > numSticks:
       print("Sorry, that would bring the total # of sticks below 0. Try again.")
   else: # valid number of sticks picked up, remove that number from the table
       numSticks -= sticksPicked
       # if there are sticks on the table, change the player
       if numSticks > 0:
           player1 = not player1

# determine the winner of the game  
print('\nThere are no sticks left on the table! Game over.')
if player1:
   print('Player 2 wins!')
else:
   print('Player 1 wins!')

#end of program

Code Screenshot:

Python version: 3.6 Python program to simulate the game of pick up stick with two players L player1 = True # player1 starts t

# input number of sticks picked up sticksPicked = int(input(How many sticks do you want to take (1, 2 or 3)? )) 34 35 36 37

Output:

How many sticks (10-100)? 999 Sorry, thats too many sticks. Try again How many sticks (10-100)? -10 Sorry, thats too few st

Add a comment
Know the answer?
Add Answer to:
Problem: Pick Up Sticks For this assignment you will be creating an interface for two humans...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT