0% found this document useful (0 votes)
24 views4 pages

Project 115

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Project 115

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sticks/Nim game

Preamble:

Nim is a game in which there are (usually) 2 players with a bundle of objects between them. I
was introduced to this game as “sticks” because whenever little 5 year old me would play, our
bundle of objects was always sticks from the backyard. There can be any number of objects in
the middle, and each player takes turns taking a number of objects from the middle. The player
who takes the last stick (or other object) loses.

Step 0, trivialities:

Create a new folder for this assignment (if you have a Comp115 folder already, create this new
folder inside of your Comp115 folder). Name the folder something like [first initial][last
name]_Project1, for example, I would name mine Nalmeder_project1.

Create a python file, name it [first initial][last name]_sticks. For example, I would name mine
Nalmeder_sticks. The ‘.py’ extension should be automatically put on your file, but in the event
that it isn’t, add the .py

You should also create a README.txt file. Here you will write which steps you completed as
well as any other information you think I should know about your file(s).

Step 1, pick up sticks:

At the very top of your program on line one, write your preferred name and last name in a
comment. I would write
# Nicholas Almeder

For this assignment, I will make the minimum number of sticks 10, and a given player can
choose to take 1, 2, or 3 sticks from the pile on their turn.

So, somewhere below your name, create an int, number_of_sticks. This variable should be
assigned the value of 10.

The program should prompt the user to input a number, and then the program should output
how many sticks are left after the user took that many sticks.

Sanity check: if a user takes 2 sticks, the program should output that there are 8 sticks
remaining.
Step 2, taking more than one turn:

So far, we have a terrible calculator that can only tell us the answer to 10 - x. Use a while loop to
allow the player to take more sticks from the pile.

Sanity check: if a user takes 2 sticks, the program should output that there are 8 sticks
remaining. If the player then takes 1 stick, the program should output that there are 7 sticks
remaining. If the player then takes 3 sticks, the program should output that there are 4 sticks
remaining. And so on.

Step 2.5, no cheating:

Now we need to prevent you (and your future opponent) from cheating. Adjust the code so that
any number above 3, below 1, or any input that is not a number is not accepted. However, make
sure this does not cause an error, crashing the program. The user should be able to type 0, 4,
abc, etc, and be prompted once again to pick a number 1 through 3.

Now you can hotseat the game if you’d like, meaning 2 players play using the same keyboard.
When you’re done taking your turn, have the other player take theirs, and so on.

Step 3, make it a ‘real game’:

So we have turns that can be repeatedly taken, but we don’t have another player or a way to
win. Edit the program so that when the user takes the final stick, they lose, exiting the while loop
and having the program end.

Now, we need a computer opponent. First we need random numbers to be generated. At the top
of your file (line 2, right under your name), write:

import random

To get a random number from 1 to 3, including 3, use

random.randrange(2)

Specifically what this will do is generate a random number from 0 to 2. To get our number to be
between 1 and 3, we simply add 1 to the result.

random.randrange(2) + 1

Now, adjust the code so that every time the user inputs a number, the computer opponent will
take a number of sticks right after.

Congrats, you’ve technically just programmed AI.


Step 3.5, clean it up:

At this point, If it doesn’t already, you should make sure all the outputs in your program tell the
user what is going on, there are sticks in a pile, type 1, 2, to 3 to pick up 1,2, or 3 sticks, and if
the user inputs something besides 1, 2, or 3, tell them it is an illegal input and to try again. Etc.

Also, at this point, create an int player_turn in your program. If the value is 1, it is your turn, if the
value is 2, it is the opponent's turn. This way, you can keep track of who wins, allowing you to
output the winner at the end of the game. Make sure to change whose turn it is after a number
of sticks have been removed from the pile.

Step 4, make it dynamic:

Short games are no fun, let’s make it longer and more interesting. Begin each game prompting
the user to choose how many sticks should go in the pile, and what the maximum number of
sticks that can be taken in one turn is. Make the possible moves become taking 1 stick, 2 sticks,
… max_sticks sticks from the pile.

Step 5 (optional), make the computer impossible to beat:

This game, with the rules as written, is subject to being exploited. To demonstrate this, create a
duplicate of your sticks file (or make a brand new Python file and copy-paste the code,
whichever makes life easier on you and your IDE). Name this one something like [first
initial][Last Name]_sticks_hardmode. I would name mine Nalmeder_sticks_hardmode.

There is a way to pretty much immediately win the game in most situations. There are some
exceptions, so for this part I recommend testing with the original constraints, 10 sticks in the
pile, 1, 2, or 3 sticks per move.

Think about it, write out stuff, play with numbers on paper, and make the computer unbeatable
using these rules.

Hint: Have the computer go first, not the player, if you haven’t already done so.
Submitting to Canvas:
When you are ready to submit, zip your folder (if you don’t know how to do that, ask me or you
may look it up) and submit the zipped folder to Canvas. The zipped folder should contain your
README.txt, sticks.py, and if you attempted it, your sticks hardmode file.

Grading:
This assignment is graded out of 100 points, any amount over 100 becomes extra credit

No submission: 0pts
Submitted step 0, with a non trivial attempt to get code working: 50pts
Submitted a completed step 1: 60 pts
Submitted a completed step 2: 70 pts
Step 2.5: 75 pts
Step 3: 85 pts
Step 3.5: 90 pts
Step 4: 100 pts
Step 5 / hardmode: 110 pts

You might also like