API Docs for: 0.0.2
Show:

chess.model.Game Class

Module: Model

Chess game model

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:

appendMove

(
  • move
)
Boolean

Append move to the model

Parameters:

Returns:

Boolean: success

Example:

var model = new chess.model.Game();
model.appendMove({ 'from': 'e2', 'to' : 'e4' }); // Using objects
model.appendMove('e7'); // Using notation
alert(model.getCurrentPosition());

back

(
  • numberOfMoves
)
Boolean

Back up x number of moves

Parameters:

Returns:

canClaimDraw

() Boolean

Returns true if user can claim draw in current position

Returns:

Boolean: can claim draw

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
)
private

Delete moves from branch, i.e. main line or variation

Parameters:

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:

findMove

(
  • moveToFind
)
chess.model.Move

Parameters:

Returns:

fire

(
  • eventName
  • param
)
private

Parameters:

getAllFens

() Array

Returns array of all FEN's in main line(Not variations)

Returns:

getAllNextMoves

(
  • nextOf
)
Array

Returns array of remaining moves

Parameters:

Returns:

getBranch

(
  • move
)
Array private

Return branch/line of current move, i.e. main line or variation

Parameters:

Returns:

getColorToMove

() String

Return color to move, "white" or "black"

Returns:

getCommentAfter

(
  • move
)
String

Get comment of current move

Parameters:

Returns:

String: comment

getCommentBefore

(
  • move
)
String

Return comment before move, i.e. get comment of previous move

Parameters:

Returns:

String: comment

getCurrentBranch

() Array

Return branch, i.e. main line or variation of current move

Returns:

getCurrentMove

() chess.model.Move

Returns current move, i.e. last played move

Returns:

getCurrentPosition

() String

Returns fen of current move or start of game fen

Returns:

getDatabaseId

() Number

Return database id of game

Returns:

getDuplicateVariationMove

(
  • move
)
chess.model.Move | Undefined

Returns true if current move already has a variation starting with given move

Parameters:

Returns:

chess.model.Move | Undefined:

getFirstMoveInGame

() chess.model.Move

Return first move in game

Returns:

getId

() String

Returns game id

Returns:

getLastMoveInGame

() chess.model.Move | Undefined

Return last move in game

Returns:

chess.model.Move | Undefined: move

getLastPositionInGame

() String

Return last position in game

Returns:

String: fen

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
)
String

Return a specific metadata key

Parameters:

Returns:

String: value

Example:

var whitePlayer = model.getMetadataValue('white');

getModel

() Object private

Return game data

Returns:

getMoves

() Array

Return array of moves in game

Returns:

getNextMove

(
  • nextOf
)
chess.model.Move | Undefined

Get next move of

Parameters:

Returns:

chess.model.Move | Undefined: next move

getParentMove

(
  • move
)
chess.model.Move | Undefined

Return parent move of given move, i.e. parent move of a move in a variation.

Parameters:

Returns:

chess.model.Move | Undefined:

getPreviousMove

(
  • move
  • includeComments
)
chess.model.Move | Undefined

Returns previous move in same branch or parent branch

Parameters:

Returns:

chess.model.Move | Undefined:

getPreviousMoveInBranch

(
  • move
)
chess.model.Move | Undefined private

Returns previous move in same branch/line or undefined

Parameters:

Returns:

chess.model.Move | Undefined:

getPreviousPosition

() String

Returns fen of previous move or start of game fen

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:

getStartPosition

() String

Return start position of game

Returns:

String: position

getValidMetadata

()

Move metadata into metadata object

getValidMove

(
  • move
  • pos
)
chess.model.Move private

Returns valid chess.model.Move object for a move

Parameters:

Returns:

goToMove

(
  • move
)

Go to a specific move.

Parameters:

gradeMove

(
  • move
  • grade
)

Grade a move

Parameters:

Example:

    model.gradeMove(model.getCurrentMove(), '!');
    ...
    ...
    model.gradeMove(model.getCurrentMove(), '??');

hasCurrentMove

() Boolean

Returns rue if current move is set

Returns:

hasMovesInBranch

(
  • branch
)
Boolean private

