4.8. Nims assignment

In this game, two players sit in front of a pile of 100 stones. They take turns, each removing between 1 and 5 stones (assuming there are at least 5 stones left in the pile). The person who removes the last stone(s) wins. Download nims template. and open it up. Check out the lines of text in between the sets of triple quotes (“””), underneath the definition of play nims. This is called a docstring, and is handy for documentation: plain English text informing users of your program what your program does and what parameters need to be given values. Also check out the text following the character #. These lines, called comments , are also documentation, usually strings of English a line or two long clarifying what the code directly below them does. In this case they have been used to enter in the outline of the program constructed from the discussion below. You’ll probably want to delete these as you write the program.

In this problem, you’ll write a function to play this game; the function has been outlined it for you. The outline helps you break the problem down into parts; this process is useful with most programming tasks. You do not have to follow the structure of this outline, but it has proved to be of use to most beginners. The outline suggests you use nested loops (one loop inside another). In the outermost loop, we want to keep playing until we are out of stones. Inside that, there is a loop that keeps prompting a user for a move until he supplies a valid move. Instead of using nested while loops, you could You have the option of either writing two blocks of code, or keeping a variable that tracks the current player. The second way could be slightly trickier, but it’s definitely do-able! The second way also allows you to do the (optional) addon complication part of the assignment: Announce which player has won at the end.

Finally, we might want to have an innermost loop that checks if the user’s input is valid. Is it a number? Is it a valid number (e.g. between 1 and 5)? Are there enough stones in the pile to take off this many? If any of these answers are no, we should tell the user and re-ask them the question. As always, feel free to ask the instructor for help on any part of this problem. If you choose to write two blocks of code, the basic outline of the program should be something like this:

while [pile is not empty]:
   while [player 1’s answer is not valid]:
      [ask player 1]
   [execute player 1’s move]
   [same as above for player 2]

Be careful with the validity checks. Specifically, we want to keep asking player 1 for their choice as long as their answer is not valid, BUT we want to make sure we ask them at least ONCE. So, for example, we will want to keep a variable that tracks whether their answer is valid, and set it to False initially.

One function you will find useful for this assignment is raw_input, for interaction with players (there’s some example code using raw_input in a while loop in Section Loops)

>>> X = raw_input('Do you want to continue?\n')
Do you want to continue?
>>> X

In the simulated interaction above, raw_input is executed, it prints out its string argument and then waits for a response from the user; the user can then type any answer they choose; the answer is assumed to be over when they hit [Enter]; raw_input then returns the string they typed. Note that on the first line, the variable X is set to that string. So raw_input allows a program to interact with a user, store their responses, and then condition future actions on those responses.

Note that what raw_input returns is a string. This is true even if a user types a number like 1:

>>> X = raw_input('Guess a number.\n')
Guess a number.
>>> X
>>> type(X)
<type 'str'>

If you want compare this to an integer, you should convert X to an integer first like this:

>>> X = int(X)

When you’re finished, test each other’s programs by playing them! Be sure to test that the game works right both when player 1 and player 2 win.

Optional add on Complication: Instead of having the program simply announce the game is over, have it announce who won (Player 1 or Player 2).

To get started on the nims_assignment, download the nims template.


This is adapted from MITs open courseware Python course.

Use of the materials in this package are governed by the same Creative Commons license as all other materials published on MIT OpenCourseWare. For more information, see ocw.mit.edu/terms