Documentation ¶
Index ¶
- func Clone(a, b interface{})
- func DeepCopy(a, b interface{})
- type Chip
- type ChipColor
- type DvonnBoard
- func (db *DvonnBoard) DeFill(id string)
- func (db *DvonnBoard) Fill(id string, chips []Chip)
- func (db *DvonnBoard) GetBoardSnapshot()
- func (db *DvonnBoard) GetCellIdsByStackColor(color ChipColor) []string
- func (db *DvonnBoard) GetCells() map[string]*HexNode
- func (db *DvonnBoard) GetCountOfPiecesControlledBy(color ChipColor) int
- func (db *DvonnBoard) GetDisconnectedCells() []string
- func (db *DvonnBoard) GetPossibleMoveFor(id string) []*HexNode
- func (db *DvonnBoard) GetRedChipsIds() []string
- func (db *DvonnBoard) IsCellEmpty(id string) bool
- func (db *DvonnBoard) IsPlaceVacant(placeId string) bool
- func (db *DvonnBoard) RemoveDisconnectedCells()
- type DvonnGame
- func (dg *DvonnGame) AddPlayer(player Player)
- func (dg *DvonnGame) GetBoard() *DvonnBoard
- func (dg *DvonnGame) GetCurrentPlayer() Player
- func (dg *DvonnGame) GetGamePhase() GamePhase
- func (dg *DvonnGame) GetGameWinner() (*MatchResult, error)
- func (dg *DvonnGame) GetNextTurnPlayer() (Player, error)
- func (dg *DvonnGame) GetPlayerByColor(color ChipColor) Player
- func (dg *DvonnGame) GetPlayers() []Player
- func (dg *DvonnGame) HasNextPlayerAValidMove(currentPlayer Player) bool
- func (dg *DvonnGame) IsGameOver() bool
- func (dg *DvonnGame) IsValidMoveLeftForPlayer(playerColor ChipColor) bool
- func (dg *DvonnGame) Move(player Player, paths ...string) MoveResult
- func (dg *DvonnGame) Redo() bool
- func (dg *DvonnGame) RemovePlayer(playerId string)
- func (dg *DvonnGame) Undo() (*DvonnGame, bool)
- type EdgeType
- type ErrorCode
- type FilterFunc
- type GamePhase
- type HexNode
- func (hn *HexNode) AddChips(chips []Chip)
- func (hn *HexNode) Empty()
- func (hn *HexNode) GetChildNodes() map[EdgeType]*HexNode
- func (hn *HexNode) GetChipsStack() []Chip
- func (hn *HexNode) GetIdentifier() string
- func (hn *HexNode) GetPossibleMovements()
- func (hn *HexNode) GetStackLength() int
- func (hn *HexNode) GetStraightAdjacentOnLevel(level int) []*HexNode
- func (hn *HexNode) GetTopChips() (Chip, error)
- func (hn *HexNode) HasFreeEdge() bool
- func (hn *HexNode) HasRedChips() bool
- func (hn *HexNode) IsEmpty() bool
- func (hn *HexNode) SetChildNode(edgeType EdgeType, node *HexNode)
- type MatchResult
- type MoveResult
- type Player
- type Set
- func (s *Set) Add(v interface{})
- func (s *Set) AddMulti(list ...interface{})
- func (s *Set) AddMultiS(list []string)
- func (s *Set) Clear()
- func (s *Set) Difference(s2 *Set) *Set
- func (s *Set) DifferenceS(s2 *Set) []string
- func (s *Set) Filter(P FilterFunc) *Set
- func (s *Set) Has(v interface{}) bool
- func (s *Set) Intersect(s2 *Set) *Set
- func (s *Set) Remove(v interface{})
- func (s *Set) Size() int
- func (s *Set) Union(s2 *Set) *Set
- type WinnerColor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
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 (*DvonnGame) AddPlayer ¶
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 ¶
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 (*DvonnGame) GetGameWinner ¶
func (dg *DvonnGame) GetGameWinner() (*MatchResult, error)
Returns winner color
func (*DvonnGame) GetNextTurnPlayer ¶
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 (*DvonnGame) GetPlayers ¶
func (*DvonnGame) HasNextPlayerAValidMove ¶
tells if the other player has some valid move left to play or not
func (*DvonnGame) IsGameOver ¶
returns false if any one of the player has any move left to play in MOVEMENT phase, else return true
func (*DvonnGame) IsValidMoveLeftForPlayer ¶
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 ¶
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 ¶
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 ¶
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 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 HexNode ¶
type HexNode struct {
// contains filtered or unexported fields
}
func GetHexNode ¶
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 (*HexNode) GetChipsStack ¶
func (*HexNode) GetIdentifier ¶
func (*HexNode) GetPossibleMovements ¶
func (hn *HexNode) GetPossibleMovements()
func (*HexNode) GetStackLength ¶
func (*HexNode) GetStraightAdjacentOnLevel ¶
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 (*HexNode) HasFreeEdge ¶
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 (*HexNode) SetChildNode ¶
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 (*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 (Player) GetPlayerColor ¶
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
func (*Set) AddMulti ¶
func (s *Set) AddMulti(list ...interface{})
AddMulti Add multiple values in the set
func (*Set) Difference ¶
Difference returns the subset from s, that doesn't exists in s2 (param)
func (*Set) DifferenceS ¶
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
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