get_disambiguator method
This function is used to uniquely identify ambiguous moves.
Implementation
String get_disambiguator(Move move) {
var moves = generate_moves();
var from = move.from;
var to = move.to;
var piece = move.piece;
var ambiguities = 0;
var same_rank = 0;
var same_file = 0;
for (var i = 0, len = moves.length; i < len; i++) {
var ambig_from = moves[i].from;
var ambig_to = moves[i].to;
var ambig_piece = moves[i].piece;
/* if a move of the same piece type ends on the same to square, we'll
* need to add a disambiguator to the algebraic notation
*/
if (piece == ambig_piece && from != ambig_from && to == ambig_to) {
ambiguities++;
if (rank(from) == rank(ambig_from)) {
same_rank++;
}
if (file(from) == file(ambig_from)) {
same_file++;
}
}
}
if (ambiguities > 0) {
/* if there exists a similar moving piece on the same rank and file as
* the move in question, use the square as the disambiguator
*/
if (same_rank > 0 && same_file > 0) {
return algebraic(from);
} /* if the moving piece rests on the same file, use the rank symbol as the
* disambiguator
*/
else if (same_file > 0) {
return algebraic(from)[1];
} /* else use the file symbol */
else {
return algebraic(from)[0];
}
}
return '';
}