alphabet

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxAlphabetSize is the maximum size of the alphabet, and is also
	// the "code" for the separation token.
	// It should be below 64 so that it can fit in one 64-bit word.
	// Gwich'in has 62 separate letters, including the blank.
	// Lojban has even more, but that's a weird constructed language.
	MaxAlphabetSize = 50
	// SeparationMachineLetter is the "MachineLetter" corresponding to
	// the separation token. It is set at the max alphabet size.
	SeparationMachineLetter = MaxAlphabetSize
	// BlankMachineLetter is the MachineLetter corresponding to the
	// blank. It is also set at the max alphabet size. Based on the context
	// in which it is used, it should not be confused with the
	// SeparationMachineLetter above.
	BlankMachineLetter = MaxAlphabetSize
	// BlankOffset is the offset at which letters with a code >= offset
	// represent blanks.
	BlankOffset = 100
	// SeparationToken is the GADDAG separation token.
	SeparationToken = '^'
	// EmptySquareMarker is a MachineLetter representation of an empty square
	EmptySquareMarker = MaxAlphabetSize + 1
	// PlayedThroughMarker is a MachineLetter representation of a filled-in square
	// that was played through.
	PlayedThroughMarker = MaxAlphabetSize + 2
	// ASCIIPlayedThrough is a somewhat user-friendly representation of a
	// played-through letter, used mostly for debug purposes.
	// Note that in order to actually be visible on a computer screen, we
	// should use `(`and `)` around letters already on a board.
	ASCIIPlayedThrough = '.'
	// BlankToken is the user-friendly representation of a blank.
	BlankToken = '?'
)

Variables

View Source
var CacheKeyPrefix = "letterdist:"

Functions

func CacheLoadFunc added in v0.4.5

func CacheLoadFunc(cfg *config.Config, key string) (interface{}, error)

CacheLoadFunc is the function that loads an object into the global cache.

func CacheReadFunc added in v0.4.5

func CacheReadFunc(data []byte) (interface{}, error)

CacheReadFunc converts raw data when populating the global cache

func HashableToUserVisible added in v0.2.11

func HashableToUserVisible(s string, alph *Alphabet) string

HashableToUserVisible converts a non-printable representation of a machine word to a printable one. The machine word is not required as an argument, just the non-printable string.

func Set added in v0.4.5

func Set(name string, data []byte) error

Set loads an alphabet from bytes and populates the cache

func ToMachineOnlyString

func ToMachineOnlyString(word string, alph *Alphabet) (string, error)

ToMachineOnlyString creates a non-printable string from the given word. This is used to make it hashable for map usage.

Types

type Alphabet

type Alphabet struct {
	// contains filtered or unexported fields
}

Alphabet defines an alphabet.

func EnglishAlphabet added in v0.2.11

func EnglishAlphabet() *Alphabet

EnglishAlphabet returns an alphabet that corresponds to the English alphabet. This function should be used for testing. In production we will load the alphabet from the gaddag.

func FrenchAlphabet added in v0.4.5

func FrenchAlphabet() *Alphabet

FrenchAlphabet returns an alphabet that corresponds to the English alphabet. This function should be used for testing. In production we will load the alphabet from the gaddag.

func FromSlice

func FromSlice(arr []uint32) *Alphabet

FromSlice creates an alphabet from a serialized array. It is the opposite of the Serialize function, except the length is implicitly passed in as the length of the slice.

func GermanAlphabet added in v0.4.5

func GermanAlphabet() *Alphabet

GermanAlphabet returns an alphabet that corresponds to the German alphabet. This function should be used for testing. In production we will load the alphabet from the gaddag.

func NorwegianAlphabet added in v0.4.5

func NorwegianAlphabet() *Alphabet

NorwegianAlphabet returns an alphabet that corresponds to the Norwegian alphabet. This function should be used for testing. In production we will load the alphabet from the gaddag. TODO: Reorder to follow Wolges sequence.

func PolishAlphabet added in v0.4.5

func PolishAlphabet() *Alphabet

PolishAlphabet returns an alphabet that corresponds to the Polish alphabet. This function should be used for testing. In production we will load the alphabet from the gaddag.

func SpanishAlphabet added in v0.4.0

func SpanishAlphabet() *Alphabet

SpanishAlphabet returns an alphabet that corresponds to the Spanish alphabet. This function should be used for testing. In production we will load the alphabet from the gaddag.