Returns true if there are moves left in branch

Parameters:

Returns:

insertSpacerInBranch

(
  • branch
  • atIndex
)

Insert space for new move in a branch at index

Parameters:

isAtEndOfGame

() Boolean

true if move displayed on board, i.e. current model move is last move in game.

Returns:

isChessMove

(
  • move
)
Boolean private

Returns true if passed move is a valid chess move

Parameters:

Returns:

isCorrectGuess

(
  • guess
  • nextMove
)
Boolean private

Returns true if passed guess matches next move

Parameters:

Returns:

isDirty

() Boolean

Return dirty flag. dirty flag is set to true when game has been changed, but not saved.

Returns:

isDuplicateVariationMove

(
  • move
)
Boolean

Returns true when trying to create variation and passed move is next move in line

Parameters:

Returns:

isInAutoPlayMode

() Boolean

Returns true if in auto play mode

Returns:

isLastMoveInVariation

(
  • move
)
Boolean

true if given move is last move in current variation, i.e. the variation active on the board

Parameters:

Returns:

isModelFor

(
  • game
)

Returns true if this model is model for given game object

Parameters:

loadGame

(
  • gameId
)

Load a game from server

Parameters:

loadRandomGame

(
  • databaseId
)

Load a random game from selected database

Parameters:

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:

Returns:

undefined

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:

populate

(
  • gameData
)
private

Populate game model by JSON game object. This method will create a new game.

Parameters:

previousMove

()

Go to previous move

registerBranchMap

(
  • move
  • branch
)
private

Store internal link between move and a branch of moves(Main line or variation)

Parameters:

registerMove

(
  • move
  • atIndex
)
private

Internally index a move

Parameters:

registerMoves

(
  • moves
  • pos
  • parent
)
private

Parse and index moves received from the server, i.e. the populate method

Parameters:

registerParentMap

(
  • move
  • parent
)
private

Store internal reference to parent move

Parameters:

registerPreviousMap

(
  • move
  • previous
)
private

Store internal reference to previous move

Parameters:

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:

setCommentAfter

(
  • comment
  • move
)

Set comment after a move

Parameters:

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:

setCurrentMove

(
  • newCurrentMove
)
Boolean private

Parameters:

Returns:

Boolean: success

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:

Example:

model.setMetadata({white:'John','black:'Jane'});

setMetadataValue

(
  • key
  • value
)

Update particular info about the game

Parameters:

Example:

model.setMetadataValue('white','John');

setPosition

(
  • fen
)

Create new game from given fen position

Parameters:

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
)
Object private

Convert to valid server model, i.e. reserved metadata moved from metadata object

Parameters:

Returns:

tryNextMove

(
  • move
)
Boolean

Try to guess next move in a game

Parameters:

Returns:

Boolean: correctMove

Example:

    var correctMove = model.tryNextMove({
        from:'e7',
        to:'e8',
        promoteTo:'queen'
    });

updateGameFromServer

(
  • data
)
private

Receive game update from server

Parameters:

Attributes

id

Number optional

id of initial game to load from server.

Default: undefined

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 EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

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:

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:

dirty

Event fired when model is clean, i.e. right after being saved to the server.

Event Payload:

dirty

Event fired when model is changed but not saved

Event Payload:

endOfBranch

Fired when current move is last move in game.

Event Payload:

endOfBranch

Fired when you try to append an invalid move to the game

Event Payload:

endOfBranch

Fired when current move is last move in branch, either last move in game or last move inside a variation.

Event Payload:

newGame

new game event. Fired when a new model is created or an old model is activated

Event Payload:

newMove

Fired when a new move is appended to the game.

Event Payload:

newVariation

Fired after creating a new variation

Event Payload:

noMoves

Fired when there are no moves in the game

Event Payload:

notEndOfGame

Fired when going to a move which is not last move in game

Event Payload:

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:

setPosition

Fired when current chess position is changed, example by moving to a different move

Event Payload:

updateMetadata

Fired when metadata is updated

Event Payload:

verifyPromotion

verify promotion event. This event is fired when you try to append a promotion move where the promoteTo info is missing

Event Payload: