dvonn

package
v0.0.0-...-1281737 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2020 License: Apache-2.0 Imports: 5 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

func Clone(a, b interface{})

Clone deep-copies a to b

func DeepCopy

func DeepCopy(a, b interface{})

DeepCopy deepcopies a to b using json marshaling

Types

type Chip

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

func GetChips

func GetChips(color ChipColor) Chip

func (*Chip) GetColor

func (c *Chip) GetColor() ChipColor

type ChipColor

type ChipColor string
const (
	RED   ChipColor = "RED"
	WHITE ChipColor = "WHITE"
	BLACK ChipColor = "BLACK"
)

can be used to set the chip color as well as the player selected color

type DvonnBoard

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

func GetDvonnBoard

func GetDvonnBoard() *DvonnBoard

func (*DvonnBoard) DeFill

func (db *DvonnBoard) DeFill(id string)

Removes all chips from the Box whose id is passed

func (*DvonnBoard) Fill

func (db *DvonnBoard) Fill(id string, chips []Chip)

Adds a stack of chips to the box whose id is passed NOTE: we shouldn't add extra logic here, like if game in placement phase then don't allow filling multiple chips on stack, etc. This should be taken care by the implementer of this method or should be taken care at root level.

func (*DvonnBoard) GetBoardSnapshot

func (db *DvonnBoard) GetBoardSnapshot()

func (*DvonnBoard) GetCellIdsByStackColor

func (db *DvonnBoard) GetCellIdsByStackColor(color ChipColor) []string

func (*DvonnBoard) GetCells

func (db *DvonnBoard) GetCells() map[string]*HexNode

NOTE: This method returns the cells on the board, however we should not be using this method coz, anything which we need to query on the cells should be written as a method in this class

func (*DvonnBoard) GetCountOfPiecesControlledBy

func (db *DvonnBoard) GetCountOfPiecesControlledBy(color ChipColor) int

func (*DvonnBoard) GetDisconnectedCells

func (db *DvonnBoard) GetDisconnectedCells() []string
 Returns cells which are not connected to the red chip Node by any link, if all are connected, then returns nil
 Solution:  - Get all the nodes where red chip are placed
			- Traverse from each of the above node where red chip is placed and store the visited node id
			- The ids which aren't visited will be the disconnected nodes

func (*DvonnBoard) GetPossibleMoveFor

func (db *DvonnBoard) GetPossibleMoveFor(id string) []*HexNode
 argument: source node id
 return: list of nodes where the chip stacks can be placed from the id passed in argument

 Solution: iterates over the node map in each of the six direction from the hexagonal node
		NOTE that maximum number of possible move for any Id can be 6, as there are 6 edges
		to the node, and nodes can be moved in any direction, but only along straight lines

func (*DvonnBoard) GetRedChipsIds

func (db *DvonnBoard) GetRedChipsIds() []string
 Returns the identifiers of hexagonal nodes where a red chip is placed
 Solution: Iterates over all the board cells and check if red chip is present in chip stack.
		   Another alternative could be storing a list of ids for red chip addresses at the time Fill is called
		   but if we go with later approach we will need to add extra piece of code in Fill() method which will
		   violate single responsibility principle. So writing a extra method below.

TODO: write testcase

func (*DvonnBoard) IsCellEmpty

func (db *DvonnBoard) IsCellEmpty(id string) bool

func (*DvonnBoard) IsPlaceVacant

func (db *DvonnBoard) IsPlaceVacant(placeId string) bool

func (*DvonnBoard) RemoveDisconnectedCells

func (db *DvonnBoard) RemoveDisconnectedCells()

type DvonnGame

type DvonnGame struct {

	// some props for undo/redo operation
	HEAD int // an index pointer which shall be pointing to the current game state
	// contains filtered or unexported fields
}

func GetDvonnGame

func GetDvonnGame(players []Player, whitePlayer Player) *DvonnGame

func (*DvonnGame) AddPlayer

func (dg *DvonnGame) AddPlayer(player Player)
 NOTE: Ideally the players needs to be passed at the time when Instance of Game is being created, adding player
	   in b/w doesn't make any sense unless the scope of game changes in future.

func (*DvonnGame) GetBoard

func (dg *DvonnGame) GetBoard() *DvonnBoard

func (*DvonnGame) GetCurrentPlayer

func (dg *DvonnGame) GetCurrentPlayer() Player

NOTE: this shall be majorly be used for validating the user move, i.e. only current player turn can take move Also make sure that the current player is getting updated after each "valid" move.

func (*DvonnGame) GetGamePhase

func (dg *DvonnGame) GetGamePhase() GamePhase

func (*DvonnGame) GetGameWinner

func (dg *DvonnGame) GetGameWinner() (*MatchResult, error)

Returns winner color

func (*DvonnGame) GetNextTurnPlayer

