Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python program For this assignment you will write a program that allows you to play a simple version of the game of Nim, either against

Python program

For this assignment you will write a program that allows you to play a simple version of the game of Nim, either against another player or against the computer. 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 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)? 2 There are no sticks left on the table! Game over. 

Part 2: Two Player Game

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.

Part 3: Play Against the Computer

Now you are going to enable your program to play (very poorly) against you. First, ask the user at the beginning of the game whether he or she wants to play against the computer or another human. Then, for the computer's moves we won't try to be smart; instead, for each turn you will generate a random number between 1 and 3 and that will be the number of sticks the computer takes. Or, if you want to be a little bit smart: if there are just 2 or 3 sticks left on the table, don't choose randomly -- take all but one of the sticks and force the other player to lose.

Here's a sample running of the game:

How many sticks (10-100)? 5 Sorry, that's too few sticks. Try again. How many sticks (10-100)? 10 Are you playing against a person, or the computer? computer 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)? -7 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: Computer There are 7 sticks on the table. I take 2 sticks. Turn: Player 1 There are 5 sticks on the table. How many sticks do you want to take (1, 2 or 3)? 3 Turn: Computer There are 2 sticks on the table. I take 1 stick. 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. Computer wins! 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2005 For Windows Mobile Web Office And Database Applications Comprehensive

Authors: Gary B. Shelly, Thomas J. Cashman, Corinne Hoisington

1st Edition

0619254823, 978-0619254827

More Books

Students also viewed these Databases questions

Question

explain the need for human resource strategies in organisations

Answered: 1 week ago

Question

describe the stages involved in human resource planning

Answered: 1 week ago