Skip to content

A Web App to Calculate Combat Rolls in Twilight Imperium

Every few weeks my son, Lucas, gets together with some of his friends for board game day. This is a day-long event where they play some board game that I’ve never heard of. Last week’s game was Twilight Imperium (4th edition, I was told to mention). I don’t know much about this game, except that it takes a long time to play (6+ hours) and is some sort of space-themed game that I imagine is like Risk but significantly more complex.

Anyway, one part of the game is that you have to roll a 10-sided die to calculate whether one of your ships successfully registers a hit on your opponent. Apparently this is something that can take a while to do as each player amasses more and more ships. Lucas realized this was ripe for automation and the day before the event brought his iPad over to show me a Numbers spreadsheet he had made that used random numbers to calculate combat rolls. Essentially you enter the number of ships at each combat level and does the appropriate number of rolls and tells you how many hits you registered.

It was a pretty clever bit of work, but not really appropriate for a spreadsheet. If you’ve even done consulting, you’re probably aware of the adage, “Excel is not a database”, because there is often a lot of work you can get converting creaky, error-prone Excel spreadsheets into full-featured apps. They same thing sort of applied here and I told Lucas that this was an impressive bit of work, but might be better suited as a Xojo app.

We then sat down together with my laptop and quickly put together a Xojo desktop app to calculate combat rolls. We decided to go with a Container that has controls to show the combat level, a field to enter the number of ships at that level and rolls results to verify accuracy.

Apparently, a roll counts as a hit if it matches or exceed the ship’s combat level. So a ship at level 10 only gets a hit if you roll a 10, but a ship at level 2 gets a hit if you roll a 2 or higher. We accomplished this with two methods:

Public Function Calculate() As Integer
  Var ships As Integer = ShipCountField.Text.ToInteger
  
  Var hits As Integer
  Rolls.RemoveAll
  
  For i As Integer = 1 To ships
    If DidItHit Then
      hits = hits + 1
    End If
  Next i
  
  DiceRollsLabel.Text = String.FromArray(Rolls, ",")
  
  Return hits
End Function
Private Function DidItHit() As Boolean
  Var diceRoll As Integer
  diceRoll = System.Random.InRange(1, 10)
  
  Rolls.Add(diceRoll.ToString)
  
  If diceRoll >= CombatLevel Then
    Return True
  Else
    Return False
  End If
End Function

Once we had it working, we realized a desktop app was not going to be useful because he was only going to bring his iPad to board game day. Unfortunately, being a school iPad, we cannot install apps on it so making an iOS app was out. Plus, other kids would want to use the app as well. Fortunately Xojo has lots of options. We decided to convert the desktop app to a web app instead, which would work in Safari on his iPad.

I quickly re-created the UI in a web project, copied over the code and it worked right away. I had him do some testing and when we were confident everything looked good, I set up a port forward on my eero so that he could access the web app when outside our wifi network.

Twilight Imperium Combat Roll Calculator

All together, start to finish, this took us maybe an hour to do.

He told me they used it the entire day and it worked great. And the results? Lucas and another player tied for the win.

He did also offer a suggestion to add buttons to increase/decrease the number of ships as it was a bit tedious to type the numbers using the iPad. I’ll probably have him do that.

Download the project, if you’re interested.