The 21 Game with Python Turtle Graphics

Here’s something fun.

The 21 Game is a two-player counting game where each player can add 1, 2, or 3, starting at zero, to the total. the total must not exceed 21 and the player who lands on 21 loses.

Python Console 21 Game

You can play the game against the computer using this trinket – just click the run button to start.

Python Code Listing for the 21 Game

It is often a great idea to run Python code using an installed version of Python on your computer rather than in a browser. One reason is that some browser-based implementations have subtly different behaviors to a “real” Python installation, and another is that sooner or later if you are serious about Python programming, you will need to work with a full version that allows installation of packages and access to your file system. So here is the full code listing for Python 3.

Strategy for the 21 Game

The computer opponent in the program above is not using any particular strategy, so with a bit of clever thinking, you may be able to find a strategy that allows you to win all or most of the time, perhaps depending on who starts. One way to explore this is with the help of a visual representation. With Python, one of my favourite tools for creating visual representations is the Turtle Graphics Module.

You can run the code in a browser using the embedded frame below. I have use insted of to embed the code as has better support for some of the Turtle Graphics methods I use in the code.

The code allows you to toggle the color of the squares in the representation of the game so you can see how you might win. If you need a hint on the strategy, think what might constitute a “safe square.”

Here is the full code listing. You can use the buttons at the top to copy the code if if you need to.

Hopefully the comments will help to understand how this code works, but there are couple of things to point out.

  • The click callback (the function that is called when a click event is detected on a turtle) needs an additional parameter so it can be attached to a particular turtle. This had me stumped for a while, but then I found this link which explains how to do it. If you don’t know, the Turtle Module is build on Tkinter, so solving Turtle-related problems can involve thinking about what’s going on with Tkinter.

  • To save having to wait for the drawing to take place, I have used screen.tracer(0) then screen.tracer(1) to re-enable animation once the drawing is complete.

The “extra arguments” trick means that creating this code from scratch is more of an intermediate exercise than one for beginners, but as a demo of what’s possible with Python Turtle Graphics as well as a useful way to help you become an expert at the 21 Game, I think it’s pretty neat.

I hope you enjoy playing this game and exploring the Python code it uses.

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"}