generated from mwc/lab_dice
Initial commit
This commit is contained in:
28
.commit_template
Normal file
28
.commit_template
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Write your entire commit message above this line.
|
||||
#
|
||||
# The first line should be a quick description of what you changed.
|
||||
# Then leave a blank line.
|
||||
# Then, taking as many lines as you want, answer the questions
|
||||
# corresponding to your checkpoint.
|
||||
#
|
||||
# Checkpoint 1:
|
||||
# - Describe a problem you could solve, or a situation you could simulate,
|
||||
# using classes.
|
||||
#
|
||||
# Checkpoint 2:
|
||||
# - This checkpoint asked you to write docstrings explaining code. When you
|
||||
# were writing docstrings, how was your thinking different from your thinking
|
||||
# when you are writing code?
|
||||
# - When you write code in the future for your own projects, do you think you will
|
||||
# include docstrings?
|
||||
#
|
||||
# Checkpoint 3:
|
||||
# - Solving problems by writing classes to represent parts of the problem is called
|
||||
# Object-Oriented Programming, or OOP for short. Describe your experience with
|
||||
# OOP while working on Yahtzee. How is this style of problem-solving different
|
||||
# from the way you might have written a Yahtzee program in Units 1 or 2?
|
||||
|
||||
|
||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
**/__pycache__/*
|
||||
33
dice_stats.py
Normal file
33
dice_stats.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from die import Die
|
||||
from tqdm import tqdm
|
||||
|
||||
class FiveDice:
|
||||
def __init__(self):
|
||||
self.dice = [Die() for number in range(5)]
|
||||
|
||||
def roll(self):
|
||||
for die in self.dice:
|
||||
die.roll()
|
||||
return self.faces()
|
||||
|
||||
def faces(self):
|
||||
return [die.face for die in self.dice]
|
||||
|
||||
def all_ones(self):
|
||||
for face in self.faces():
|
||||
if face != 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
dice = FiveDice()
|
||||
successes = 0
|
||||
trials = 1000000
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.all_ones():
|
||||
successes += 1
|
||||
|
||||
print(successes/trials)
|
||||
|
||||
|
||||
|
||||
17
die.py
Normal file
17
die.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# die.py
|
||||
# ------
|
||||
# By MWC Contributors
|
||||
# Implments a Die class.
|
||||
|
||||
from random import randint
|
||||
|
||||
class Die:
|
||||
def __init__(self):
|
||||
self.roll()
|
||||
|
||||
def __str__(self):
|
||||
return str(self.face)
|
||||
|
||||
def roll(self):
|
||||
self.face = randint(1, 6)
|
||||
return self.face
|
||||
15
play.py
Normal file
15
play.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from yahtzee import Yahtzee
|
||||
from yahtzee_goals import (
|
||||
GoalOnes,
|
||||
GoalTwos,
|
||||
GoalThrees,
|
||||
)
|
||||
|
||||
goals = [
|
||||
GoalOnes(),
|
||||
GoalTwos(),
|
||||
GoalThrees(),
|
||||
]
|
||||
|
||||
game = Yahtzee(goals)
|
||||
game.play()
|
||||
19
pyproject.toml
Normal file
19
pyproject.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[project]
|
||||
name = "lab-dice"
|
||||
version = "5.0.0"
|
||||
description = ""
|
||||
authors = [{ name = "Chris Proctor", email = "chris@chrisproctor.net" }]
|
||||
requires-python = ">=3.10,<4.0"
|
||||
readme = "README.md"
|
||||
license = { text = "MIT" }
|
||||
dependencies = [
|
||||
"click (>=8.1.8,<9.0.0)",
|
||||
"tqdm (>=4.67.1,<5.0.0)",
|
||||
]
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[tool.uv]
|
||||
package = false
|
||||
54
uv.lock
generated
Normal file
54
uv.lock
generated
Normal file
@@ -0,0 +1,54 @@
|
||||
version = 1
|
||||
requires-python = ">=3.10, <4.0"
|
||||
resolution-markers = [
|
||||
"platform_system == 'Windows'",
|
||||
"platform_system != 'Windows'",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "click"
|
||||
version = "8.1.8"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "colorama", marker = "platform_system == 'Windows'" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/b9/2e/0090cbf739cee7d23781ad4b89a9894a41538e4fcf4c31dcdd705b78eb8b/click-8.1.8.tar.gz", hash = "sha256:ed53c9d8990d83c2a27deae68e4ee337473f6330c040a31d4225c9574d16096a", size = 226593 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/7e/d4/7ebdbd03970677812aac39c869717059dbb71a4cfc033ca6e5221787892c/click-8.1.8-py3-none-any.whl", hash = "sha256:63c132bbbed01578a06712a2d1f497bb62d9c1c0d329b7903a866228027263b2", size = 98188 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "colorama"
|
||||
version = "0.4.6"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", size = 27697 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lab-dice"
|
||||
version = "5.0.0"
|
||||
source = { virtual = "." }
|
||||
dependencies = [
|
||||
{ name = "click" },
|
||||
{ name = "tqdm" },
|
||||
]
|
||||
|
||||
[package.metadata]
|
||||
requires-dist = [
|
||||
{ name = "click", specifier = ">=8.1.8,<9.0.0" },
|
||||
{ name = "tqdm", specifier = ">=4.67.1,<5.0.0" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tqdm"
|
||||
version = "4.67.1"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "colorama", marker = "platform_system == 'Windows'" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/a8/4b/29b4ef32e036bb34e4ab51796dd745cdba7ed47ad142a9f4a1eb8e0c744d/tqdm-4.67.1.tar.gz", hash = "sha256:f8aef9c52c08c13a65f30ea34f4e5aac3fd1a34959879d7e59e63027286627f2", size = 169737 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/d0/30/dc54f88dd4a2b5dc8a0279bdd7270e735851848b762aeb1c1184ed1f6b14/tqdm-4.67.1-py3-none-any.whl", hash = "sha256:26445eca388f82e72884e0d580d5464cd801a3ea01e63e5601bdff9ba6a48de2", size = 78540 },
|
||||
]
|
||||
111
yahtzee.py
Normal file
111
yahtzee.py
Normal file
@@ -0,0 +1,111 @@
|
||||
from die import Die
|
||||
|
||||
class Yahtzee:
|
||||
"""A command-line Yahtzee game.
|
||||
This version of Yahtzee is initialized with a list of goals.
|
||||
"""
|
||||
def __init__(self, goals):
|
||||
self.score = 0
|
||||
self.goals = goals
|
||||
self.dice = [Die() for num in range(5)]
|
||||
|
||||
def play(self):
|
||||
print("Welcome to Yachtzee!")
|
||||
self.score = 0
|
||||
for goal in self.goals:
|
||||
goal.used = False
|
||||
while self.count_unused_goals() > 0:
|
||||
self.play_round()
|
||||
print(f"Your final score was {self.score}")
|
||||
|
||||
def play_round(self):
|
||||
print("=" * 80)
|
||||
self.rolls_left = 3
|
||||
for die in self.dice:
|
||||
die.roll()
|
||||
self.show_status()
|
||||
goal = self.choose_goal()
|
||||
goal.used = True
|
||||
self.score += goal.score(self.dice)
|
||||
|
||||
def show_status(self):
|
||||
dice = ', '.join([str(die) for die in self.dice])
|
||||
print(f"Score: {self.score}. Rolls left: {self.rolls_left}. Dice: {dice}.")
|
||||
|
||||
def choose_goal(self):
|
||||
options = []
|
||||
unused_goals = self.get_unused_goals()
|
||||
for goal in unused_goals:
|
||||
option = goal.prompt(self.dice)
|
||||
options.append(option)
|
||||
if self.rolls_left > 0:
|
||||
options.append("Re-roll")
|
||||
choice = self.get_choice(options)
|
||||
if options[choice] == "Re-roll":
|
||||
self.reroll()
|
||||
self.show_status()
|
||||
return self.choose_goal()
|
||||
else:
|
||||
return unused_goals[choice]
|
||||
|
||||
def get_choice(self, options):
|
||||
print("What would you like to do?")
|
||||
for i, option in enumerate(options):
|
||||
print(f"{i}. {option}")
|
||||
choice = input("> ")
|
||||
while not self.option_choice_is_valid(choice, options):
|
||||
print("Sorry, that's not a valid choice.")
|
||||
choice = input("> ")
|
||||
return int(choice)
|
||||
|
||||
def option_choice_is_valid(self, choice, options):
|
||||
if not choice.isdigit():
|
||||
return False
|
||||
if int(choice) < 0:
|
||||
return False
|
||||
if int(choice) >= len(options):
|
||||
return False
|
||||
return True
|
||||
|
||||
def count_unused_goals(self):
|
||||
return len(self.get_unused_goals())
|
||||
|
||||
def get_unused_goals(self):
|
||||
unused_goals = []
|
||||
for goal in self.goals:
|
||||
if not goal.used:
|
||||
unused_goals.append(goal)
|
||||
return unused_goals
|
||||
|
||||
def reroll(self):
|
||||
self.rolls_left -= 1
|
||||
choices = self.get_reroll_choices()
|
||||
dice_to_reroll = self.get_dice_to_reroll(choices)
|
||||
for die in dice_to_reroll:
|
||||
die.roll()
|
||||
|
||||
def get_dice_to_reroll(self, choice_ints):
|
||||
dice_to_reroll = []
|
||||
for die in self.dice:
|
||||
if die.face in choice_ints:
|
||||
choice_ints.remove(die.face)
|
||||
dice_to_reroll.append(die)
|
||||
return dice_to_reroll
|
||||
|
||||
def get_reroll_choices(self):
|
||||
print("Which dice do you want to re-roll?")
|
||||
choices = input("> ")
|
||||
while not self.reroll_choices_are_valid(choices):
|
||||
print("Please enter the numbers on dice you want to re-roll.")
|
||||
choices = input("> ")
|
||||
choice_ints = [int(digit) for digit in choices]
|
||||
return choice_ints
|
||||
|
||||
def reroll_choices_are_valid(self, choices_str):
|
||||
if not choices_str.isdigit():
|
||||
return False
|
||||
choice_ints = [int(digit) for digit in choices_str]
|
||||
for die in self.dice:
|
||||
if die.face in choice_ints:
|
||||
choice_ints.remove(die.face)
|
||||
return len(choice_ints) == 0
|
||||
42
yahtzee_goals.py
Normal file
42
yahtzee_goals.py
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
class GoalOnes:
|
||||
"One point for each one"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Ones ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
total = 0
|
||||
for die in dice:
|
||||
if die.face == 1:
|
||||
total += 1
|
||||
return total
|
||||
|
||||
class GoalTwos:
|
||||
"Two points for each two"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Twos ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
total = 0
|
||||
for die in dice:
|
||||
if die.face == 2:
|
||||
total += 2
|
||||
return total
|
||||
|
||||
class GoalThrees:
|
||||
"Three points for each three"
|
||||
|
||||
def prompt(self, dice):
|
||||
potential_score = self.score(dice)
|
||||
return f"Threes ({potential_score})"
|
||||
|
||||
def score(self, dice):
|
||||
total = 0
|
||||
for die in dice:
|
||||
if die.face == 3:
|
||||
total += 3
|
||||
return total
|
||||
Reference in New Issue
Block a user