## Friday, February 17, 2017

### Programmatically Defining Terra Mystica's Bowls Of Power

I would like to start this article off by acknowledging the vast audience this information will appeal to, so let’s begin by talking about Set collections.

Let’s say we have two Sets: one showing all the people in the universe that are interested in Swift Programming and the other showing all the people in the universe that are interested in European board games.  In Swift these would be written as such:

Now imagine there are thousands and thousands of entries in both of those Sets.  Lets run the intersect method on them to create a new Set that will show us how many people belong to both groups!

Okay, great.  Now let’s take a look inside the new Set we just made and see how big our potential audience is!

Oh, hey Tim. I didn’t know you and I had so much in common.

ANYWAYS, lets get to the topic at hand, The Bowls Of Power mechanism in one of my favorite board games of all-time, Terra Mystica.

The Bowls Of Power are an oft-maligned aspect of the Terra Mystica experience, decried by many as unthematic or fiddly.  This article will do absolutely nothing to dispel those criticisms and instead focus solely on mapping out the mechanism programmatically in the Swift Programming language.  Why, I hear you ask?  Well, I love board games and am interested in designing them, and I also seek to continually strengthen my Swiftiness in every way I can.  I thought this might be a fun way to dig really deeply into game mechanics and also write some code that provides some unique challenges.  Will I come out the other end a better game designer and programmer?  Or am I just wasting my time?  Only time will tell.  Let’s get to it.

In Terra Mystica, The Bowls Of Power represent your clan’s spell-casting ability or magical strength.  A player has three Bowls Of Power that each start with a different amount of Power Tokens inside them.  Let’s define a Power Token as an Integer and our Bowls Of Power as an Array of Integer Arrays:

As you can see, Bowl #1 starts with 5 Power Tokens, Bowl #2 starts with 7, and Bowl #3 with 0.  I arbitrarily decided that Power Tokens are Integers equal to 1, but it doesn’t really matter because we’re going to be manipulating the amount of elements in the arrays, not the elements themselves.  They could be Integers, Strings, I could make a class or struct called PowerToken, it doesn’t really matter.  Please tell me why I’m stupid in the comments.

Anywho, a little bit more about Power Tokens.  Power Tokens are what you use to cast your spells in Terra Mystica, BUT they can only be used if they are inside Bowl #3.  “But Bowl #3 has no tokens in it!” I hear Tim Allen screaming at his computer monitor.  Ah, that’s where gaining power comes into play.  Every time you gain Power in the game, you move your Power Tokens around according to certain rules.  Let’s show it in a function.

The first thing this function does is set a variable equal to the amount of power to be gained.  Then, the function checks to see if there are any Power Tokens in Bowl #1.  If there are it moves them into Bowl #2.  Once Bowl #1 is empty, it will only then start to move tokens from Bowl #2 to Bowl #3 where they are available for use.  It will continue shuffling the Power Tokens around between arrays until there’s no more power to move around.

Makes sense?  Great.  Let’s check out what happens when you SPEND Power Tokens.

As you can see, the function checks to make sure there are enough Power Tokens in Bowl #3 before moving them back to Bowl #1, where they will available for future gainPower function calls.

But what if you want to cast a spell but you don’t have enough power in Bowl #3?  I gotchu bro.  As long as you have a large amount of Power Tokens in Bowl #2, you will likely be able to cast the spell all the same.  BUT there are some serious circumstances to this.  Let’s take a look.

We are only given the option to Sacrifice Power to cast a spell if we don’t have enough Power Tokens in Bowl #3 to do it already.  Then, we figure out how much Power we need from Bowl #2 and how much Power that’s still coming from Bowl #3 in order to cast the spell.  After that, we have to be sure that there are at least twice as many tokens in Bowl #2 than we need for the spell.  Why?  Because every time you cast a spell using a Power Token from Bowl #2, you must PERMANENTLY sacrifice one of your Power Tokens for the rest of the game.  The logic written below then shuffles around the Power Tokens into the appropriate bowls.

Great!  Our Power Bowels are good to go.  Let’s see them in action.

Remember, to start off we have 5 Power Tokens in Bowl #1 and 7 Power Tokens Bowl #2.  Bowl #3 is empty.  Also remember that when we look into our Bowls, the Power Tokens are going to be represented with the Integer 1 (because I said so, k?).  So let’s look at how these Function calls exactly are manipulating our Bowls.

So there we have it.  The Bowls Of Power Mechanism from Terra Mystica defined in the Swift Programming language developed by Apple Inc in 2014 year of our lord .  All of their required functionality is included as these are the only three actions that ever need to be taken on the bowls in the game.  I thought about making the sacrificePower function get called within the spendPower function depending on if the appropriate conditions are met (and the other way around), but I already screen-capped everything so deal with it.

Until next mechanism, bozos!