Summary of the game

Pig is a simple game of luck and gamble, where two players take turns to roll a dice. On a player’s go, they add the number on the dice to their score for that go. If they land on 1, their score for that go is set to 0 and it becomes the other player’s turn. Before each roll, the player can decide if they want to “stick” or roll again. If they choose to stick, their score for that go gets added to their overall score. The aim of the game is to have a higher score than your opponent at the end of n rounds.


I broke this game down into 3 parts.

p1 = 0
p2 = 0

rounds = 10

playing = True
draw = False
winner = "NONE"

# Mainloop

for r in range(1, rounds + 1):
    print("-----Round %d-----" % r)
    print("Player 1 score: %i" % p1)
    print("Player 2 score: %i" % p2)

    p1 += move("p1")
    p2 += move("p2")

if p1 > p2:
    winner = "p1"

elif p2 > p1:
    winner = "p2"
    draw = True

if draw:
    print("Player 1 score: %i" % p1)
    print("Player 2 score: %i" % p2)

    print(f"----{winner} WINS-----")
    print("Player 1 score: %i" % p1)
    print("Player 2 score: %i" % p2)

I broke this game down into 3 parts.

Part 1

Part one is the structure of the game. This includes the main loop, adding to the players’ overall scores and printing the winner at the end.

def move(player):
    rolling = True
    die = 0
    round_score = 0
    rolls = 0

    # User
    if player == "p1":
        print("\n---YOUR GO---\n")
        while rolling:
            op = input("Do you want to roll (r) or stick (s)?")

            if op.lower().strip() == "r":
                die = random.randint(1, 6)  # Creating the random number
                round_score += die
                print("\nThe dice landed on %i!" % die)

                rolls += 1

                if die == 1 and rolls != 1:
                    print("0 points for this round!\n")
                    return 0

                print("Total for this round: %d" % round_score, "\n")

                print("You got %d points for this round!" % round_score, "\n")
                return round_score

Part 2

Part 2 is where the player inputs their decisions into the game. You can choose to roll (r) or stick (s).

        # Computer

        print("\n---COMPUTER'S GO---\n")

        while rolling:
            die = random.randint(1, 6)
            round_score += die
            rolls += 1

            print("The computer landed on %i" % die)

            if die == 1 and rolls != 1:  # Cannot get out on the first go
                print("0 points for this round!\n")
                return 0

            print("The computer's total is %i" % round_score, "\n")

            v = (round_score / rolls) / 6  # Getting the value

            prob = (rolls ** (v * 4)) / 50  # Getting the probability

            if random.random() < prob:
                print("The computer has decided to stick\n")
                return round_score


Part 3

Part 3 is the complicated bit. It is where the computer decides what it wants to do. It uses a formula to calculate a probability on whether it will stick or not.

Making the formula

I mostly used trial and error to make this formula, as I have no idea what numbers change what so I just messed around with it until it looked good. A website that really helped was This is a really great website that plots a line representing a formula that you can specify and change. It also has “sliders” so you can quickly change values and see the result.


To make this formula I started breaking down the game to decide on some simple rules you can follow to maximise your score. At the end of the day this game is luck however you can make certain decisions that increase your point gains. Firstly:

  • The more, high numbers you are getting, the higher the chances that you are going to stick and not gamble

For example, if you roll three 6s in a row, that is really lucky so you are not going to want to waste that. Inversely:

  • The more, lower numbers you are getting, the more likely you are going to want to carry on and try to get higher scores

This is a general trend however sometimes if you are getting unlucky, you might just want to stick with what you have and not risk the few points you do have.

To resemble this this certain degree of luck, I made a variable called “V”, or “Value”. This variable is a simple percentage (shown as 0 to 1), that shows how lucky your series of rolls are. For example, 6 is absolute maximum value so that will give you a 1, further rolls will change this value for example if you roll a 3 that will bring you down do v = 0.75. This is how I calculated this:


  • “round_score” = the total points that player has for that series of rolls
  • “rolls” = the amount of rolls the player has done in that round

    \[V = {round\_score \div rolls \over 6}\]