func (Alphabet) CurIdx

func (a Alphabet) CurIdx() MachineLetter

func (*Alphabet) Init

func (a *Alphabet) Init()

Init initializes the alphabet data structures

func (Alphabet) Letter

func (a Alphabet) Letter(b MachineLetter) rune

Letter returns the letter that this position in the alphabet corresponds to.

func (Alphabet) Letters

func (a Alphabet) Letters() map[MachineLetter]rune

func (Alphabet) NumLetters

func (a Alphabet) NumLetters() uint8

NumLetters returns the number of letters in this alphabet.

func (*Alphabet) Reconcile

func (a *Alphabet) Reconcile()

Reconcile will take a populated alphabet, sort the glyphs, and re-index the numbers.

func (*Alphabet) Serialize

func (a *Alphabet) Serialize() []uint32

Serialize serializes the alphabet into a slice of 32-bit integers.

func (*Alphabet) Update

func (a *Alphabet) Update(word string) error

update the alphabet map.

func (Alphabet) Val

func (a Alphabet) Val(r rune) (MachineLetter, error)

Val returns the 'value' of this rune in the alphabet; i.e a number from 0 to maxsize + blank offset. Takes into account blanks (lowercase letters).

func (Alphabet) Vals

func (a Alphabet) Vals() map[rune]MachineLetter

type Bag

type Bag struct {
	// contains filtered or unexported fields
}

A Bag is the bag o'tiles!

func NewBag

func NewBag(ld *LetterDistribution, alph *Alphabet) *Bag

func (*Bag) Copy added in v0.2.11

func (b *Bag) Copy() *Bag

Copy copies to a new bag and returns it. Note that the initialTiles are only shallowly copied. This is fine because we don't ever expect these to change after initialization.

func (*Bag) CopyFrom added in v0.2.11

func (b *Bag) CopyFrom(other *Bag)

CopyFrom copies back the tiles from another bag into this bag. The caller of this function is responsible for ensuring `other` has the other structures we need! (letter distribution, etc). It should have been created from the Copy function above.

func (*Bag) Draw

func (b *Bag) Draw(n int, ml []MachineLetter) error

Draw draws n random tiles from the bag. Shuffling is immaterial if fixedOrder is false. Returns the number of tiles actually drawn. This is a zero-alloc draw into the passed-in slice. NOTE: this function does not resize ml at all. It must be the correct size to allow tiles to fit in!

func (*Bag) DrawAtMost

func (b *Bag) DrawAtMost(n int, ml []MachineLetter) int

DrawAtMost draws at most n tiles from the bag. It can draw fewer if there are fewer tiles than n, and even draw no tiles at all :o This is a zero-alloc draw into the passed-in slice.

func (*Bag) Exchange

func (b *Bag) Exchange(letters []MachineLetter, ml []MachineLetter) error

Exchange exchanges the junk in your rack with new tiles.

func (*Bag) HasRack added in v0.5.0

func (b *Bag) HasRack(letters []MachineLetter) bool

hasRack returns a boolean indicating whether the passed-in rack is in the bag, in its entirety.

func (*Bag) LetterDistribution added in v0.4.0

func (b *Bag) LetterDistribution() *LetterDistribution

func (*Bag) Peek added in v0.2.11

func (b *Bag) Peek() []MachineLetter

func (*Bag) PeekMap added in v0.5.0

func (b *Bag) PeekMap() []uint8

func (*Bag) PutBack added in v0.4.0

func (b *Bag) PutBack(letters []MachineLetter)

PutBack puts the tiles back in the bag, and shuffles the bag.

func (*Bag) Redraw added in v0.4.0

func (b *Bag) Redraw(currentRack []MachineLetter, ml []MachineLetter) int

Redraw is basically a do-over; throw the current rack in the bag and draw a new rack.

func (*Bag) Refill

func (b *Bag) Refill()

Refill refills the bag.

func (*Bag) RemoveTiles

func (b *Bag) RemoveTiles(tiles []MachineLetter) error

RemoveTiles removes the given tiles from the bag, and returns an error if it can't.

func (*Bag) SetFixedOrder added in v0.5.0

func (b *Bag) SetFixedOrder(f bool)

SetFixedOrder makes the bag drawing algorithm repeatable if the bag is copied before drawing. This can be useful for more accurate Monte Carlo sims. It is extremely recommended to do a shuffle first if you want to use fixed order.

