jhlywa/chess.js — pull request #59 by yo35 (2014-02-19 00:33:20+00:00)
Title: Fix issue #58 ('Fooling check detection')

This patch aims at solving issue #58.

The original problem concerns the internal variables `kings['w']` and `kings['b']`, that normally point to the squares where the white and black kings are located. In some scenarios where - at some point - two or more kings of a given color are present on the board, these variables are not properly updated.

The proposed solution consists in adding two counters (`kings['w_count']` and `kings['b_count']`) to track the number of kings (per color) present on the board. King position lookup is performed by the new function `get_sanitized_king_square(color)`, which returns an invalid square index if the corresponding counter is not equal to 1, and updates and returns the corresponding square pointer otherwise.

It can be noted that, in scenarios where the number of kings per color is always `< 2`, the potentially expensive loop in `get_sanitized_king_square(color)` is never run. Thus, the current implementation might be as fast as the original one in common situations.