game

package
v0.0.0-...-ec02fb1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

package Game implements the core logic of rock paper scissors lizard spock

Index

Constants

View Source
const (
	NUM_PLAYERS   = 2
	GAMEID_LENGTH = 5
)

Variables

This section is empty.

Functions

func Beats

func Beats(first, second string) (bool, string)

Beats returns if first would beat second also returns the verb needed <first> crushes <second> In the case of a tie, returns "ties" as the verb

func GenerateRandomBytes

func GenerateRandomBytes(n int) ([]byte, error)

GenerateRandomBytes returns securely generated random bytes. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateRandomString

func GenerateRandomString(n int) (string, error)

GenerateRandomString returns a random string of length N

func ValidPlay

func ValidPlay(play string) bool

ValidPlay returns true only if the play given in the argument is valid

Types

type Game

type Game struct {
	// ID is the identifier of the overall game
	ID string
	// Round tracks which round of the game is currently active
	Round int
	// PlayCount keeps track of how many plays have been submitted
	PlayCount int
	// Players is indexed by Player.ID and points to player records
	Players map[string]*Player
	// RoundSummary is a summary of the previous round ("Rock beats Scissors")
	RoundSummary string
	// Winner is the Player.ID which won the last round.
	Winner string
}

Game is the key data for the overall game

func NewGame

func NewGame() *Game

func (*Game) AdvanceGame

func (g *Game) AdvanceGame() error

AdvanceGame updates a game to resolve the winner, round, etc

type GameContext

type GameContext struct {
	Game *Game
	// ID of the current player inside the game
	ActingPlayer *Player
}

GameContext is a container for the overall game and the current player action in it

func NewGameContext

func NewGameContext(playerID, playerAddress string, game *Game) (*GameContext, error)

func (*GameContext) AssignPlayer

func (gc *GameContext) AssignPlayer(p *Player) error

AssignPlayer sets a player to be P1 or P2

func (*GameContext) Play

func (gc *GameContext) Play(play string) error

type Player

type Player struct {
	// ID is the user-supplied user identifier
	ID string
	// Address is the connection id (or other location) for how to get to the player
	// It may change over time for the same player, e.g. if they reconnect
	Address string
	// Play is the player's last move
	Play string
	// Round is the last round played by the player
	Round int
	// Score is the player's score
	Score int
	// Game is the GameID this player is associated with
	Game string
	// WonLastRound identifies if the player ... won the last round
	WonLastRound bool
}

Player stores relevant information about a current player's state

Jump to

Keyboard shortcuts

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