func (dg *DvonnGame) GetNextTurnPlayer() (Player, error)
 Returns the player whose turn is next, i.e. after the current move
 Logic: if game is not over then either of the player has a valid move left
	so, it returns other player if other player has a valid move, otherwise
	it returns the current player itself

func (*DvonnGame) GetPlayerByColor

func (dg *DvonnGame) GetPlayerByColor(color ChipColor) Player

func (*DvonnGame) GetPlayers

func (dg *DvonnGame) GetPlayers() []Player

func (*DvonnGame) HasNextPlayerAValidMove

func (dg *DvonnGame) HasNextPlayerAValidMove(currentPlayer Player) bool

tells if the other player has some valid move left to play or not

func (*DvonnGame) IsGameOver

func (dg *DvonnGame) IsGameOver() bool

returns false if any one of the player has any move left to play in MOVEMENT phase, else return true

func (*DvonnGame) IsValidMoveLeftForPlayer

func (dg *DvonnGame) IsValidMoveLeftForPlayer(playerColor ChipColor) bool
 Checks if any valid move is left for a player
 arg: player for whom checking valid move is required
 return: return true if any valid move is available, else false

 Solution:
	- get all cell ids which have the respective player color on top of stack
	- If any possible move from that cell id is present then return true
	- If no possible move could be found then return false, stating no valid move left

func (*DvonnGame) Move

func (dg *DvonnGame) Move(player Player, paths ...string) MoveResult

This is a wrapper method on _move method which is actually doing all the logic related to move. NOTE: this method is purposely written to be consumed by game client for playing moves This method will also serve the purpose of writing logic on result obtained from the last move played. This method should be called once for one move.

func (*DvonnGame) Redo

func (dg *DvonnGame) Redo() bool

Usage of this method: It doesn't mutate the game state, for that the caller need to make sure for reinitializing the game state with what is returned from this method.

NOTE: redo can only be performed when last step would be undo operation. So, there is a clear validation for the HEAD pointer must be smaller than the movesPlayed stack length.

if undo would have been done in last step then dg.HEAD would be lesser than the length of movesPlayed stack coz, an decrement for the HEAD must have happened if undo wasn't done in the last step then the length of the game state stack would match the HEAD (NOTE: in case of normal play steps I do pop off the extra top elements above the HEAD to maintain consistency)

func (*DvonnGame) RemovePlayer

func (dg *DvonnGame) RemovePlayer(playerId string)

TODO: this method will not be used extensively, very rarely this has some use case but don't forget to write testcase

func (*DvonnGame) Undo

func (dg *DvonnGame) Undo() (*DvonnGame, bool)
 Usage of this method: this method doesn't mutate the current state rather, it will return one state which will be
 the previous state. So, the caller needs to make sure that instance is being reassigned to what is being returned from hear

 In case of consecutive undo operation:
	method checks if HEAD pointer is pointing to 0, then it can't perform undo operation and this method will return
	a flag to indicate that

 Each time we perform a successful undo operation we decrement the HEAD pointer
 And on any successful move post undo operation we pop off the top elements after HEAD pointer from the movesPlayed stack
 to balance the game state stack and HEAD pointer.

type EdgeType

type EdgeType int
const (
	UNKNOWN          EdgeType = iota
	UPPER_EDGE                // upper
	LEFTUPPER_EDGE            // left upper
	LEFTBOTTON_EDGE           // left button
	BOTTON_EDGE               // bottom
	RIGHTUPPER_EDGE           // right upper
	RIGHTBOTTON_EDGE          // right bottom
)

type ErrorCode

type ErrorCode int

represents error states

const (
	ERROR_UNKNOWN ErrorCode = iota
	ERROR_ARGUMENT_COUNT_MISMATCH
	ERROR_INVALID_PLAYER_TURN
	ERROR_DESTINATION_ALREADY_OCCUPIED

	ERROR_INVALID_GAME_PHASE
	ERROR_EMPTY_ORIGIN_DESTINATION
	ERROR_EMPTY_ORIGIN
	ERROR_EMPTY_DESTINATION
	ERROR_NO_FREE_ADJACENT_FOUND
	ERROR_INVALID_DESTINATION_SELECTED
	ERROR_WRONG_ORIGIN_SELECTED
)

type FilterFunc

type FilterFunc func(v interface{}) bool

type GamePhase

type GamePhase int
const (
	UNKNOWN_PHASE GamePhase = iota
	PLACEMENT_PHASE
	MOVEMENT_PHASE
)

type HexNode

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

func GetHexNode

func GetHexNode(id string) *HexNode

func (*HexNode) AddChips

func (hn *HexNode) AddChips(chips []Chip)

To Add chips on the box, accepts slice of chips (stack of chips)

func (*HexNode) Empty

func (hn *HexNode) Empty()

To make the Hex Box empty, this operation will be performed when player moves to other box

func (*HexNode) GetChildNodes

