scouts

package
v0.0.0-...-804359f Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2023 License: ISC Imports: 8 Imported by: 0

Documentation

Overview

Package scouts implements the game of Scouts.

Index

Constants

View Source
const (
	FormattedPlayerAScoutPiece   = 'A'
	FormattedPlayerBScoutPiece   = 'B'
	FormattedPlayerABoulderPiece = 'a'
	FormattedPlayerBBoulderPiece = 'b'
	FormattedBlankPiece          = '.'
)
View Source
const (
	Player1 = PlayerA
	Player2 = PlayerB
)
View Source
const PlaceScoutTurns = 5

PlaceScoutTurns is the number of turns that each player has to place their scouts. After this number of turns, other moves are allowed.

View Source
const PlaysPerTurn = 2

PlaysPerTurn is the number of plays that each player has at the beginning of each turn after the start.

View Source
const StartingPlaysPerTurn = 1

StartingPlaysPerTurn is the number of plays that each player has at the beginning of each turn. The start is always when the player must place a scout.

Variables

View Source
var BoardBounds = image.Rect(0, 0, 8, 10)

BoardBounds is the size of the board.

Functions

func IsPlayerBase

func IsPlayerBase(player Player, pt Point) bool

IsPlayerBase returns whether the given point is on the base of the given player.

Types

type Board

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

Board is a type that represents the board. Player A is at the bottom of the board and player B is at the top of the board. It exposes no methods for modifying the board, only for reading it. To modify the board, use the Apply method on a Move.

func NewBoard

func NewBoard() *Board

NewBoard returns a new board.

func (*Board) Bounds

func (b *Board) Bounds() image.Rectangle

Bounds returns the bounds of the board.

func (*Board) HasPieceAt

func (b *Board) HasPieceAt(p Point) bool

HasPieceAt returns whether there is a piece at the given point.

func (*Board) PieceAt

func (b *Board) PieceAt(p Point) Piece

PieceAt returns the piece at the given point, or nil if there is no piece at the given point.

func (*Board) PieceKindAt

func (b *Board) PieceKindAt(p Point) PieceKind

func (*Board) Pieces

func (b *Board) Pieces() []Piece

Pieces returns the pieces on the board.

func (*Board) PointIsPiece

func (b *Board) PointIsPiece(p Point, kind PieceKind) bool

func (*Board) PointIsPlayer

func (b *Board) PointIsPlayer(p Point, player Player) bool

type BoulderMove

type BoulderMove struct {
	// TopLeft is the top left position of the boulder.
	TopLeft Point `json:"top_left"`
}

BoulderMove represents a move that places a boulder on the board. A boulder is a 2x2 piece that cannot be moved.

func (*BoulderMove) MarshalText

func (m *BoulderMove) MarshalText() ([]byte, error)

func (*BoulderMove) String

func (m *BoulderMove) String() string

func (*BoulderMove) Type

func (m *BoulderMove) Type() MoveType

Type returns the move type.

func (*BoulderMove) UnmarshalText

func (m *BoulderMove) UnmarshalText(text []byte) error

type BoulderPiece

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

BoulderPiece is a type that represents a boulder piece on the board.

func (*BoulderPiece) Kind

func (p *BoulderPiece) Kind() PieceKind

Kind returns the kind of the piece.

func (*BoulderPiece) MarshalJSON

func (p *BoulderPiece) MarshalJSON() ([]byte, error)

MarshalJSON marshals the boulder piece to JSON.

func (*BoulderPiece) Player

func (p *BoulderPiece) Player() Player

Player returns the player that owns the piece.

func (*BoulderPiece) Position

func (p *BoulderPiece) Position() []Point

Position returns the position of the piece, which may be multiple points.

type BoulderPieceModel

type BoulderPieceModel struct {
	Kind     PieceKind `json:"kind"`
	Player   Player    `json:"player"`
	Position [4]Point  `json:"position"`
}

BoulderPieceModel is the JSON model for a boulder piece.

type CurrentTurn

type CurrentTurn struct {
	// Moves is the list of past valid moves that the player has made.
	Moves []Move
	// Plays is the number of plays that the player has left.
	Plays int
	// Player is the player whose turn it is.
	Player Player
}

CurrentTurn is a type that represents the current turn in the game. A current turn instance will not contain any invalid moves, but it may not be a complete turn.

type DashMove

type DashMove struct {
	ScoutPosition Point `json:"scout_position"`
	Destination   Point `json:"destination"`
}

DashMove represents a dash move. A player may dash a scout in any direction in 1 unit increments. The scout may not dash off the board.

func (*DashMove) MarshalText

func (m *DashMove) MarshalText() ([]byte, error)

func (*DashMove) String

func (m *DashMove) String() string

func (*DashMove) Type

