A classic game of chance.

Two-dice pig

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

Whenever you want to use more than one variable with the same name, such as dice1, dice2, you should consider if a list/array would be a better approach. It is more scalable which means the program can be extended without much if any additional code for new situations.

An array must already exist with the correct number of elements before you can refer to its indexes. E.g. dice = [0, 0] declares that dice is an array with two indexes, both assigned the number zero. That enables you to use the commands: dice[0] = and dice[1] = because the original data can now be over-written. Remember empty lists have no indexes.

Using modulus is a good way of looping a variable back to zero when it goes past a maximum value. E.g. x = x % 6 would prevent the variable x storing more than the number six.

Investigate

Questions to think about with this program to check your understanding:

Relation question

At the end of the player’s turn line 63 is executed: player = (player + 1) % 2. What does this achieve, and how does it relate to line 30: score = [0, 0]?

Reveal answer

If it is player 1’s turn it becomes player 2’s turn. If it is player 2’s turn it becomes player 1’s turn.

Player 1 is actually player 0 in the code, and player 2 is player 1 in the code.

score is an array storing the score for both players. You can use score[player] to change or output the score for any player without the need for additional selection statements to determine whose turn it is, and therefore which variable to change. This would be necessary if you used two variables to store the two scores instead. E.g.

if player == 1:
    player1_score = player1_score + total
else:
    player2_score = player2_score + total

You can see that it is much easier to write:

score[player] = score[player] + total

It also means you can increase the number of players without having to add any additional code. This means the program is more “scalable”.

Approach question

The condition in line 33 to determine the end of the game works for a two player game while score[0] <= 100 and score[1] <= 100:, but does not scale well if you wanted a 3-6 player game because the number of conditions would need to increase too. What is a better approach here?

Reveal answer

Use a Boolean variable to determine if the game has been won. E.g.

winner = -1 # Nobody has won the game.
while winner == -1:
    if score[player] >= 100:
        winner = player

This code is much more scalable because it allows any number of players without any additional code.

Make

Change the program so that:

  1. If the player rolls a double one their total is zero, they end their turn and their score is reset back to zero.
  2. It asks how many players from 1-4 and can be played by that many players.

Typical inputs and outputs from the program would be:

How many players? 1-4:3
 
------------------------------------
Player 1 it's your turn.
Your score is currently: 0
Press Enter to roll the dice.

You rolled a 5 and 4
You scored 9
Your total is 9
Do you want to continue y/n? :y
Press Enter to roll the dice.

You rolled a 3 and 3
You scored 6
Your total is 15
Do you want to continue y/n? :n
Press Enter to hand the dice to the next player.


------------------------------------
Player 2 it's your turn.
Your score is currently: 0
Press Enter to roll the dice.

You rolled a 5 and 5
You scored 10
Your total is 10
Do you want to continue y/n? :n
Press Enter to hand the dice to the next player.


------------------------------------
Player 3 it's your turn.
Your score is currently: 0
Press Enter to roll the dice.

You rolled a 1 and 2
Oh no, that's a pig out!
Press Enter to hand the dice to the next player.


------------------------------------
Player 1 it's your turn.
Your score is currently: 15
Press Enter to roll the dice.

You rolled a 4 and 1
Oh no, that's a pig out!
Press Enter to hand the dice to the next player.


------------------------------------
Player 2 it's your turn.
Your score is currently: 10
Press Enter to roll the dice.

You rolled a 4 and 5
You scored 9
Your total is 9
Do you want to continue y/n? :n
Press Enter to hand the dice to the next player.


------------------------------------
Player 3 it's your turn.
Your score is currently: 0
Press Enter to roll the dice.

You rolled a 5 and 6
You scored 11
Your total is 11
Do you want to continue y/n? :y
Press Enter to roll the dice.

You rolled a 6 and 5
You scored 11
Your total is 22
Do you want to continue y/n? :n
Press Enter to hand the dice to the next player.


------------------------------------
Player 1 it's your turn.
Your score is currently: 15
Press Enter to roll the dice.

You rolled a 1 and 1
Oh no, that's a double pig out, back to zero!
Press Enter to hand the dice to the next player.
🆘 If you're really stuck, use this Parsons code sorting exercise
roll, points
# Two-dice pig program

# -------------------------
# Import libraries
# -------------------------
import random


# -------------------------
# Subprograms
# -------------------------
# Function to return the outcome of the roll of two dice as an array
def roll():
---
    dice = [0, 0]
---
    dice[0] = random.randint(1, 6)
    dice[1] = random.randint(1, 6)
---
    return dice    
---

# Function to calculate the points scored from the dice roll
def points(dice):
---
    # Both dice lose all score
    if dice[0] == 1 and dice[1] == 1:
---
        return -1
---
    # One of the two dice lose running total
    elif dice[0] == 1 or dice[1] == 1:
---
        return 0
---
    # Sum of the dice is the score
    else:
---
        return dice[0] + dice[1]
play_game, main program
# Procedure to play the game
def play_game(number_of_players):
---
    random.seed()
    score = [0, 0, 0, 0]
    player = 0
    new_player = True
    winner = -1
---
    # While the winner is not a player number play the game
    while winner == -1:
---
        # Reset variables for the next player
---
        if new_player:
---
            print()
            print("------------------------------------")
            print("Player", player + 1, "it's your turn.")
            print("Your score is currently:", score[player])
            total = 0
            new_player = False
            turn_end = False
---
        print("Press Enter to roll the dice.")
        input()
        dice = roll()
---
        result = points(dice)
        print("You rolled a", dice[0], "and", dice[1])
---
        # Add the dice to the running total if not a pig out. 
        if result > 0:
---
            print("You scored", result)
            total = total + result
            print("Your total is", total)
---
            choice = ""
            # Get a valid input to gamble or bank
            while choice not in ["y", "n"]:
---
                choice = input("Do you want to continue y/n? :")
---
            # Banking adds the running total to the score
            if choice == "n":
---
                score[player] = score[player] + total
---
                # If the banked score is 100 or more the player is the winner
                if score[player] >= 100:
---
                    winner = player
---
                # If the player hasn't won end their turn
                else:
---
                    turn_end = True
---
        # A score of zero end the turn
        elif result == 0:
---
            print("Oh no, that's a pig out!")
            turn_end = True
---
        # A score of -1 loses all the score and ends the turn
        else:
---
            print("Oh no, that's a double pig out, back to zero!")
            score[player] = 0
            turn_end = True
---
        # If there is a winner announce it
        if winner > -1:
---
            print("Player", player + 1, "WINS!")
---
        # At the end of the turn change the player
        if turn_end:
---
            print("Press Enter to hand the dice to the next player.")
            input()
---
            player = (player + 1) % number_of_players
            new_player = True
---


# -------------------------
# Main program
# -------------------------
---
number_of_players = int(input("How many players? 1-4:"))
---
play_game(number_of_players)