yahtzee

package module
v0.0.0-...-6781cc5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2018 License: LGPL-3.0 Imports: 1 Imported by: 0

README

YAHTZEE!

A Go implementation of an optimal Yahtzee player on the Raspberry Pi. See more about this project on my blog, or play along online.

GoDoc Build Status Coverage Status

Installation

Score tables

You will need to build the score tables first, using the compute_scores tool:

$ go get github.com/timpalpant/yahtzee
$ cd cmd/compute_scores
$ go build

Build the expected value score tables:

$ ./compute_scores -logtostderr -observable expected_value -output expected-value.gob.gz

Build the high score tables:

$ ./compute_scores -logtostderr -observable score_distribution -output score-distribution.gob.gz

The expected value tables are 5.7 MB and the high score tables are 1.8 GB on disk.

Web server

Build and run the web server, passing the location of the score data tables:

$ go install github.com/timpalpant/yahtzee/cmd/yahtzee_server
$ yahtzee_server -logtostderr -port 8080 -expected_scores expected-scores.gob.gz -score_distributions score-distributions.gob.gz

The server will take a few minutes (and GB of RAM) to load the score tables at startup. Navigate to http://localhost:8080.

Image processing server

The image processing web service is written in Python. The easiest way to build and run it is with Docker:

$ cd image_processor
$ docker build -t yahtzee-image-processor:latest .
$ docker run -p 8080:8080 yahtzee-image-processor:latest

A pre-built image is also available at: docker.io/timpalpant/yahtzee-image-processor:latest.

Raspberry Pi Player

First install GoCV, as described here: https://gocv.io/getting-started/

Then build the RPi player with:

$ cd cmd/rpi
$ go build

and run with:

$ ./rpi -logtostderr -v 3 -yahtzee_uri http://localhost:8085 -image_processing_uri http://localhost.local:8080 -score_to_beat 300

It is recommended to run the yahtzee server and image processing service on a separate machine, since they require more memory and computational resources than available on the pi.

License

This package is released under the GNU Lesser General Public License, Version 3.0

Yahtzee is a registered trademark owned by Hasbro. Hasbro is not affiliated with this project.

Documentation

Index

Constants

View Source
const (
	MaxGame  = 64 << shiftUHS
	NumTurns = int(Yahtzee + 1)

	UpperHalfBonusThreshold = 63
	UpperHalfBonus          = 35
	YahtzeeBonus            = 100
	MaxScore                = 1500
)
View Source
const (
	NDice  = 5
	NSides = 6

	// The largest possible roll integer (+1). This can be used
	// to pre-allocate arrays indexed by roll.
	MaxRoll = (NDice << (bitsPerSide * (NSides - 1))) + 1
)

Variables

This section is empty.

Functions

func IsYahtzee

func IsYahtzee(roll Roll) bool

Types

type Box

type Box uint
const (
	Ones Box = iota
	Twos
	Threes
	Fours
	Fives
	Sixes
	ThreeOfAKind
	FourOfAKind
	FullHouse
	SmallStraight
	LargeStraight
	Chance
	Yahtzee
)

func (Box) IsUpperHalf

func (b Box) IsUpperHalf() bool

func (Box) Score

func (b Box) Score(roll Roll) int

BoxScore returns the score that would be received for playing the given roll in the given box.

Note it does not include any bonuses.

func (Box) String

func (b Box) String() string

type GameState

type GameState uint

Each distinct game is represented by an integer as follows:

  1. The lowest 13 bits represent whether a box has been filled. Bits 0-5 are the Upper half (ones, twos, ... sixes). Bits 6-12 are the Lower half (three of a kind ... yahtzee)
  2. Bit 13 represents whether you are eligible for the bonus, meaning that you have previously filled the Yahtzee for points. Therefore bit 13 can only be set if bit 12 is also set.
  3. Bits 14-19 represent the upper half score in the range [0, 63]. Since for all upper half scores >= 63 you get the upper half bonus, they are equivalent and the upper half score is capped at 63.

This means that all games are represented by an integer < 6.4mm (MaxGame).

func NewGame

func NewGame() GameState

func (GameState) AddUpperHalfScore

func (game GameState) AddUpperHalfScore(score int) GameState

func (GameState) AvailableBoxes

func (game GameState) AvailableBoxes() []Box

func (GameState) BonusEligible

func (game GameState) BonusEligible() bool

func (GameState) BoxFilled

func (game GameState) BoxFilled(b Box) bool

func (GameState) FillBox

func (game GameState) FillBox(box Box, roll Roll) (GameState, int)

func (GameState) GameOver

func (game GameState) GameOver() bool

func (GameState) IsValid

func (game GameState) IsValid() bool

func (GameState) SetBonusEligible

func (game GameState) SetBonusEligible() GameState

func (GameState) SetBoxFilled

func (game GameState) SetBoxFilled(box Box) GameState

func (GameState) String

func (game GameState) String() string

func (GameState) Turn

func (game GameState) Turn() int

func (GameState) TurnsRemaining

func (game GameState) TurnsRemaining() int

func (GameState) UpperHalfScore

func (game GameState) UpperHalfScore() int

type Roll

type Roll uint

Type Roll encodes an unordered roll of five dice as the concatenation of 6 octal integers. Each 3 bits represent the number of ones, the number of twos, etc.

func AllDistinctRolls

func AllDistinctRolls() []Roll

func NewRoll

func NewRoll() Roll

func NewRollFromBase10Counts

func NewRollFromBase10Counts(counts int) Roll

Construct a new roll from the given counts in base-10 (as opposed to the canonical representation in base-8).

func NewRollFromDice

func NewRollFromDice(dice []int) Roll

Construct a new Roll from the given dice.

func NewRollOfDie

func NewRollOfDie(die, count int) Roll

Construct a new Roll with count of the given die.

func (Roll) Add

func (r Roll) Add(die int) Roll

Return a new Roll constructed by adding the given die to this one.

func (Roll) CountOf

func (r Roll) CountOf(side int) int

CountOf returns the number of a particular side in this roll.

func (Roll) Counts

func (r Roll) Counts() []int

func (Roll) Dice

func (r Roll) Dice() []int

Dice unpacks this Roll into its natural representation as a list of sides.

func (Roll) HasNInARow

func (r Roll) HasNInARow(n int) bool

HasNInARow checks whether there is a sequence of N sides in a row.

func (Roll) HasNOfAKind

func (r Roll) HasNOfAKind(n int) bool

HasNOfAKind checks whether there are at least N of any side in this roll.

func (Roll) IsFullHouse

func (r Roll) IsFullHouse() bool

func (Roll) NumDice

func (r Roll) NumDice() int

NumDice returns the total number of dice in this roll.

func (Roll) One

func (r Roll) One() int

Return the side of one of the dice in this roll.

func (Roll) PossibleHolds

func (r Roll) PossibleHolds() []Roll

Return all possible distict kept subsets of this roll.

func (Roll) Probability

func (r Roll) Probability() float32

func (Roll) Remove

func (r Roll) Remove(die int) Roll

func (Roll) String

func (r Roll) String() string

func (Roll) SubsequentRolls

func (r Roll) SubsequentRolls() []Roll

Return all possible subsequent rolls starting from this one. If this roll contains two dice, it will return all possible combinations of these two with three others rolled. The returned rolls will always contain NDice.

func (Roll) SumOfDice

func (r Roll) SumOfDice() int

SumOfDice returns the sum of the sides of all dice.

type TurnStep

type TurnStep int
const (
	Begin TurnStep = iota
	Hold1
	Hold2
	FillBox
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL