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
  • Develop a game using Matlab. Work in groups of two. A Game of Sticks The rules...

    Develop a game using Matlab. Work in groups of two. A Game of Sticks The rules are as follows: Players: 2 Sticks: 20 Players take turns sequentially to pick 1~3 sticks. Loser picks up the last stick! This project has the following deliverables: (1) Your project implements a 2-player version of the game of sticks in the command window. Required display in command window: ‘Welcome to the game!’ ‘Please enter player 1 name:’ ‘Please enter player 1 name:’ ‘Player 1...

  • 1. NIM game. This is a different version or easier version of NIM game Consider a pile of 5 matchsticks. Two people take turns removing 1 or 2 sticks each time from this pile. Suppose both players pl...

    1. NIM game. This is a different version or easier version of NIM game Consider a pile of 5 matchsticks. Two people take turns removing 1 or 2 sticks each time from this pile. Suppose both players play smartly (nobody plays a fool move trying to let the opponent wins. But there is only one winner anyway) a)If the person getting the last stick wins, will the first player win? Why? Show the steps the first and second player will...

  • Developing an optimal strategy for a variant of the game Nim Nim is a subtraction game...

    Developing an optimal strategy for a variant of the game Nim Nim is a subtraction game that is played with sticks. The subtraction game variant is simple. A pile of sticks is placed in front of a pair of participants. The players take turns removing either 1, 2, 3, or 4 sticks from the pile. The player who removes that last stick from the pile loses the game. It turns out that there is an optimal strategy for playing this...

  • 1. NIM game is a game with multiple piles of match sticks with the two players...

    1. NIM game is a game with multiple piles of match sticks with the two players taking turns to remove 1 pile or part of 1 pile. The person getting the last stick wins. In the class I had shown how (1, 1) and (2, 2) are losing configurations (i.e. the person facing two piles of 2 and 2 sticks will lose). a) Prove that (3, 4) is a winning configuration. Show how the 1s player can win if she...

  • Need help answering part A-C of this Calc question. # of red sides showing #0f sticks...

    Need help answering part A-C of this Calc question. # of red sides showing #0f sticks tossed 0 1 2 3 4 5 6 7 Tablh /. Each cell of the table contains the number of ways of tossing a certain number of sticks and getting a certain number of red sides. 1.2 Binomial coefficients. The boardgames Senet (Egypt) and The Royal Game of Ur (Mesopotamia) predate Zeno and Archimedes by well over a thousand years. In some versions of...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • Your task in to design a game of Nim in Python. In this game “Two players take turns removing objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remo...

    Your task in to design a game of Nim in Python. In this game “Two players take turns removing objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap/pile. The goal of the game is to avoid taking the last object.” (Wikipedia) In your implementation of the game, there will be 3 piles of objects. At the start...

  • Python 3.7 Card Game:   In this assignment you will be creating ‘card game’ with specific rules....

    Python 3.7 Card Game:   In this assignment you will be creating ‘card game’ with specific rules. Rules of the game are fairly simple. The game will be played with 3 players, two AI players and 1 Human player.   Rules of the game:   1- Will have 3 players (2 AI, and 1 Human player).   2- The cards we have for this game is three ones (1), three twos (2), and three three’s (3).   3- Need to shuffle the cards randomly so...

  • Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of...

    Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of scoring outcomes of a single turn in a game called Pig. Your Task For this assignment you will simulate a given number of hold?at?N turns of a game called Pig, and report the estimated probabilities of the possible scoring outcomes. You are NOT implementing the game of Pig, only a single turn of this game. The value of N will be acquired via user...

  • Task 1 Draw a flowchart that presents the steps of the algorithm required to perform the task spe...

    Task 1 Draw a flowchart that presents the steps of the algorithm required to perform the task specified. You can draw the flowcharts with a pen/pencil on a piece of paper and scan it for submission. Please ensure that the scanned file and your handwriting are clear and legible. However, it is preferable to draw flowcharts using a drawing software. Here are links to some free drawing tools https://www.draw.io/ www.lucidchart.com http://dia-installer.de/ https://pencil.evolus.vn/ ---------------------------------DON'T NEED THE PYTHON CODE... JUST THE ALGORITHM...

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