Skip to main content

yahtzee

A Yahtzee-solving python package and command line tool.

The algorithm is mathematically guaranteed to have the best strategy. That is, it maximizes expected score for a single game of Yahtzee.

The command line tool (play_yahtzee) guides you through a game and advises you on the best moves to make.

The algorithm assumes "vanilla" rules. It attains an average score of ~230 points.

Installation

Clone the repository:

$ git clone git@github.com:dpmerrell/yahtzee.git

Make sure you have python >= 3.8. I recommend using a virtual environment:

$ source my_py38_env

Install via pip:

(my_py38_env)$ cd yahtzee
(my_py38_env)$ pip install .

Congratulations! The yahtzee package and play_yahtzee command line tool are now installed.

Command line tool

After installation, you can run the interactive command line tool play_yahtzee:

(my_py38_env)$ play_yahtzee

###### STARTING GAME ######

    Roll all of your dice!
    Enter all of your dice here:
    

The prompts will lead you through a game of Yahtzee and advise you on the best moves to make.

Python package

The yahtzee python package has the following components:

  • A dynamic programming algorithm that solves the whole game (solve_game, solve_turn)
  • Precomputed results for that algorithm (PRECOMP_STATE_VALUES) (you can compute those results for yourself, but it will take hours)
  • An object-oriented interface for the algorithm to interact with a game of Yahtzee (OptimalPlayer, InteractiveGame)
  • A command line script based on that interface (play_interactive_game())

The source code is small -- look through it for details.

Algorithmic ideas

Yahtzee is a Markov Decision Process. It has convenient properties that make it straightforward to solve.

The solver uses dynamic programming to iterate through every possible state and action in a game of Yahtzee. For each state, the algorithm computes the maximal expected score (subject to the game's randomness), and the corresponding action. The algorithm stores these expected scores and optimal actions in a pair of tables. The table of actions is called the policy.

It turns out Yahtzee's state space has an interesting structure that makes it economical to

  1. Store a cache of expected scores for a very small number (2^13 = 8192) of states.
  2. At play time, recompute a small portion of the entire game's policy at the beginning of each turn.

This design strikes a balance between memory and computation. A very small number of expected values are stored, but some computation is required during the game.

(In a fully automated setting where speed matters, it may be better to just store the entire policy.)

This software is distributed under an MIT license, see LICENSE.txt.

The name "Yahtzee" may or may not be trademarked. I don't know, Hasbro can get in touch with me if they have problems with what I'm doing

Metadata

Release files for yahtzee-solve 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yahtzee-solve 0.0.1
File Size Uploaded
yahtzee_solve-0.0.1.tar.gz 100.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for yahtzee-solve 0.0.1
File Interpreter ABI Platform
yahtzee_solve-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 192.9 kB

Release files / yahtzee_solve-0.0.1.tar.gz

Download URL yahtzee_solve-0.0.1.tar.gz
Size 100.3 kB
Tags Source
SHA-256 checksum
How to use checksums
50d3325b865d684bff9d8a8ad06a2aa9331350fc322d40a1e6364e6ccef177d6
BLAKE2b-256 checksum
How to use checksums
14bc4874e68d421d5f308044210a58a1905ece7b1d73a7d9667abc2dfe58ae42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

Release files / yahtzee_solve-0.0.1-py3-none-any.whl

Download URL yahtzee_solve-0.0.1-py3-none-any.whl
Size 92.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d7b5ece140d8c92d25150b0602541efd681c98d6f0b13951bfafbee0acfc8e5
BLAKE2b-256 checksum
How to use checksums
abc24aa1e85753b98dcd4ca582dbc1fe7722105927b819ae871ffd505187a6e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page