Chessboard
Node.js module to control the logic of a chess board.
Require
var chess = require("chessboard");
Chess Instance
{
board: [Object],
figures: [Array],
rules: [Object],
Game: [Function]
}
This returns the general chess instance containing the Game() prototype and objects containing board, pieces and rules.
This three objects are loaded from external files and may be used by the Game() prototype to get a board, pieces or rules.
Before we see how the module works in general, let#s have a look at the structure of these three objects.
Board
It has keys from a-h that are containing an array with 8 items. Each item represents 'false' or the id of a pieces (Pieces are declared in the figures object).
If it's 'false', the field is empty.
board: {
a: [8, 0, false, false, false, false, 16, 24],
b: [10, 1, false, false, false, false, 17, 26],
c: [12, 2, false, false, false, false, 18, 28],
d: [15, 3, false, false, false, false, 19, 31],
e: [14, 4, false, false, false, false, 20, 30],
f: [13, 5, false, false, false, false, 21, 29],
g: [11, 6, false, false, false, false, 22, 27],
h: [9, 7, false, false, false, false, 23, 25]
}
Pieces
It's an array containing objects who describe the piece.
The index of an item represents it's id.
figures: [
{ type: "farmer", color: "black" },
{ type: "farmer", color: "black" },
{ type: "farmer", color: "black" },
{ type: "farmer", color: "black" },
{ type: "farmer", color: "black" },
{ type: "farmer", color: "black" },
{ type: "farmer", color: "black" },
{ type: "farmer", color: "black" },
{ type: "tower", color: "black" },
{ type: "tower", color: "black" },
{ type: "jumper", color: "black" },
{ type: "jumper", color: "black" },
{ type: "runner", color: "black" },
{ type: "runner", color: "black" },
{ type: "king", color: "black" },
{ type: "queen", color: "black" },
{ type: "farmer", color: "white" },
{ type: "farmer", color: "white" },
{ type: "farmer", color: "white" },
{ type: "farmer", color: "white" },
{ type: "farmer", color: "white" },
{ type: "farmer", color: "white" },
{ type: "farmer", color: "white" },
{ type: "farmer", color: "white" },
{ type: "tower", color: "white" },
{ type: "tower", color: "white" },
{ type: "jumper", color: "white" },
{ type: "jumper", color: "white" },
{ type: "runner", color: "white" },
{ type: "runner", color: "white" },
{ type: "king", color: "white" },
{ type: "queen", color: "white" },
{ type: "queen", color: "black" }
]
Rules
It's an object containing objects that describe the rules for a type of figures.
The rule definition for a type of figures has a few properties:
rules: {
farmer: {
directions: ["forward"],
ratio: [0],
max: 1,
special: ["checkUnused", "farmerHits"]
},
tower: {
directions: ["forward", "backward", "left", "right"],
ratio: [0],
max: false,
special: ["checkCastling"]
},
jumper: {
directions: ["forward", "backward", "left", "right"],
ratio: [0.5, 2],
max: 2,
jump: true
},
runner: {
directions: ["forward", "backward", "left", "right"],
ratio: [1],
max: false
},
king: {
directions: ["forward", "backward", "left", "right"],
ratio: [0, 1],
max: 1,
special: ["checkCastling"]
},
queen: {
directions: ["forward", "backward", "left", "right"],
ratio: [0, 1],
max: false
}
}
How they're interpreted?
Within the module, there is a function that checks a lot of things. For example, whether the direction is allowed, the maximum amount of steps is okay, the ratio is possible for this type of piece and whether the way is clear. (If jump is true, the way doesn't matters).
The next thing is the property special that contains an array with method names. This methods are individual methods to manage more complicated rules because it's not possible to explain such a complex rule like castling just in JSON.
Properties
{
directions: [Array],
ratio: [Array],
max: [Number],
jump: [Boolean],
special: [Array]
}
This is the connection between these three things (pieces, board & rules).
Create a Game
var game = new chess.Game();
A Game() contains it's own board, pieces and rules. If you don't pass custom ones, the Game() Function clones board, pieces and rules from the general chess instance.
Custom Board, Pieces or Rules
There are three ways to set custom board, pieces or rules:
- Change the board, pieces or rules within the general chess instance before you create a new Game()
- The instance of a Game() contains it's own board, pieces and rules (Mostly cloned from the general chess instance). You can change these things every time. There is no reinitialization required.
- Pass them as arguments to the Game() prototype function
Game Instance
The instance you get from Game() contains it's own board, pieces and rules.
There are also some methods to interact with the board or to print/export the chess board.
game: {
board: [Object],
figures: [Array],
rules: [Object],
specialRules: [Object],
print: [Function],
exportSVG: [Function],
move: [Function],
getTargets: [Function],
getAttacks: [Function]
}
Move
To move a piece on the board, use the move() method of a Game() Instance.
The first argument is an object containing options.
The from and to keys are required. Anything else wouldn't make any sense.
var myMove = game.move({
from: "d2",
to: "c4",
rules: true,
test: false
});
Move Return
{
success: true,
hit: { type: [String],
color: [String],
moves: [Number],
id: [Number],
row: [String],
line: [Number],
coords: [Array] } || false,
move: {
way: [Array],
step: [[Number], [Number]],
validation: {
obstacles: [Number],
direction: [String],
ratio: [Number],
max: [Boolean],
target: [Boolean]
},
from: [String],
to [String],
targets: [Array],
attacks: [Array]
}
}
{
success: false,
hit: false,
error: [Array]
}
getTargets()
To get possible targets for a piece, just run the getTargets() method of a game instance.
The only argument is the field on which the piece stands. 'a1' || 'd7'
var targets = game.getTargets("a2");
targets = [
{
field: [[String], [Number]],
coords: [[Number], [Number]],
hit: false || [Object]
},
{
...
}
]
getAttacks()
To get the possible attacks to a field, just run the getAttacks() method of a game instance.
The only argument is the field that could be attacked. 'a6' || 'f5'
var attacks = game.getAttacks("a6");
attacks = [
{
field: [[String], [Number]],
coords: [[Number], [Number]],
attacker: [Object]
},
{
...
}
]