func (m *DashMove) Type() MoveType

Type returns the move type.

func (*DashMove) UnmarshalText

func (m *DashMove) UnmarshalText(text []byte) error

type FormattedBoard

type FormattedBoard string

FormattedBoard is a type that represents a board in a human-readable format. This is mostly used for debugging.

func FormatBoard

func FormatBoard(board *Board) FormattedBoard

FormatBoard returns a human-readable representation of the board.

type Game

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

Game is a game instance.

func NewGame

func NewGame() *Game

NewGame returns a new game instance.

func NewGameFromPastTurns

func NewGameFromPastTurns(turns []PastTurn) (*Game, error)

NewGameFromPastTurns returns a new game instance from the given past turns.

func (*Game) Apply

func (g *Game) Apply(p Player, move Move) error

Apply applies the given move to the game.

func (*Game) Board

func (g *Game) Board() *Board

Board returns the board.

func (*Game) CurrentTurn

func (g *Game) CurrentTurn() CurrentTurn

CurrentTurn returns the current turn.

func (*Game) Ended

func (g *Game) Ended() (Player, bool)

Ended returns true and the winning player if the game has ended.

func (*Game) MakeMove

func (g *Game) MakeMove(p Player, move Move) error

MakeMove is an alias for [Apply].

func (*Game) PastTurns

func (g *Game) PastTurns() []PastTurn

PastTurns returns the past turns. It only contains valid turns. It does not contain the current turn.

func (*Game) PlayerPastTurns

func (g *Game) PlayerPastTurns(p Player) []PastTurn

PlayerPastTurns returns the past turns for the given player. It only contains valid turns. It does not contain the current turn.

func (*Game) PossibleMoves

func (g *Game) PossibleMoves(p Player) PossibleMoves

PossibleMoves returns the possible moves for the given player.

type JumpMove

type JumpMove struct {
	ScoutPosition Point `json:"scout_position"`
	Destination   Point `json:"destination"`
}

JumpMove represents a jump move. A jump move describes a piece jumping over another piece in one of the four cardinal directions. A jump move costs 0 plays, but later jumps must be made on the same scout.

func (*JumpMove) MarshalText

func (m *JumpMove) MarshalText() ([]byte, error)

func (*JumpMove) String

func (m *JumpMove) String() string

func (*JumpMove) Type

func (m *JumpMove) Type() MoveType

Type returns the move type.

func (*JumpMove) UnmarshalText

func (m *JumpMove) UnmarshalText(text []byte) error

type Move

type Move interface {
	encoding.TextMarshaler
	encoding.TextUnmarshaler
	fmt.Stringer

	// Type returns the move type.
	Type() MoveType
	// contains filtered or unexported methods
}

Move is a type that represents a move.

func ParseMove

func ParseMove(s string) (Move, error)

ParseMove parses a move from a string.

type MoveType

type MoveType string

MoveType is a type that represents a move type.

const BoulderMoveType MoveType = "boulder"
const DashMoveType MoveType = "dash"
const JumpMoveType MoveType = "jump"
const PlaceScoutMoveType MoveType = "place_scout"
const SkipMoveType MoveType = "skip"

type Moves

type Moves []Move

Moves is a list of moves.

func ParseMoves

func ParseMoves(str string) (Moves, error)

ParseMoves parses a list of moves.

func (*Moves) MarshalText

func (m *Moves) MarshalText() ([]byte, error)

func (*Moves) UnmarshalText

func (m *Moves) UnmarshalText(text []byte) error

type PastTurn

type PastTurn struct {
	// Moves is the list of past valid moves that the player has made.
	Moves []Move
	// Player is the player whose turn it is.
	Player Player
}

PastTurn is a type that represents a turn in the game. A game turn instance must represent a valid turn, meaning it must contian no invalid moves.

type Piece

type Piece interface {
	json.Marshaler

	// Kind returns the kind of the piece.
	Kind() PieceKind
	// Player returns the player that owns the piece.
	Player() Player
	// Position returns the position of the piece, which may be multiple points.
	Position() []Point
}

Piece is a type that represents a piece on the board. A piece must be able to be marshaled to JSON. It must not be unmarshaled.

type PieceKind

type PieceKind string

PieceKind is a type that can either be ScoutPiece or BoulderPiece.

const (
	NoPieceKind      PieceKind = ""
	ScoutPieceKind   PieceKind = "scout"
	BoulderPieceKind PieceKind = "boulder"
)

func (PieceKind) String

func (k PieceKind) String() string

type PlaceScoutMove

type PlaceScoutMove struct {
	ScoutPosition Point `json:"scout_position"`
}

PlaceScoutMove represents a place scout move. Each player must place 5 scouts on their base before the game begins.

func (*PlaceScoutMove) MarshalText