func (*Bag) Shuffle

func (b *Bag) Shuffle()

Shuffle shuffles the bag.

func (*Bag) TilesRemaining

func (b *Bag) TilesRemaining() int

type LetterDistribution

type LetterDistribution struct {
	Distribution map[rune]uint8
	PointValues  map[rune]uint8
	SortOrder    map[rune]int
	Vowels       []rune

	Name string
	// contains filtered or unexported fields
}

LetterDistribution encodes the tile distribution for the relevant game.

func EnglishLetterDistribution

func EnglishLetterDistribution(cfg *config.Config) (*LetterDistribution, error)

EnglishLetterDistribution returns the English letter distribution.

func FrenchLetterDistribution added in v0.4.5

func FrenchLetterDistribution(cfg *config.Config) (*LetterDistribution, error)

EnglishLetterDistribution returns the English letter distribution.

func GermanLetterDistribution added in v0.4.5

func GermanLetterDistribution(cfg *config.Config) (*LetterDistribution, error)

func Get added in v0.4.5

func Get(cfg *config.Config, name string) (*LetterDistribution, error)

Get loads a named alphabet from the cache or from a file

func NamedLetterDistribution added in v0.4.0

func NamedLetterDistribution(cfg *config.Config, name string) (*LetterDistribution, error)

NamedLetterDistribution loads a letter distribution by name.

func NorwegianLetterDistribution added in v0.4.5

func NorwegianLetterDistribution(cfg *config.Config) (*LetterDistribution, error)

func PolishLetterDistribution added in v0.2.1

func PolishLetterDistribution(cfg *config.Config) (*LetterDistribution, error)

func ScanLetterDistribution added in v0.4.5

func ScanLetterDistribution(data io.Reader) (*LetterDistribution, error)

func SpanishLetterDistribution

func SpanishLetterDistribution(cfg *config.Config) (*LetterDistribution, error)

func (*LetterDistribution) Alphabet added in v0.4.5

func (ld *LetterDistribution) Alphabet() *Alphabet

func (*LetterDistribution) MakeBag

func (ld *LetterDistribution) MakeBag() *Bag

MakeBag returns a bag of tiles.

func (*LetterDistribution) NumTotalTiles added in v0.4.0

func (ld *LetterDistribution) NumTotalTiles() int

func (*LetterDistribution) Score added in v0.4.0

func (ld *LetterDistribution) Score(ml MachineLetter) int

Score gives the score of the given machine letter. This is used by the move generator to score plays more rapidly than looking up a map.

type LetterSet

type LetterSet uint64

LetterSet is a bit mask of acceptable letters, with indices from 0 to the maximum alphabet size.

type LetterSlice

type LetterSlice []rune

LetterSlice is a slice of runes. We make it a separate type for ease in defining sort functions on it.

func (LetterSlice) Len

func (a LetterSlice) Len() int

func (LetterSlice) Less

func (a LetterSlice) Less(i, j int) bool

func (LetterSlice) Swap

func (a LetterSlice) Swap(i, j int)

type MachineLetter

type MachineLetter byte

MachineLetter is a machine-only representation of a letter. It goes from 0 to the maximum alphabet size.

func ToMachineLetters

func ToMachineLetters(word string, alph *Alphabet) ([]MachineLetter, error)

ToMachineLetters creates an array of MachineLetters from the given string.

func (MachineLetter) Blank

func (ml MachineLetter) Blank() MachineLetter

Blank blankifies a letter; i.e. it adds the BlankOffset to it. It returns a new MachineLetter; it does not modify the one that is passed in!

func (MachineLetter) IntrinsicTileIdx added in v0.2.11

func (ml MachineLetter) IntrinsicTileIdx() (MachineLetter, bool)

IntrinsicTileIdx returns the index that this tile would have in a rack's LetArr. If it is not a letter (for example a played-thru marker) will return false as the second arg. It essentially checks whether the relevant ml is an actual played tile or not.

func (MachineLetter) IsBlanked

func (ml MachineLetter) IsBlanked() bool

IsBlanked returns true if this is the blank version of a letter.

func (MachineLetter) IsPlayedTile added in v0.2.11

func (ml MachineLetter) IsPlayedTile() bool

