(abstract) new Rule()
Rules define specific characteristics of each variant of the game.
All rules should inherit from this base class and define the following
properties:
- Maximum number of points (usually 26 - 24 on board, one on bar, and one outside)
- Maximum number of checkers (usually 15 per player/colour)
and methods for:
- Rolling dice (and a list of moves the player has to make
after drawing a specific dice)
- Reset state to initial position of pieces.
- Validating a move
- Checking if game has ended
- Checking which player won
- Checking how much points the player won
- Source:
Members
country :string
Full name of country where this rule (variant) is played.
To list multiple countries use a pipe ('|') character as separator.
Type:
- string
- Source:
countryCode :string
Two character ISO code of country where this rule (variant) is played.
To list multiple codes use a pipe ('|') character as separator.
List codes in same order as countries in the field above.
Type:
- string
- Source:
description :string
Full description of rule
Type:
- string
- Source:
maxPieces :number
The number of pieces each player has
Type:
- number
- Source:
name :string
Rule name, matching the class name (eg. 'RuleBgCasual')
Type:
- string
- Source:
title :string
Short title describing rule specifics
Type:
- string
- Source:
Methods
allPiecesAreHome(state, type) → {boolean}
Check if all pieces are in the home field.
Parameters:
Name | Type | Description |
---|---|---|
state |
State | State to change |
type |
PieceType | Type of piece (white/black) |
- Source:
Returns:
- True if all pieces are in home field
- Type
- boolean
(abstract) applyMoveActions(state, actionList)
Call this method to apply a list of actions to a game state.
Actions depend on rule and usually are `move`, `hit`, `recover` or `bear`.
Parameters:
Name | Type | Description |
---|---|---|
state |
State | State to change |
actionList |
Array.<MoveAction> | List of action to apply. |
- Source:
bear(state, piece)
Bear piece - remove from board and place outside
Parameters:
Name | Type | Description |
---|---|---|
state |
State | Board state |
piece |
Piece | Piece to bear |
- Source:
Throws:
Throws an error if there is no piece at fromPos or piece is of wrong type
calculateMoveWeights(state, movesLeft, pieceType, rootPiece, stopAtMax) → {Object}
Recursively try out all combinations for the specified player (by piece type)
and check the "weight" of each combination. Weight is the sum of all piece
movements for a particular combination of moves (called "branch")
Parameters:
Name | Type | Description |
---|---|---|
state |
State | State |
movesLeft |
Array.<number> | Move values left |
pieceType |
PieceType | Type of piece (white/black) |
rootPiece |
Piece | Check only branches starting from a specific piece |
stopAtMax |
boolean | Stop calculation if a branch that allows all moves to be played has been found. |
- Source:
Returns:
- Map containing maximum weight for each branch, indexed by piece ID
and total maximum weight for all branches, accessed with 'max' index
- Type
- Object
countAtHigherPos(state, position, type) → {number}
Count pieces of specified type at higher positions
Parameters:
Name | Type | Description |
---|---|---|
state |
State | State to check |
position |
number | Normalized position (inclusive) |
type |
PieceType | Type of piece (white/black) |
- Source:
Returns:
- Number of pieces at positions higher than position
- Type
- number
(abstract) getGameScore(state, player) → {boolean}
Check game state and determine how much points the player
should be awared for this state.
If opponent player has not borne any pieces, award 2 points.
If opponent has not borne any pieces, and still has pieces in home field of player, award 3 points.
In all other cases award 1 point.
Parameters:
Name | Type | Description |
---|---|---|
state |
State | Game state |
player |
Player | Player |
- Source:
Returns:
- True if player has won the game
- Type
- boolean
(abstract) getMoveActions(state, piece, steps) → {Array.<MoveAction>}
Call this method after a request for moving a piece has been made.
Determines if the move is allowed and what actions will have to be made as
a result. Actions can be `move`, `hit`, `recover` or `bear`.
If move is allowed or not depends on the current state of the game. For example,
if the player has pieces on the bar, they will only be allowed to recover pieces.
Multiple actions can be returned, if required. Placing (or moving) a piece over
an opponent's blot will result in two actions: `hit` first, then `recover` (or `move`).
The list of actions returned would usually be appllied to game state and then
sent to client. The client's UI would play the actions (eg. with movement animation)
in the same order.
Parameters:
Name | Type | Description |
---|---|---|
state |
State | State to change |
piece |
Piece | Piece to move |
steps |
number | Number of steps to increment towards first home position |
- Source:
- See:
-
- RuleBgCasual.getMoveActions for an example on how to implement this method
Returns:
- List of actions if move is allowed, empty list otherwise.
- Type
- Array.<MoveAction>
(abstract) hasWon(state, player) → {boolean}
Check game state and determine if the specified player has won the game.
This method assumes that the specified player was on turn and has played
and confirmed his move.
Parameters:
Name | Type | Description |
---|---|---|
state |
State | Game state |
player |
Player | Player |
- Source:
Returns:
- True if player has won the game
- Type
- boolean
havePiecesOnBar(state, type) → {boolean}
Check if there are any pieces on the bar.
Parameters:
Name | Type | Description |
---|---|---|
state |
State | State to check |
type |
PieceType | Type of piece (white/black) |
- Source:
Returns:
- True if there are any pieces on the bar
- Type
- boolean
hit(state, piece)
Hit piece - send piece to bar
Parameters:
Name | Type | Description |
---|---|---|
state |
State | Board state |
piece |
Piece | Piece to hit |
- Source:
Throws:
Throws an error if there is no piece at fromPos or piece is of wrong type
(abstract) incPos(position, type, steps) → {number}
Increment position by specified number of steps and return an incremented position
Parameters:
Name | Type | Description |
---|---|---|
position |
number | Position |
type |
PieceType | Type of piece |
steps |
number | Number of steps to increment towards first home position |
- Source:
Returns:
Incremented position (denormalized)
- Type
- number
initialize(state)
Initialize state.
Parameters:
Name | Type | Description |
---|---|---|
state |
State | Board state to initialize |
- Source:
isMoveActionRestricted(state, movesLeft, piece, steps) → {boolean}
Checks if a specific move action is restricted. If there are any move combinations
that would allow the player to use all dice values, then the player is obliged to
use one of those combination. If playing both dice values are not possible and the
player should choose which one of them to play, they must play the higher value
(as long as it is possible).
Parameters:
Name | Type | Description |
---|---|---|
state |
State | State |
movesLeft |
Array.<number> | Move values left |
piece |
Piece | Piece to move |
steps |
number | Number of steps to move |
- Source:
Returns:
- Returns true if move is restricted (not allowed).
- Type
- boolean
(abstract) markAsPlayed(game, move)
Mark move as played
Parameters:
Name | Type | Description |
---|---|---|
game |
Game | Game |
move |
number | Move (number of steps) |
- Source:
move(state, piece, toPos)
Move piece to specified point, without enforcing any rules or performing any validation.
Parameters:
Name | Type | Description |
---|---|---|
state |
State | Board state |
piece |
Piece | Piece to move |
toPos |
number | Position to which to move the piece to |
- Source:
Throws:
Throws an error if there is no piece at fromPos or piece is of wrong type
(abstract) nextTurn(match)
Proceed to next turn.
Start next turn:
1. Reset turn
2. Change players
3. Roll new dice
Parameters:
Name | Type | Description |
---|---|---|
match |
Match | Match |
- Source:
place(state, number, type, position)
Place one or more pieces from player set to board point.
Parameters:
Name | Type | Description |
---|---|---|
state |
State | Board state |
number |
number | Number of pieces to place |
type |
PieceType | Type of pieces to place |
position |
number | Position at which to place piece(s) |
- Source:
recover(state, piece, position)
Recover piece - place from bar to board
Parameters:
Name | Type | Description |
---|---|---|
state |
State | Board state |
piece |
Piece | Piece to recover |
position |
number | Position to which to place the piece |
- Source:
Throws:
Throws an error if there is no piece at fromPos or piece is of wrong type
(abstract) resetState(state)
Reset state to initial position of pieces according to current rule.
Parameters:
Name | Type | Description |
---|---|---|
state |
State | Board state |
- Source:
rollDice(game, valuesopt) → {Dice}
Roll dice and generate list of moves the player has to make according to
current rules.
Descendants rules would normally override this method in order to properly determine
the allowed move values that correspond to a specific die combination (eg. doubles).
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
game |
Game | Game object. Used to check if it is the first turn of the game. | |
values |
Array.<number> |
<optional> |
Optional parameter containing the dice values to use, instead of generating random values. Used by some rules as RuleBgGulbara. |
- Source:
Returns:
- Dice object containing random values and allowed moves
- Type
- Dice
validateConfirm(game, player) → {boolean}
Validate confirmation of moves.
This is the base method for validation of moves that make a few general
checks like:
- Is the game started and is finished?
- Is it player's turn?
- Was dice rolled?
- Are all moves played?
- Not confirmed already?
Descendant rules must extend this method and add additional validation checks
according to the rule specifics.
Parameters:
Name | Type | Description |
---|---|---|
game |
Game | Game |
player |
Player | Player |
- Source:
Returns:
True if confirmation is allowed
- Type
- boolean
validateMove(game, player, piece, steps) → {boolean}
Validate piece move.
This is the base method for validation of moves that make a few general
checks like:
- Is the game started and is finished?
- Is it player's turn?
- Was dice rolled?
- Are moves with values equal to the steps left?
Descendant rules must extend this method and add additional validation checks
according to the rule specifics.
Parameters:
Name | Type | Description |
---|---|---|
game |
Game | Game |
player |
Player | Player requesting move |
piece |
Piece | Piece to move |
steps |
number | Number of steps to make forward to the first home position |
- Source:
Returns:
True if move is valid and should be allowed.
- Type
- boolean
validateTurn(game, player) → {boolean}
Validate player's turn.
This is the base method for validation of moves that make a few general
checks like:
- Is the game started and is finished?
- Is it player's turn?
Parameters:
Name | Type | Description |
---|---|---|
game |
Game | Game |
player |
Player | Player |
- Source:
Returns:
True if move is valid and should be allowed.
- Type
- boolean
validateUndo(game, player) → {boolean}
Validate request for undoing moves.
This is the base method for validation of undo that make a few general
checks like:
- Is the game started and is finished?
- Is it player's turn?
- Was dice rolled?
- Are all moves played?
- Not confirmed already?
Descendant rules can extend this method and add additional validation checks
according to the rule specifics.
Parameters:
Name | Type | Description |
---|---|---|
game |
Game | Game |
player |
Player | Player |
- Source:
Returns:
True if confirmation is allowed
- Type
- boolean