Skip to content

Instantly share code, notes, and snippets.

@ghluka
Last active January 14, 2025 00:26
Show Gist options
  • Save ghluka/0250bc2615c0a1d3154f88f4c4544e4d to your computer and use it in GitHub Desktop.
Save ghluka/0250bc2615c0a1d3154f88f4c4544e4d to your computer and use it in GitHub Desktop.
Marble Draw game simulation for MDM4U1 summative | Mathematics of Data Managment @ RCI
"""Marble Draw simulation for the first section of my data management's culminating assignment
"""
import random
class MarbleDraw:
"""
A simulation of a Marble Draw game.
Game rules:
- There are 15 marbles in a bag: 7 red and 8 blue.
- Draw 4 marbles without replacement, costing 10 points per draw.
- The bag is reset after each draw.
- Points are awarded based on the number of red marbles drawn:
0 red: 30 points
1 red: 10 points
2 red: 0 points
3 red: 20 points
4 red: 50 points
"""
BAG = ["red"] * 7 + ["blue"] * 8
DRAW_COST = 10
REWARDS = {0: 30, 1: 10, 3: 20, 4: 50}
def __init__(self):
"""Initializes the game with default points and totals."""
self.points = 1000
self.total_red = 0
self.total_black = 0
self.draws = []
def draw(self) -> None:
"""Draws 4 marbles from a new bag, adds to the totals and the points accordingly."""
# Draw 4 marbles
bag = self.BAG[:]
random.shuffle(bag)
self.draws.append([bag.pop() for _ in range(4)])
red_count = self.draws[-1].count("red")
# Update totals
self.total_red += red_count
self.total_black += 4 - red_count
# Update points
self.points -= self.DRAW_COST
self.points += self.REWARDS.get(red_count, 0)
def main() -> None:
"""Runs 100 Marble Draw simulations and outputs the outcomes."""
marble_draw = MarbleDraw()
for _ in range(100):
marble_draw.draw()
#frequencies = {}
#for i in marble_draw.draws:
# frequencies[i.count("red")] = frequencies.get(i.count("red"), 0) + 1
#print(frequencies)
print(
"Welcome\n"
f"You have {marble_draw.points} points.\n\n"
"Total Occurrence Frequency\n"
f"Red {marble_draw.total_red}\n"
f"Black {marble_draw.total_black}"
)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment