jhlywa / chess.js

A TypeScript chess library for chess move generation/validation, piece placement/movement, and check/checkmate/draw detection
BSD 2-Clause "Simplified" License
3.72k stars 892 forks source link

How to make chess.js play white and make a first move? #474

Open Huub62 opened 2 months ago

Huub62 commented 2 months ago

For a kids-chessgame I use chess.js and chessboard.js. Playing with white is easy, but how can i let chess.js make a first move and let the kids play with black?

Thanks in advance for any help.

tazo-taz commented 1 month ago

It has built in method which returns all possible moves of current player and you can let computer start playing.

const moves = chess.moves({ verbose: true }) const randomMove = moves[Math.floor(Math.random() * moves.length)] chess.play(randomMove)