# Leveraging probabilities and quadratics over random chance in strategy games

So, I’ve decided that this spring, after wrapping up the side projects I’ve picked up over the last year, I’m going to start work on a Civ-like strategy game. (See…this is what happens when you deliver a sub-par product on a marquee franchise. Who has two thumbs and thinks he can do better?) It likely won’t look as nice as most of the recent iterations of Civ, since I plan to use the browser as the UI and web-based backend for game management and rules enforcement, but graphical beauty isn’t the ultimate point of turn based strategy…now is it? I’ve always wanted to take a crack at game development, and solid modern javascript implementations along with innovations in CSS has made the browser a viable option recently as a games platform.

In any case, I plan to use this new category on the blog, of which this is the first post, to wrangle some free-range thoughts before I’m able to begin in earnest. I want to first explore some thoughts on the difference between complete random chance vs an exponential sliding scale of probabilities to model game events that have a variable degree of being triggered.

For purposes of example, I’m going to take a relatively simple mechanic from the Civ series: research and the tech tree.

#### Tech in the Civilization series

In all iterations of the game, your empire produces a certain amount of scientific research each turn, measured in ‘beakers’ and depends on the current state of your empire and how you have your cities configured. We’ll refer to one unit of science as [B] for ‘beakers’ from here on out.

Traditionally, this is a pretty non-random game mechanic. You produce a certain amount of [B] per turn, and it takes a fixed amount of [B] to fully research a tech. It’s a pretty simple mechanic if left at that, but Civ games typically have not, leaving this mechanic slightly unbalanced with additions like tech trading and research agreements, where techs can be bought, traded for each other, or two nations can agree to spend gold to share their next discoveries with each other. Buffs to science production from city improvement are only applied directly to the amount of [B] being produced, whether in specific numbers or percentages, simply making it go faster at a predictable rate.

What I’d like to do to modify this is introduce a non-linear mechanic for determining when a tech has been researched. Using a parabola to instead model the chances that your eggheads will experience a ‘breakthrough’ on the techs that you’ve set them out to research, we can create a more interesting (and true to life, in a way) mechanic where the longer your researchers spend on a tech, the greater the probability that they’ll master the subject they’re researching, as opposed to simply collecting a specific number of beakers towards a hard-set arbitrary finish line.

#### A quick mathematics refresher

I had to drag myself through this earlier, but it was worth it, promise.

A probability is typically expressed as a value between zero and one; zero meaning that there is no chance of an event happening and 1 meaning that it absolutely will happen. So, the probability that a coin flipped landing heads is 0.5. This is particularly useful to model weighted random events within computer programs, as it’s relatively easy to ask the computer to generate a random decimal value between 0 and 1.

A parabola is the result generally of an expression where y is a number (m) taken to the power of another (n) or m^n. For example: y = x^2 (or y equals x squared). This should bring back some memories of high school math class:

The full form of this is referred to as a quadratic equation, and takes the following form:

*y = ax^2 + bx + c*

The key things we need to keep in mind for this discussion are twofold. First, we’ll be manipulating the exponent (the ^2 part) in this equation a bit. The closer to 1 that the exponent is, the gentler the curve will be – at 1, the curve flattens out completely. Second, the value of *c* shifts the entire curve up if it’s positive, or down if it’s negative.

#### Applying it - the basics

For the purposes of using a quadratic to model a changing probabilty, we’re really only concerned with the square area between 0 and 1 in both the x and y directions.

Think of the x-axis as the percentage of the [B] we have against the [B] value of the tech we’re researching, expressed as a decimal. So, if the tech we’re after has a [B] value of 100 to research, and we have 40 [B], we’d have x = 0.4. This is essentially modeling the ‘effort’ our civilization is putting towards a technology.

Think of the y-axis as the probability that we will finish researching the tech at this point in our research. If we’re using y = x^2 as our model, the probability that we’d finish the tech this turn after reaching 40 [B] would be (0.4)^2, which comes out to about 0.16 or a 16% chance.

At 80 [B], our probability of getting that tech is (0.8)^2 which comes out to 0.64 or a 64% chance.

