Three Dice
Stop three spinning dice with Space, matches multiply your points. Teaches random numbers, reading a key, conditions and a function.
Open in Tokbit Level: Expert. Free in the browser, nothing to install, no account needed.
Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.
Flowchart
Python program
The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.
"""Three dice spin and Space stops them. A match multiplies the points, no match halves them. Teaches a drawing function."""
from random import randint
# Tokbit screen functions, not in plain Python:
from tokbit import (
cls, at, color, background, border, key, sleep, beep, note, WHITE, BLUE,
YELLOW, LIGHT_RED, LIGHT_GREEN, LIGHT_BLUE,
)
def draw(position: int, value: int):
"""Draws a die at position 0 to 2 with value 1 to 6 from text lines."""
top: list[str] = [" ", " o ", " o ", " o o ", " o o ", " o o "]
middle: list[str] = [" o ", " ", " o ", " ", " o ", " o o "]
bottom: list[str] = [" ", " o ", " o ", " o o ", " o o ", " o o "]
x: int = 5 + position * 11
at(x, 7)
print("+-------+", end="")
at(x, 8)
print("|" + top[value - 1] + "|", end="")
at(x, 9)
print("|" + middle[value - 1] + "|", end="")
at(x, 10)
print("|" + bottom[value - 1] + "|", end="")
at(x, 11)
print("+-------+", end="")
def header(attempt: int, points: int):
"""Title, round number, points so far and the Space hint."""
cls()
color(WHITE)
at(2, 1)
print("THREE DICE", end="")
at(2, 3)
print("Try", attempt, "of 5", " ", "Points:", points, end="")
color(LIGHT_BLUE)
at(2, 5)
print("Space (Action) stops the next die.", end="")
def roll(dice: list[int]):
"""The dice spin and Space stops the next one; ends when all three stand still."""
stopped: int = 0
# Round loop: the dice spin until all three are stopped.
while stopped < 3:
color(YELLOW)
for i in range(stopped, 3):
dice[i] = randint(1, 6)
draw(i, dice[i])
sleep(80)
if key() == " ":
color(WHITE)
draw(stopped, dice[stopped])
beep(880, 40)
stopped = stopped + 1
def evaluate(dice: list[int], points: int) -> int:
"""Returns the points after a round: three alike times 3, two times 2, otherwise half."""
if dice[0] == dice[1] and dice[1] == dice[2]:
points = points * 3
color(LIGHT_GREEN)
at(2, 14)
print("Three of a kind! Points times 3.", end="")
note("C5", 120)
note("E5", 120)
note("G5", 120)
note("C6", 360)
else:
if dice[0] == dice[1] or dice[1] == dice[2] or dice[0] == dice[2]:
points = points * 2
color(YELLOW)
at(2, 14)
print("A pair! Points times 2.", end="")
else:
points = points // 2
color(LIGHT_RED)
at(2, 14)
print("No match, points halved.", end="")
return points
def end_round(attempt: int, points: int):
"""Prints the points and waits for Space before the next round."""
color(WHITE)
at(2, 16)
print("Points:", points, end="")
if attempt < 5:
at(2, 18)
print("Space: next try", end="")
else:
at(2, 18)
print("Space: result", end="")
k: str = ""
while k != " ":
k = key()
sleep(50)
def prize_for(points: int) -> str:
"""The prize for the points at the end of the game."""
prize: str = ""
if points >= 3000:
prize = "a TV set"
else:
if points >= 1000:
prize = "a radio"
else:
if points >= 400:
prize = "a watch"
else:
if points >= 100:
prize = "an umbrella"
else:
prize = "slippers"
return prize
def game_over(points: int):
"""End of the game: the final points and the prize."""
cls()
color(WHITE)
at(2, 8)
print("GAME OVER", end="")
at(2, 10)
print("Points:", points, end="")
color(YELLOW)
at(2, 12)
print("You win:", prize_for(points), end="")
at(0, 15)
background(BLUE)
border(LIGHT_BLUE)
points: int = 100
dice: list[int] = [1, 1, 1]
# Game loop: five rounds, each with a spin, scoring and points.
for attempt in range(1, 6):
header(attempt, points)
roll(dice)
points = evaluate(dice, points)
end_round(attempt, points)
game_over(points)