dovahkiin
dovahkiin

Reputation: 718

Chess.js library. Determine winner and loser

Using the chess.js library (see this link), I can know if a chess game is finished, by using game_over() function.

But how do I know who won and who lost?

Upvotes: 1

Views: 1872

Answers (3)

Mat Forsberg
Mat Forsberg

Reputation: 454

You would check

chess.in_checkmate();

first to see if the other side put this side in check on their last turn. If not and game over then it is a draw.

Upvotes: 1

Eduardo
Eduardo

Reputation: 8392

According to the docs: game_over "Returns true if the game has ended via checkmate, stalemate, draw, threefold repetition, or insufficient material". So it may have been a draw too.

I would check:

  1. if the game ended in checkmate (there is a function for that), and see who moved last => winner
  2. otherwise it is a draw

Keep in mind, though, that a chess game can also be won by the other party resigning, or running out of time on their clock.

Upvotes: 1

Ilya
Ilya

Reputation: 5557

in_checkmate()

Returns true or false if the side to move has been checkmated.

Combined with turn(), it gives you the winner / loser (if it's not a draw).

Upvotes: 8

Related Questions