At 90 [B], our probability of getting that tech is (0.9)^2 which comes out to 0.81 or an 81% chance.

The neat thing about using a quadratic function, as these are called, to determine the probability of whether we get a new tech on each turn or not, is that we’re introducing an element of carefully controlled chance to this mechanic. The earlier we are in the course of researching a tech, the less likely we are to get a ‘breakthrough’ with it, although there’s always a very slim chance. The more we invest into research on a tech, the closer we get to the full amount of [B] needed, and the chances that we’ll pop the tech early gets progressively larger and larger.

#### Depressing the curve - stacking the odds towards later

Ok, say we don’t want such a smooth progressive increase in the probability that we land a new tech. At [y = x^2], we’ve better odds than a coin flip at around 70% through the [B] value of any given tech. Say we’d rather have pretty minimal odds over the normal course of research, and have it spike up at the end.

Well, here’s where some basic mathematics works in our favor. 1 multiplied by itself any number of times will always result in 1; thus we can use any exponent we wish in this equation, and we’ll always end up with a probability of 1 at 100% researched. (It’s also possible for us to move that around a bit, but more on that later - let’s stick to the severity of the curve for now.)

The higher the exponent is, the more the curve will dip down towards (0,1) before arching up to hit (1,1), meaning that it will take more scientific effort over a longer period of time before the probability side reaches a point where you might reasonably pop a tech ahead of time. For instance, here’s our probability curve with an exponent of 10 instead of 2:

This also applies in the opposite direction - if we actually want to approach higher probabilities more rapidly and have them pretty high for most of the curve, we instead go with a fractional exponent. This will bow the curve out in the opposite direction, towards the point (0,1):

#### Never reaching 100% certainty

Now we’re getting into the practical points of applying this concept to the notion of long-term scientific research. It’s common that years of research and effort towards a new application of science doesn’t bear fruit, and while finite arbitrary ‘number of beakers’ are needed in order to balance a research mechanic within a game, it’s typically far from the truth that a research team will come upon a breakthrough once they’ve done *x* amount of work, no more and no less. Instead, it’s more realistic to define the breakthrough as being more likely to happen the longer and more concentrated the efforts are, but you’re never guaranteed that eureka moment at any time.

Here’s where the *c* part of *y = ax^2 + bx + c *comes in. If we define *c* to be slightly negative, we can bring a portion of the curve into negative y territory, essentially defining those points as completely improbable for purposes of our calculations. For example, here’s a curve with an exponent of 5, and a *c* of -0.1:

In this model, we don’t have any chance of popping a tech advancement until well after we’re halfway through the full expected effort denoted by [B]. Also, we never hit a full 1.0 probability that we’re going to pop a tech, if we’re remaining within the constraints of 0 to 1.0 effort, taking anything greater than full [B] for the tech to be 1.0 on the x-axis.

#### Wrapup

Using this technique, a research mechanic can be created that properly replicates the ‘it’s in the last place you look’ phenomenon of scientific research - sometimes a project takes half the time to produce results, sometimes it takes double the time. By using a system of weighted probabilities instead of a linear system of research with completely random events either moving that linear result along more or less quickly, you produce a system that can be tweaked into giving just the right amount of statistical variance to feel slightly random but still predictable.

This also allows an expanding of the research dynamic in that we can now open the option up to players to divide their research efforts towards multiple technologies, and have it be a worthwhile trade-off. Since there’s always a chance that you can pop any tech that you’ve put any effort towards, dividing your tech efforts is a matter of hedging your bets; you can put the majority of your efforts towards a single tech that you want to prioritize, but you could divert 10% of your efforts towards some other techs in order to try and get a lucky breakthrough on very little effort.

If the exact probability curve being used can be shielded from direct view, instead giving players an ‘estimated time of arrival’ in turns before a tech arrives, you can allow other game mechanics such as foreign espionage to have a negative effect on a player’s research, or write in positive buffs to all player’s scientific progress as the game gets later to simulate the progressively changing nature that information is shared over long periods of time…all without having to provide literal and finite penalties or bonuses to effort (beakers) to achieve it. It can instead be done by tweaking the equations that govern the probability that a tech will be completely researched.