func (m *PlaceScoutMove) MarshalText() ([]byte, error)

func (*PlaceScoutMove) String

func (m *PlaceScoutMove) String() string

func (*PlaceScoutMove) Type

func (m *PlaceScoutMove) Type() MoveType

Type returns the move type.

func (*PlaceScoutMove) UnmarshalText

func (m *PlaceScoutMove) UnmarshalText(text []byte) error

type Player

type Player int

Player is a type that represents a player.

const (
	PlayerNone Player = iota
	PlayerA
	PlayerB
)

func ParsePlayer

func ParsePlayer(str string) (Player, error)

ParsePlayer parses a player from a string. To convert a player to a string, use the String method.

func (Player) Opponent

func (p Player) Opponent() Player

Opponent returns the opponent of the player.

func (Player) String

func (p Player) String() string

String returns the string representation of the player.

func (Player) Validate

func (p Player) Validate() error

Validate validates the player.

type Point

type Point struct {
	X int `json:"x"`
	Y int `json:"y"`
}

Point is a point on the board.

func Pt

func Pt(x, y int) Point

Pt returns a Point with the given coordinates.

func (Point) Add

func (p Point) Add(q Point) Point

Add returns the vector p+q.

func (Point) Div

func (p Point) Div(k int) Point

Div returns the vector p/k.

func (Point) Eq

func (p Point) Eq(q Point) bool

Eq reports whether p and q are equal.

func (Point) In

func (p Point) In(r image.Rectangle) bool

In reports whether p is in r.

func (Point) MarshalJSON

func (p Point) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Point) MarshalText

func (p Point) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Point) Mul

func (p Point) Mul(k int) Point

Mul returns the vector p*k.

func (Point) String

func (p Point) String() string

String returns a string representation of the point in the form "x,y".

func (Point) Sub

func (p Point) Sub(q Point) Point

Sub returns the vector p-q.

func (*Point) UnmarshalJSON

func (p *Point) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Point) UnmarshalText

func (p *Point) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type PossibleMoves

type PossibleMoves struct {
	// Moves is a list of possible moves. It never contains BoulderMove, since
	// that's a special case that's determined through the CanPlaceBoulder
	// field.
	Moves Moves `json:"moves"`
	// CanPlaceBoulder is whether or not the player can place a boulder.
	CanPlaceBoulder bool `json:"can_place_boulder"`
}

PossibleMoves represents a list of possible moves.

func (PossibleMoves) String

func (m PossibleMoves) String() string

type ScoutPiece

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

ScoutPiece is a type that represents a scout piece on the board.

func (*ScoutPiece) Kind

func (p *ScoutPiece) Kind() PieceKind

Kind returns the kind of the piece.

func (*ScoutPiece) MarshalJSON

func (p *ScoutPiece) MarshalJSON() ([]byte, error)

MarshalJSON marshals the scout piece to JSON.

func (*ScoutPiece) Player

func (p *ScoutPiece) Player() Player

Player returns the player that owns the piece.

func (*ScoutPiece) Position

func (p *ScoutPiece) Position() []Point

Position returns the position of the piece, which may be multiple points.

func (*ScoutPiece) Returning

func (p *ScoutPiece) Returning() bool

Returning returns whether the scout is returning to its base.

type ScoutsPieceModel

type ScoutsPieceModel struct {
	Kind      PieceKind `json:"kind"`
	Player    Player    `json:"player"`
	Position  Point     `json:"position"`
	Returning bool      `json:"returning"`
}

ScoutsPieceModel is the JSON model for a scout piece.

type SkipMove

type SkipMove struct{}

SkipMove represents a skip move.

func (*SkipMove) MarshalText

func (m *SkipMove) MarshalText() ([]byte, error)

func (*SkipMove) String

func (m *SkipMove) String() string

func (*SkipMove) Type

func (m *SkipMove) Type() MoveType

Type returns the move type.

func (*SkipMove) UnmarshalText

func (m *SkipMove) UnmarshalText(text []byte) error

type UnexpectedGameStateError

type UnexpectedGameStateError struct {
	Expected gameState `json:"expected"`
	Actual   gameState `json:"actual"`
}

UnexpectedGameStateError is an error that is returned when the game state is not the expected game state.

func (UnexpectedGameStateError) Error

func (e UnexpectedGameStateError) Error() string

type UnexpectedPieceError

type UnexpectedPieceError struct {
	Position Point     `json:"position"`
	Expected PieceKind `json:"expected"`
	Actual   PieceKind `json:"actual"`
}

UnexpectedPieceError is an error that is returned when a piece is not the expected piece.

func (UnexpectedPieceError) Error

func (e UnexpectedPieceError) Error() string

Jump to

Keyboard shortcuts

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