This gives you the average score, as a number between 1 and 0. This is only one piece of the puzzle however, as this does not take into account the general rule of:

  • The more rolls you take, the more likely you are going to stick

So, I needed to use this value and bring in the number of rolls into account. I started messing around on the quadratic line plotter. I used a slider and the variable “v” to test how the graph changes as the value changes. This is the formula I got:

    \[y = {x^{v*4} \over 50}\]

These numbers make a line that can be used to give the computer a probability of sticking. This is much more accurate and rewarding than linearly changing the probability.

This is how it works:

The number of rolls are shown on the x axis along the bottom and the probability is derived from the y. The computer picks a random number from 0 to 1. If it is below the y value produced by this graph, the computer sticks. So the more space under the line between 1 and 0 show the higher the probability of sticking. Of course, if the line goes above 1, the computer will always stick as there is a 100% chance of it being under the line.

When you move the slider for v, the line responds.

At v = 0.6, the slope’s gradient becomes more gradual. This allows for the computer to take more rolls.

At v = 0.4, the gradient is very shallow so the computer will roll more.


This was the first formula that I looked at and said “that looks alright”, so I wasn’t expecting much. I reckoned it was a bit too subtle and not daring enough to be accurate to human decision. I definitely expected to go through a couple more generations before I got it right. So, as you might imagine, I was very surprised when the computer beat me on my first game. It then went onto beat my friend on it’s second game, both times it looked like we were going to win, but then the computer comes back in the final few rounds. We then went on to beat it three times in a row, proving that it was not completely unbeatable. At home it beat my dad, in the same late game clutch as it had done twice before.

I have got to admit I was very impressed with my program! However, there is a lot that can be improved. This game is based almost solely on luck, so I think I had got the probabilities just good enough so that luck could tip it over the edge for the win. There is still a lot more room for improvement however, as the computer did make some questionable decisions here and there.

Final round problem

There was a situation in one of the games the computer lost. It was the final round, and the computer was about 10 points behind the player. It was the computers go. First roll: 3… And it stuck. Admittedly, there is a pretty low chance of the computer sticking after just rolling 3, but it does flag up a problem in the logic. In order for the computer to win it must have rolled a score which meant it beat the opponent. So, what a human would do is think, “I might as well keep rolling until it means I can beat the opponent as the only other option is a loss”. This is something that I can program in at a later date.

Other methods

While I was making the formula, I thought of some other methods of helping the computer decide whether to stick or to roll. On of these methods was the “target number” method, where, the computer would roll until it got to a good amount, say 14, and then stick, this would ensure that the computer always tries to get a good sum of points. Also, there should be a way of fining the “sweet spot”. This would be a number with the highest probability to size ratio. Put simply, the biggest number the computer has a good probability of getting.

Taking this method further, you could add in an element of competitiveness. Perhaps, tell the computer that it always has to beat the what the opponent got for their turn. This could backfire however because the player could get a very low number; there’d be no point stooping to that goes’ level when there’s valuable points to be getting earnt to put the computer ahead of its competition.

The correct way to play Pig

Hiding behind this seemingly simple game of luck, there is a single, mathematically correct way of playing the game, a way that puts you at the absolute best chances of winning the game. This, is what I intend to find out. There are two ways I see, of finding this optimum method. One, you could do it completely mathematically, using probability and calculations to find the best method. Secondly, the much more fun method, is letting the computer itself decide what the best way of playing the game. I would need to find a way of the computer playing pig with itself over and over again, thousands of times a second, a way of measuring reward – so it knows what went well and what did not go so well, and a way of recording and somehow condensing the data the computer gathers so it can use that data to improve its own strategies.


I expect the computer to come up with some seemingly bizarre “best way” of playing the game, that at first seems completely wrong to humans. For example, “roll 4 every single time” or something strange like that. I think the computer will come up with something like this (that does not look right to humans) because the computer has no bias or misleading instincts that may give humans a disadvantage. The computer understands no aspect of “luck” or getting “unlucky”. All it sees is a probability. And will not be influenced by the feeling of “aww I’m getting so unlucky, lets stick now”. Fundamentally this is what will allow the computer to find a better algorithm than a human, the best algorithm.