Initial commit

This commit is contained in:
nate 2025-05-20 00:21:33 +00:00
commit 76245a405e
9 changed files with 325 additions and 0 deletions

28
.commit_template Normal file
View 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
View File

@ -0,0 +1 @@
**/__pycache__/*

33
dice_stats.py Normal file
View 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
View 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
View 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()

56
poetry.lock generated Normal file
View File

@ -0,0 +1,56 @@
# This file is automatically @generated by Poetry 2.0.0 and should not be changed by hand.
[[package]]
name = "click"
version = "8.1.8"
description = "Composable command line interface toolkit"
optional = false
python-versions = ">=3.7"
groups = ["main"]
files = [
{file = "click-8.1.8-py3-none-any.whl", hash = "sha256:63c132bbbed01578a06712a2d1f497bb62d9c1c0d329b7903a866228027263b2"},
{file = "click-8.1.8.tar.gz", hash = "sha256:ed53c9d8990d83c2a27deae68e4ee337473f6330c040a31d4225c9574d16096a"},
]
[package.dependencies]
colorama = {version = "*", markers = "platform_system == \"Windows\""}
[[package]]
name = "colorama"
version = "0.4.6"
description = "Cross-platform colored terminal text."
optional = false
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
groups = ["main"]
markers = "platform_system == \"Windows\""
files = [
{file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
]
[[package]]
name = "tqdm"
version = "4.67.1"
description = "Fast, Extensible Progress Meter"
optional = false
python-versions = ">=3.7"
groups = ["main"]
files = [
{file = "tqdm-4.67.1-py3-none-any.whl", hash = "sha256:26445eca388f82e72884e0d580d5464cd801a3ea01e63e5601bdff9ba6a48de2"},
{file = "tqdm-4.67.1.tar.gz", hash = "sha256:f8aef9c52c08c13a65f30ea34f4e5aac3fd1a34959879d7e59e63027286627f2"},
]
[package.dependencies]
colorama = {version = "*", markers = "platform_system == \"Windows\""}
[package.extras]
dev = ["nbval", "pytest (>=6)", "pytest-asyncio (>=0.24)", "pytest-cov", "pytest-timeout"]
discord = ["requests"]
notebook = ["ipywidgets (>=6)"]
slack = ["slack-sdk"]
telegram = ["requests"]
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<4.0"
content-hash = "bb89287de0cafdf48a8eea6717121a79981ba4c7811aca416921cceb4f89f67b"

22
pyproject.toml Normal file
View File

@ -0,0 +1,22 @@
[project]
name = "lab-dice"
version = "0.1.0"
description = ""
authors = [
{name = "Chris Proctor",email = "chris@chrisproctor.net"}
]
license = {text = "MIT"}
readme = "README.md"
requires-python = ">=3.10,<4.0"
dependencies = [
"click (>=8.1.8,<9.0.0)",
"tqdm (>=4.67.1,<5.0.0)"
]
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
[tool.poetry]
package-mode = false

111
yahtzee.py Normal file
View 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
View 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