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?
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										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 Yachtzee
 | 
			
		||||
from yahtzee_goals import (
 | 
			
		||||
    GoalOnes, 
 | 
			
		||||
    GoalTwos, 
 | 
			
		||||
    GoalThrees,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
goals = [
 | 
			
		||||
    GoalOnes(), 
 | 
			
		||||
    GoalTwos(),
 | 
			
		||||
    GoalThrees(),
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
game = Yachtzee(goals)
 | 
			
		||||
game.play()
 | 
			
		||||
							
								
								
									
										51
									
								
								poetry.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								poetry.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
# This file is automatically @generated by Poetry 1.5.1 and should not be changed by hand.
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "click"
 | 
			
		||||
version = "8.1.6"
 | 
			
		||||
description = "Composable command line interface toolkit"
 | 
			
		||||
optional = false
 | 
			
		||||
python-versions = ">=3.7"
 | 
			
		||||
files = [
 | 
			
		||||
    {file = "click-8.1.6-py3-none-any.whl", hash = "sha256:fa244bb30b3b5ee2cae3da8f55c9e5e0c0e86093306301fb418eb9dc40fbded5"},
 | 
			
		||||
    {file = "click-8.1.6.tar.gz", hash = "sha256:48ee849951919527a045bfe3bf7baa8a959c423134e1a5b98c05c20ba75a1cbd"},
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[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"
 | 
			
		||||
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.66.1"
 | 
			
		||||
description = "Fast, Extensible Progress Meter"
 | 
			
		||||
optional = false
 | 
			
		||||
python-versions = ">=3.7"
 | 
			
		||||
files = [
 | 
			
		||||
    {file = "tqdm-4.66.1-py3-none-any.whl", hash = "sha256:d302b3c5b53d47bce91fea46679d9c3c6508cf6332229aa1e7d8653723793386"},
 | 
			
		||||
    {file = "tqdm-4.66.1.tar.gz", hash = "sha256:d88e651f9db8d8551a62556d3cff9e3034274ca5d66e93197cf2490e2dcb69c7"},
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[package.dependencies]
 | 
			
		||||
colorama = {version = "*", markers = "platform_system == \"Windows\""}
 | 
			
		||||
 | 
			
		||||
[package.extras]
 | 
			
		||||
dev = ["pytest (>=6)", "pytest-cov", "pytest-timeout", "pytest-xdist"]
 | 
			
		||||
notebook = ["ipywidgets (>=6)"]
 | 
			
		||||
slack = ["slack-sdk"]
 | 
			
		||||
telegram = ["requests"]
 | 
			
		||||
 | 
			
		||||
[metadata]
 | 
			
		||||
lock-version = "2.0"
 | 
			
		||||
python-versions = "^3.11"
 | 
			
		||||
content-hash = "e78479d542c88ba0a220d425c59162a0f8c7ecfef5c42eb296d1431c0ab05b87"
 | 
			
		||||
							
								
								
									
										17
									
								
								pyproject.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								pyproject.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
[tool.poetry]
 | 
			
		||||
name = "lab-dice"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
description = ""
 | 
			
		||||
authors = ["Chris Proctor <chris@chrisproctor.net>"]
 | 
			
		||||
readme = "README.md"
 | 
			
		||||
packages = [{include = "lab_dice"}]
 | 
			
		||||
 | 
			
		||||
[tool.poetry.dependencies]
 | 
			
		||||
python = "^3.11"
 | 
			
		||||
click = "^8.1.6"
 | 
			
		||||
tqdm = "^4.66.1"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
[build-system]
 | 
			
		||||
requires = ["poetry-core"]
 | 
			
		||||
build-backend = "poetry.core.masonry.api"
 | 
			
		||||
							
								
								
									
										108
									
								
								yahtzee.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								yahtzee.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,108 @@
 | 
			
		||||
from die import Die
 | 
			
		||||
 | 
			
		||||
class Yachtzee:
 | 
			
		||||
    """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!")
 | 
			
		||||
        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
 | 
			
		||||
							
								
								
									
										45
									
								
								yahtzee_goals.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								yahtzee_goals.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
 | 
			
		||||
class GoalOnes:
 | 
			
		||||
    "One point for each one"
 | 
			
		||||
    used = False
 | 
			
		||||
 | 
			
		||||
    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"
 | 
			
		||||
    used = False
 | 
			
		||||
 | 
			
		||||
    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"
 | 
			
		||||
    used = False
 | 
			
		||||
 | 
			
		||||
    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