chess.model.Game Class
Chess game model
Item Index
Methods
- activate
- addAction
- appendMove
- back
- canClaimDraw
- clearCurrentMove
- clearMovesInBranch
- deleteMove
- findMove
- fire
- getAllFens
- getAllNextMoves
- getBranch
- getColorToMove
- getCommentAfter
- getCommentBefore
- getCurrentBranch
- getCurrentMove
- getCurrentPosition
- getDatabaseId
- getDuplicateVariationMove
- getFirstMoveInGame
- getId
- getLastMoveInGame
- getLastPositionInGame
- getMetadata
- getMetadataValue
- getModel
- getMoves
- getNextMove
- getParentMove
- getPreviousMove
- getPreviousMoveInBranch
- getPreviousPosition
- getResult
- getStartPosition
- getValidMetadata
- getValidMove
- goToMove
- gradeMove
- hasCurrentMove
- hasMovesInBranch
- insertSpacerInBranch
- isAtEndOfGame
- isChessMove
- isCorrectGuess
- isDirty
- isDuplicateVariationMove
- isInAutoPlayMode
- isLastMoveInVariation
- isModelFor
- loadGame
- loadRandomGame
- newGame
- newVariation
- newVariationBranch
- nextAutoPlayMove
- nextMove
- overwriteMove
- populate
- previousMove
- registerBranchMap
- registerMove
- registerMoves
- registerParentMap
- registerPreviousMap
- resetPosition
- save
- setClean
- setComment
- setCommentAfter
- setCommentBefore
- setCurrentMove
- setDefaultModel
- setDirty
- setMetadata
- setMetadataValue
- setPosition
- startAutoPlay
- startAutoPlay
- toEnd
- toEndOfCurrentBranch
- toStart
- toValidServerModel
- tryNextMove
- updateGameFromServer
Attributes
Methods
activate
()
Activate model. This will fire newGame and setPosition events
addAction
-
action
Add action as a move. Actions are not fully implemented. When implemented, it will add supports for interactive chess games, example: start and stop autoplay. Display comments, videos or audio etc.
Parameters:
-
action
chess.model.Move
appendMove
-
move
Append move to the model
Parameters:
-
move
chess.model.Move | String
Returns:
Example:
var model = new chess.model.Game();
model.appendMove({ 'from': 'e2', 'to' : 'e4' }); // Using objects
model.appendMove('e7'); // Using notation
alert(model.getCurrentPosition());
canClaimDraw
()
Boolean
Returns true if user can claim draw in current position
Returns:
clearCurrentMove
()
private
Delete current move reference. This method is called when creating a new game and when first move in the game is deleted
clearMovesInBranch
-
branch
-
fromIndex
Delete moves from branch, i.e. main line or variation
deleteMove
-
moveToDelete
Delete a move. This method will fire the deleteMove and endOfBranch events. If deleted move is in main line, the endOfGame event will also be fired. The event "noMoves" will be fired if the deleted move is the first move in the game. "deleteVariation" will be fired if the deleted move is the first move in a variation.
Parameters:
-
moveToDelete
chess.model.Move
getAllNextMoves
-
nextOf
Returns array of remaining moves
Parameters:
-
nextOf
chess.model.Move
Returns:
getBranch
-
move
Return branch/line of current move, i.e. main line or variation
Parameters:
-
move
chess.model.Move
Returns:
getCommentAfter
-
move
Get comment of current move
Parameters:
-
move
chess.model.Move
Returns:
getCommentBefore
-
move
Return comment before move, i.e. get comment of previous move
Parameters:
-
move
chess.model.Move
Returns:
getDuplicateVariationMove
-
move
Returns true if current move already has a variation starting with given move
Parameters:
-
move
chess.model.Move
Returns:
getLastMoveInGame
()
chess.model.Move | Undefined
Return last move in game
Returns:
getMetadata
()
Object
Return all game metadata info
Returns:
Example:
var m = model.getMetadata();
returns an object like
{ "white": "Magnus Carlsen", "black": "Levon Aronian", "Result" : "1-0" }
getMetadataValue
-
key
Return a specific metadata key
Parameters:
-
key
String
Returns:
Example:
var whitePlayer = model.getMetadataValue('white');
getNextMove
-
nextOf
Get next move of
Parameters:
-
nextOf
chess.model.Move
Returns:
getParentMove
-
move
Return parent move of given move, i.e. parent move of a move in a variation.
Parameters:
-
move
chess.model.Move | Object
Returns:
getPreviousMove
-
move
-
includeComments
Returns previous move in same branch or parent branch
Parameters:
-
move
chess.model.Move -
includeComments
Boolean
Returns:
getPreviousMoveInBranch
-
move
Returns previous move in same branch/line or undefined
Parameters:
-
move
chess.model.Move
Returns:
getResult
()
Number
Return result of game, either from metadata("result") or by trying to calculate final position. Return value will be 1 for white win, -1 for black win. 0.5 for draw and 0 for undecided.
Returns:
getValidMetadata
()
Move metadata into metadata object
getValidMove
-
move
-
pos
Returns valid chess.model.Move object for a move
Parameters:
-
move
Object | chess.model.Move -
pos
String
Returns:
gradeMove
-
move
-
grade
Grade a move
Example:
model.gradeMove(model.getCurrentMove(), '!');
...
...
model.gradeMove(model.getCurrentMove(), '??');
hasMovesInBranch
-
branch
Returns true if there are moves left in branch
Parameters:
-
branch
Array
Returns:
insertSpacerInBranch
-
branch
-
atIndex
Insert space for new move in a branch at index
isAtEndOfGame
()
Boolean
true if move displayed on board, i.e. current model move is last move in game.
Returns:
isChessMove
-
move
Returns true if passed move is a valid chess move
Parameters:
-
move
Object
Returns:
isCorrectGuess
-
guess
-
nextMove
Returns true if passed guess matches next move
Returns:
isDirty
()
Boolean
Return dirty flag. dirty flag is set to true when game has been changed, but not saved.
Returns:
isDuplicateVariationMove
-
move
Returns true when trying to create variation and passed move is next move in line
Parameters:
-
move
chess.model.Move
Returns:
isLastMoveInVariation
-
move
true if given move is last move in current variation, i.e. the variation active on the board
Parameters:
-
move
chess.model.Move
Returns:
isModelFor
-
game
Returns true if this model is model for given game object
Parameters:
-
game
Object
loadRandomGame
-
databaseId
Load a random game from selected database
Parameters:
-
databaseId
Number
newGame
()
Empty model and reset to standard position
newVariation
-
move
Add a new move as a variation. If current move is already first move in variation it will go to this move and not create a new variation. This method will fire the events "newVariation", "newMove" and "endOfBranch" on success. "invalidMove" will be fired on invalid move.
Parameters:
-
move
chess.model.Move
Returns:
newVariationBranch
()
private
Create new variation branch
nextAutoPlayMove
()
private
Auto play next move
nextMove
()
Go to next move
overwriteMove
-
oldMove
-
newMove
Overwrite a move with a different move. oldMove has to be a move in the game. When found, this move and all following move will be deleted and the new move will be appended.
Parameters:
-
oldMove
chess.model.Move -
newMove
chess.model.Move
populate
-
gameData
Populate game model by JSON game object. This method will create a new game.
Parameters:
-
gameData
Object
previousMove
()
Go to previous move
registerBranchMap
-
move
-
branch
Store internal link between move and a branch of moves(Main line or variation)
Parameters:
-
move
chess.model.Move -
branch
Object
registerMove
-
move
-
atIndex
Internally index a move
Parameters:
-
move
chess.model.Move -
atIndex
Number
registerMoves
-
moves
-
pos
-
parent
Parse and index moves received from the server, i.e. the populate method
Parameters:
-
moves
Object -
pos
String -
parent
chess.model.Move
registerParentMap
-
move
-
parent
Store internal reference to parent move
Parameters:
-
move
chess.model.Move -
parent
chess.model.Move
registerPreviousMap
-
move
-
previous
Store internal reference to previous move
Parameters:
-
move
chess.model.Move -
previous
chess.model.Move
resetPosition
()
Call goToMove for current move and trigger the events. This method is called when overwrite of move is cancelled from game editor and when you're guessing the wrong move in a tactic puzzle
save
()
Save model to server
setClean
()
private
Set dirty flag to false, i.e. game has been changed and saved
setComment
-
move
-
comment
Set comment property of a move
Parameters:
-
move
chess.model.Move -
comment
String
setCommentAfter
-
comment
-
move
Set comment after a move
Parameters:
-
comment
String -
move
chess.model.Move
setCommentBefore
-
comment
-
move
Set comment before a move, i.e. set comment of previous move, or in case of first move in game, set "commment" attribute of game metadata.
Parameters:
-
comment
String -
move
chess.model.Move
setCurrentMove
-
newCurrentMove
Parameters:
-
newCurrentMove
chess.model.Move
Returns:
setDefaultModel
()
Reset model data to default, blank game
setDirty
()
private
Set dirty flag to true, i.e. game has been changed but not saved.
setMetadata
-
metadata
Update game information
Parameters:
-
metadata
Object
Example:
model.setMetadata({white:'John','black:'Jane'});
setMetadataValue
-
key
-
value
Update particular info about the game
Example:
model.setMetadataValue('white','John');
startAutoPlay
()
Start auto play of moves
startAutoPlay
()
Stop auto play of moves
toEnd
()
Go to last move in game
toEndOfCurrentBranch
()
Go to last move in current branch, i.e. main line or variation
toStart
()
Go to start of game
toValidServerModel
-
gameData
Convert to valid server model, i.e. reserved metadata moved from metadata object
Parameters:
-
gameData
Object
Returns:
Attributes
moveParser
chess.parser.FenParser0x88
Fires event moveParserChange
Fires when the value for the configuration attribute moveParser
is
changed. You can listen for the event using the on
method if you
wish to be notified before the attribute's value has changed, or
using the after
method if you wish to be notified after the
attribute's value has changed.
Parameters:
-
e
EventFacadeAn Event Facade object with the following attribute-specific properties added:
Events
deleteAction
Special event not yet implemented. Supporting for adding info to games such as video links, automatic start and stop of auto play for lecture purpose will be added as actions later.
Event Payload:
-
eventName
String -
model
chess.model.Game -
move
chess.model.Move
deleteMove
Fired when a move is deleted. It will only be fired for one move and not the following moves which of course also will be deleted.
Event Payload:
-
eventName
String -
model
chess.model.Game -
deleted
chess.model.Movemove
dirty
Event fired when model is clean, i.e. right after being saved to the server.
Event Payload:
-
this
chess.model.Game
endOfBranch
Fired when current move is last move in game.
Event Payload:
-
eventName
String -
model
chess.model.Game
endOfBranch
Fired when you try to append an invalid move to the game
Event Payload:
-
eventName
String -
model
chess.model.Game -
move
chess.model.Move
endOfBranch
Fired when current move is last move in branch, either last move in game or last move inside a variation.
Event Payload:
-
eventName
String -
model
chess.model.Game
newGame
new game event. Fired when a new model is created or an old model is activated
Event Payload:
-
eventName
String -
model
chess.model.Game
newMove
Fired when a new move is appended to the game.
Event Payload:
-
eventName
String -
model
chess.model.Game -
appendedMove
chess.model.Move
newVariation
Fired after creating a new variation
Event Payload:
-
eventName
String -
model
chess.model.Game -
parentMove
chess.model.Move
noMoves
Fired when there are no moves in the game
Event Payload:
-
eventName
String -
model
chess.model.Game
notEndOfGame
Fired when going to a move which is not last move in game
Event Payload:
-
eventName
String -
model
chess.model.Game
overwriteOrVariation
Fired when appending a move in the middle of a game. This method sends a message to the controller saying that it needs to know if appended move should be added as variation or if it should overwrite current next move
Event Payload:
-
eventName
String -
model
chess.model.Game -
newMove,
ObjectnextMove
setPosition
Fired when current chess position is changed, example by moving to a different move
Event Payload:
-
eventName
String -
model
chess.model.Game
updateMetadata
Fired when metadata is updated
Event Payload:
-
eventName
String -
model
chess.model.Game -
metadata,
Objectexample {key:'white','value':'John'}
verifyPromotion
verify promotion event. This event is fired when you try to append a promotion move where the promoteTo info is missing
Event Payload:
-
eventName
String -
model
chess.model.Game -
appendedMove
chess.model.Move