domain

package
v0.0.0-...-135d0e9 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2018 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package domain provides the domain logic for games (e.g. Hangman).

Index

Constants

This section is empty.

Variables

NewID generates a new unique id for an object in the domain

Functions

func DefaultNewID

func DefaultNewID() string

DefaultNewID is the default implementation of NewID, and creates an id string similar to MongoDB.

func LoadAllHangman

func LoadAllHangman(s store.Store) (map[store.ID]*Hangman, error)

LoadAllHangman loads all hangman games from the store

func SaveHangman

func SaveHangman(s store.Store, id store.ID, g *Hangman) (store.ID, error)

SaveHangman loads a specific game from the store

Types

type Dictionary

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

Dictionary is an unsorted immutable ring of words

func NewDictionary

func NewDictionary(words []string) *Dictionary

NewDictionary creates a new dictionary object from a slice of words

func (*Dictionary) GetAt

func (d *Dictionary) GetAt(pos int) string

GetAt returns a word form the Dictionary based on the value of a given position in the ring

type ErrAlreadyUsed

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

ErrAlreadyUsed indicates that the letter choice has previously been used.

type ErrGameOver

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

ErrGameOver indicates that the game has finished and no more play can take place.

type ErrInvalidLetter

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

ErrInvalidLetter indicates that a letter is not valid choice for the game

type ErrInvalidTurns

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

ErrInvalidTurns indicates that the specified number of turns is not acceptable.

type ErrInvalidWord

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

ErrInvalidWord indicates that a word is not valid choice for the game

type Hangman

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

Hangman represents a single game of hangman.

func LoadHangman

func LoadHangman(s store.Store, id store.ID) (*Hangman, store.ID, error)

LoadHangman loads a specific game from the store

func NewHangman

func NewHangman(word string, turns int) (*Hangman, error)

NewHangman creates a new instance of Hangman.

func (*Hangman) Current

func (g *Hangman) Current() string

Current returns the current state of the players knowledge about the word.

func (*Hangman) PlayLetter

func (g *Hangman) PlayLetter(l rune) (bool, error)

PlayLetter allows the client to guess a letter in the word.

func (*Hangman) PlayWord

func (g *Hangman) PlayWord(word string) (bool, error)

PlayWord allows the client to guess the word.

func (*Hangman) State

func (g *Hangman) State() State

State returns the current state of the game (Play, Win, Lose).

func (*Hangman) Turns

func (g *Hangman) Turns() int

Turns returns the number of turns remaining in the game.

func (*Hangman) Word

func (g *Hangman) Word() string

Word returns the word that needs to be guessed.

type HangmanResult

type HangmanResult struct {
	ID      store.ID `json:"id,omitempty"`
	Current string   `json:"current,omitempty"`
	Word    *string  `json:"word,omitempty"`
	Turns   int      `json:"turns,omitempty"`
	State   State    `json:"state,omitempty"`
	Success *bool    `json:"success,omitempty"`
}

HangmanResult is the result of the last action in a game of hangman.

func CreateHangman

func CreateHangman(stg store.Store, word string, turns int) (*HangmanResult, error)

CreateHangman creates a new game instance and stores it.

func JoinHangman

func JoinHangman(stg store.Store, id store.ID) (*HangmanResult, error)

JoinHangman loads an existing game instance.

func PlayLetter

func PlayLetter(stg store.Store, id store.ID, letter rune) (*HangmanResult, error)

PlayLetter submits a letter guess to a specified game

func PlayWord

func PlayWord(stg store.Store, id store.ID, word string) (*HangmanResult, error)

PlayWord submits a word guess to a specified game.

type HangmanResults

type HangmanResults []*HangmanResult

HangmanResults is a slice of HangmanResult

func ListHangman

func ListHangman(stg store.Store) (HangmanResults, error)

ListHangman returns details of all current games of hangman in the store

type State

type State int

State is an enum representing the state of a game.

const (
	// Play indicates that gameplay is in progress,
	Play State = iota + 1
	// Win indicates that the game is complete and the client has won.
	Win
	// Lose indicates that the game is complete and the client has lost.
	Lose
)

func (*State) MarshalJSON

func (s *State) MarshalJSON() ([]byte, error)

MarshalJSON marshals an instance of State enum into JSON.

func (State) String

func (s State) String() string

String returns the string representation of the enum (play,win.lose)

func (*State) UnmarshalJSON

func (s *State) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals JSON into an instance of the State enum.

Jump to

Keyboard shortcuts

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