Python 3 Create SVG Gameboard 9 Mens Morris

9 Men’s Morris is one of my favorite games. It’s simple enough to play against most 5 year olds, but complex enough to challenge most adults. You can print out this game board created with python and see how to create SVG files in Python.

This python script is a demo of using python to create a simple board game. Similar code to this is used for converting SVG to GCode for my CNC router I didn’t have the SVG I wanted, so this was written in Perl. Then I realized nobody except me likes Perl! So I wrote it in Python.

The game, 9 men’s morris, is very old, and has been found carved in stones in Egyptian pyramids.

Installation

You probably already know how to setup a python script. However, for the class we were teaching, we’ve included the basics that an absolute beginner can follow:

  • install python 3 https://www.python.org/downloads/
  • add it to path, install pip, add to path
  • pip install svgwrite
  • pip install json
  • save the python script and the json file in same directory

Topics Learned

  • python module svgwrite
  • python pip installing modules
  • python comments
  • python multiline comments
  • python escaping characters
  • python dictionary data structure
  • python dictionary of lists structure
  • python for loops
  • python splitting strings
  • python converting string to float
  • python functions
  • python passing variables
  • python basic math operator
  • SVG Basics
  • SVG File Creation
  • SVG Text
  • SVG Circles
  • SVG Squares
  • SVG Lines/Paths
  • SVG Styles
  • JSON Basics
  • JSON loading json file
  • JSON writing json file
  • JSON indenting json file
  • 9 Men’s Morris Rules

The JSON File:

We create a JSON file to store dots and lines in. This would likely be easier as data in the script, but for the class we were teaching, we wanted a JSON demo. As we had about 40 games using various lines and configs, this seemed like a good use of JSON.

Source Code for 9 Mens’ Morris in Python

Keep in mind, this only makes the “game board”. But it does demonstrate how to use Python to create SVG files.

Download the SVG for 9 Men’s Morris

You may want to download the actual svg for 9 men’s morris created by this script.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.