IsPlayedTile returns true if this represents a tile that was actually played on the board. It has to be an assigned blank or a letter, not a played-through-marker.

func (MachineLetter) IsVowel

func (ml MachineLetter) IsVowel(alph *Alphabet) bool

IsVowel returns true for vowels. Note that this needs an alphabet.

func (MachineLetter) Unblank

func (ml MachineLetter) Unblank() MachineLetter

Unblank is the opposite of the above function; it removes the blank offset from a letter.

func (MachineLetter) UserVisible

func (ml MachineLetter) UserVisible(alph *Alphabet) rune

UserVisible turns the passed-in machine letter into a user-visible rune.

type MachineWord

type MachineWord []MachineLetter

MachineWord is an array of MachineLetters

func ToMachineWord

func ToMachineWord(word string, alph *Alphabet) (MachineWord, error)

ToMachineWord creates a MachineWord from the given string.

func (MachineWord) Bytes added in v0.2.11

func (mw MachineWord) Bytes() []byte

Bytes converts the machine word to bytes. Also useful for hashing purposes.

func (MachineWord) Score

func (mw MachineWord) Score(ld *LetterDistribution) int

Score returns the score of this word given the ld.

func (MachineWord) String

func (mw MachineWord) String() string

String() returns a non-printable string version of this machineword. This is useful for hashing purposes.

func (MachineWord) UserVisible

func (mw MachineWord) UserVisible(alph *Alphabet) string

UserVisible turns the passed-in machine word into a user-visible string.

type Rack

type Rack struct {
	// letArr is an array of letter codes from 0 to MaxAlphabetSize.
	// The blank can go at the MaxAlphabetSize place.
	LetArr []int
	// contains filtered or unexported fields
}

Rack is a machine-friendly representation of a user's rack.

func NewRack

func NewRack(alph *Alphabet) *Rack

NewRack creates a brand new rack structure with an alphabet.

func RackFromString

func RackFromString(rack string, a *Alphabet) *Rack

RackFromString creates a Rack from a string and an alphabet

func (*Rack) Add

func (r *Rack) Add(letter MachineLetter)

func (*Rack) Alphabet added in v0.3.0

func (r *Rack) Alphabet() *Alphabet

func (*Rack) Clear added in v0.4.2

func (r *Rack) Clear()

func (*Rack) Copy added in v0.2.11

func (r *Rack) Copy() *Rack

Copy returns a deep copy of this rack

func (*Rack) CopyFrom added in v0.2.11

func (r *Rack) CopyFrom(other *Rack)

func (*Rack) CountOf added in v0.4.9

func (r *Rack) CountOf(letter MachineLetter) int

func (*Rack) Empty

func (r *Rack) Empty() bool

func (*Rack) Has added in v0.4.4

func (r *Rack) Has(letter MachineLetter) bool

func (*Rack) Hashable added in v0.2.11

func (r *Rack) Hashable() string

Hashable returns a hashable representation of this rack, that is not necessarily user-friendly.

func (*Rack) NoAllocTilesOn added in v0.4.9

func (r *Rack) NoAllocTilesOn(letters []MachineLetter) int

NoAllocTilesOn places the tiles in the passed-in slice, and returns the number of letters

func (*Rack) NumTiles

func (r *Rack) NumTiles() uint8

NumTiles returns the current number of tiles on this rack.

func (*Rack) ScoreOn

func (r *Rack) ScoreOn(ld *LetterDistribution) int

ScoreOn returns the total score of the tiles on this rack.

func (*Rack) Set

func (r *Rack) Set(mls []MachineLetter)

Set sets the rack from a list of machine letters

func (*Rack) String

func (r *Rack) String() string

String returns a user-visible version of this rack.

func (*Rack) Take

func (r *Rack) Take(letter MachineLetter)

func (*Rack) TilesOn

func (r *Rack) TilesOn() MachineWord

TilesOn returns the MachineLetters of the rack's current tiles. It is alphabetized.

type Word

type Word struct {
	Word string
	Dist *LetterDistribution
	// contains filtered or unexported fields
}

func (Word) Len

func (w Word) Len() int

func (Word) Less

func (w Word) Less(i, j int) bool

func (Word) MakeAlphagram

func (w Word) MakeAlphagram() string

func (Word) String

func (w Word) String() string

func (Word) Swap

func (w Word) Swap(i, j int)

Jump to

Keyboard shortcuts

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