func (hn *HexNode) GetChildNodes() map[EdgeType]*HexNode

func (*HexNode) GetChipsStack

func (hn *HexNode) GetChipsStack() []Chip

func (*HexNode) GetIdentifier

func (hn *HexNode) GetIdentifier() string

func (*HexNode) GetPossibleMovements

func (hn *HexNode) GetPossibleMovements()

func (*HexNode) GetStackLength

func (hn *HexNode) GetStackLength() int

func (*HexNode) GetStraightAdjacentOnLevel

func (hn *HexNode) GetStraightAdjacentOnLevel(level int) []*HexNode

returns: the list of Adjacent nodes which are in straight line from the current Node Adjacent nodes in straight line means: As there can be six adjacent nodes in a hexagonal node and those nodes

can have another six adjacent nodes, but if you see not all of them will be in straight line.

func (*HexNode) GetTopChips

func (hn *HexNode) GetTopChips() (Chip, error)

func (*HexNode) HasFreeEdge

func (hn *HexNode) HasFreeEdge() bool

returns true if any of the surrounding node is empty NOTE: empty means here, there can be hexagonal node present but that hexagonal node should not have any chip kept on it, i.e. stack length should be zero

func (*HexNode) HasRedChips

func (hn *HexNode) HasRedChips() bool

func (*HexNode) IsEmpty

func (hn *HexNode) IsEmpty() bool

func (*HexNode) SetChildNode

func (hn *HexNode) SetChildNode(edgeType EdgeType, node *HexNode)

type MatchResult

type MatchResult struct {
	WinningColor WinnerColor
	WinnerScore  int
	LoserScore   int
}

func (*MatchResult) GetLoserScore

func (mr *MatchResult) GetLoserScore() int

func (*MatchResult) GetWinnerColor

func (mr *MatchResult) GetWinnerColor() WinnerColor

func (*MatchResult) GetWinnerScore

func (mr *MatchResult) GetWinnerScore() int

type MoveResult

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

A type for returning the details of action taken on a player Move, The move could be played in both the phases PLACEMENT and MOVEMENT phase.

func GetMoveResult

func GetMoveResult(isGameOver, isActionSuccess bool,
	code ErrorCode, errM string, err error, phase GamePhase) MoveResult

func (*MoveResult) GetErrorCode

func (mr *MoveResult) GetErrorCode() ErrorCode

func (*MoveResult) GetErrorMessage

func (mr *MoveResult) GetErrorMessage() string

func (*MoveResult) GetNextPlayer

func (mr *MoveResult) GetNextPlayer() Player

func (*MoveResult) IsActionSuccess

func (mr *MoveResult) IsActionSuccess() bool

func (*MoveResult) IsGameOver

func (mr *MoveResult) IsGameOver() bool

func (*MoveResult) SetNextPlayer

func (mr *MoveResult) SetNextPlayer(player Player)

type Player

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

func GetPlayer

func GetPlayer(name, id string, playerColor ChipColor) Player

func (Player) GetId

func (p Player) GetId() string

func (Player) GetName

func (p Player) GetName() string

func (Player) GetPlayerColor

func (p Player) GetPlayerColor() ChipColor

type Set

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

func GetSet

func GetSet() *Set

func NewSet

func NewSet() *Set

func (*Set) Add

func (s *Set) Add(v interface{})

func (*Set) AddMulti

func (s *Set) AddMulti(list ...interface{})

AddMulti Add multiple values in the set

func (*Set) AddMultiS

func (s *Set) AddMultiS(list []string)

AddMulti Add multiple values in the set

func (*Set) Clear

func (s *Set) Clear()

func (*Set) Difference

func (s *Set) Difference(s2 *Set) *Set

Difference returns the subset from s, that doesn't exists in s2 (param)

func (*Set) DifferenceS

func (s *Set) DifferenceS(s2 *Set) []string

func (*Set) Filter

func (s *Set) Filter(P FilterFunc) *Set

Filter returns a subset, that contains only the values that satisfies the given predicate P

func (*Set) Has

func (s *Set) Has(v interface{}) bool

func (*Set) Intersect

func (s *Set) Intersect(s2 *Set) *Set

func (*Set) Remove

func (s *Set) Remove(v interface{})

func (*Set) Size

func (s *Set) Size() int

func (*Set) Union

func (s *Set) Union(s2 *Set) *Set

type WinnerColor

type WinnerColor string
const (
	WINNER_UNKNOWN WinnerColor = "UNKNOWN"
	WINNER_WHITE   WinnerColor = "WHITE"
	WINNER_BLACK   WinnerColor = "BLACK"
	WINNER_DRAW    WinnerColor = "DRAW"
)

func GetWinnerColorFromPlayerColor

func GetWinnerColorFromPlayerColor(color ChipColor) WinnerColor

Jump to

Keyboard shortcuts

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