• Paul Wennekes

Settlers of Catan in R, part 2

Last time we used R to define and randomize the underlying data for a game of Catan. This time we'll focus on the visual representation of the game board, for which we will be heavily relying on the Magick package. If you want to follow along with the code yourself, feel free to get all the necessary image files (made publicly available by Ryan Schenk) from my GitHub page here: There, you will also be able to see the most up to date version of the scripts, as well suggest improvements or make other contributions!

First of all, let's make sure that the appropriate packages are installed and loaded:

Let's load Ryan Schenk's Catan land tiles. If you're following along: by loading different images here you can use this script to randomly generate Catan maps with your own design. Ryan's tiles are oriented differently to how I need them to be, so I'm going to rotate the files by 30 degrees, then I'll fill the background transparantly and trim the edges. Note: if you find that the images you're using do not come out looking the way you expect, experimenting with the "fuzz" value will solve a lot of problems here - this value is essentially the forcefulness with which the fill and trim functions apply to slightly different colours, and different values will be appropriate for different source files.

Each of the land tiles (save the desert) will have a number on it to indicate which dice-rolls will generate resources from that tile. These tiles are pretty simple so we can make them in R by resizing, recolouring and annotating circles. I will make sure to add the little dots that indicate how many dice-roll combination there are for each number, and colour the sixes and eights differently. I will also add transparant canvas to the tiles so that when aligned with the land-type tiles the circle ends up nicely in the middle. Finally I will make sure that I fill the remaining bits of the number-tiles with transparency so the mosaic will be free of artefacts:

Next, We can generate each of the land tiles by making mosaics for each combination of terrain type and number. This is quite a lot of code, but very repetitive so not hard to do with a bit of excel / textedit find and replace magic. There are probably more elegant ways to do this using functions but for now I just want a working model, and I will worry about optimisation later:

Just because the board will otherwise be unbearably large, I'm also quickly going to resize the tiles so we will have a more manageable image size:

It isn't necessary for the script to run, but it can be helpful to output the files whenever you make alterations to them to check whether things have gone according to plan. Here is an example tile, a sheep tile with the number 8 on it:

Looks good! We have all the land tiles, now we have to make the sea and harbour tiles. Let's start with the harbour-type indicators. We will need six types, one for each resource. The procedure is pretty similar to that of making the number tiles: we have to extend the canvas to make sure that the mosaic is properly aligned, annotate the appropriate ratio (2:1 or 3:1 depending on the harbour type), and colour in the circles Then we just have to make sure that the whole tile except for the actual circle is transparant so that we can overlay it onto the harbour tiles to generate each of the possible combinations.

One thing I'd like to do is to make differen versions for each of the orientations, this not only makes sense in order to indicate the locations from where players will have acces to the harbours, but also just looks better for the regular sea tiles. We can do this by simply rotating the same starting tiles by 30, 90, 150, 210, 270 and 330 degrees. Again, we'll also fill the background with transparency and trim the edges off the tiles.

Now, all that remains is to make a mosaic for each of the combinations of harbour-orientation and harbour-type, then make the background transparant again. We've done this before with the land-tiles, so we can repeat the method here:

Finally, I will output the objects to actual images. This way, when it comes to generating a board I can just load the pre-made images instead of having to generate them from scratch every time I want to set up a board:

If you're just interested in the random generator part of the code, all you need is the code starting from here, and the images from the "All_tiles" folder on

We already have the algorithm to randomly draw the tiles and add numbers to them from last time, but I'll include it here for completeness:

Now, we'll make a composite image which will be our map. The width of the map needs to be 7 tiles, whereas the height of the map needs to be 5.5 times the height of the tiles, since hexagons can be seen as 6 equilateral triangles, meaning that the offset of one piece to a piece below it is 0.75 times the tile height. So we can use these distances as the locations of the tiles by expanding on what we already did before for making the mosaic images of the terrain tiles with numbers: we extend the canvas for each of the tiles which we have randomly generated in the previous section.

We can use the same methodology for randomly drawing the harbours, and then generating the tiles on their transparent canvases:

Now all that remains to be done is to generate the map, by creating a mosaic of the background with the first tile, then the next, etc. Then finally we can create a .gif image by simply listing the image objects

Let's see if it worked:

Hooray! That will be it for today. I'd love it if you had a look at the GitHub repository for this project, and feel free to suggest any improvements! Here is the link: Thank you for reading, and see you next time!

1,324 views0 comments

Recent Posts

See All

Settlers of Catan is one of my all-time favourite board games. Players build towns and cities, collect and trade resources to expand their settlements and race to be be the first to get 10 victory poi