A classic game of chance.
Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
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.
Questions to think about with this program to check your understanding:
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]`?
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".
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?
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.
Change the program so that:
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.
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Play the game with 1-3 friends to check your program has met the success criteria.