Python Strategy Game - Take the Last Stone

Here’s a fun game. Play it first with a friend if you can. Start with a number between 10 and 20. Take it in turns to subtract either one or two from the number. The player to take the last stone wins. Make sure you both get to go first.

Next explore how this works using an actual pile of stones/pebbles, or if you don’t have any, anything handy where there are multiple identical items. I ended up using drawing pins!

Is there a winning strategy? Does it depend on who starts? If there is a winning strategy, what is it? Are there some situations where you can’t win if your opponent uses a particular strategy?

Spend some time thinking about this and doing experiments. Maybe write your results on some paper and see if there is a pattern.

It is definitely worth spending some time thinking and experimenting with this game. I have written the answer below and you can reveal it by clicking “show solution”. If you give up too early though you will deny yourself the opportunity to strengthen your thinking skills and also to feel the satisfaction that comes when you figure out these kinds of things for yourself.

Python Program for Last Stone Strategy Game

Below you will find a code listing for a Python program that simulates playing this game against an opponent.

When learning to program with Python or any other programming language, you should definitely actually type in code examples for yourself in most situations. Doing so will provide a much more effective learning experience than simply copy/pasting code and running it. There are several reasons for this, which I won’t go into now, but take my word for it and type in the provided code.

The code may or may not be a little above your level of ability to understand fully. If so, you should just type it in anyway. You will find that by doing so, some understanding will seep in “by osmosis”, and then when you have run the code a few times and come back to look at it, you will likely find it makes sense a lot more sense than it did to begin with.

The program is written using functions, and these are a programming construct that you definitely need to master if you intend to take programming at all seriously, so this is a good place to get on board. The functions are named in a fairly self-explanatory way as are the variables in the program (this is a very important habit to get into from the start – don’t use names like num and val in your programs for example, as they don’t provide enough information to anyone who reads you code, including you when you come back to it after a while…)

Here is the Python code listing for the Last Stone Game

Here’s a few point about the code:

  • The title is printed using ASCII art – you can click the link to learn more.
  • There are 2 levels. Level 1 uses a random choice to determine the opponent move, whereas level 2 uses the strategy.
  • The START_NUM constant can be used to change the initial number of stones.
  • There is a nested while loop to allow repeat play.
  • Don’t worry about global. It just makes current_number available throughout the program. Context is everything: Global Variables Will Break the Internet.

I hope you found that interesting and useful. If you would like to learn Python programming from me, why not get in touch and we can discuss you learning needs.

Happy Computing!

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Get the code for the classic Retro Alien Blitz Game using Python Turtle Graphics when you join our mailing list