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 891 forks source link

Prompt for promotion piece #293

Open spankyed opened 3 years ago

spankyed commented 3 years ago

I think its an oversight that each developer has to hack up a solution to allow their users to select which piece they want to promote to. There should be some standard way of doing this. See issue #49.

jhlywa commented 3 years ago

I'm a little confused. Do you have an algebraic from / to square and want to figure out if the move is a promotion prior to making it?

const chess = Chess('7k/4P3/8/8/8/8/8/7K w - - 0 1') 

const from = 'e7'
const to = 'e8'

const isPromotion = chess.moves({ verbose: true })
                         .filter((move) => move.from === from && 
                                           move.to === to &&
                                           moves.flags.includes('p')).length > 0

if (isPromotion) { 
    const promotion = promptUserForPromotionPiece() // <- you supply this
    chess.move({ to, from, promotion })
} else {
    chess.move({ to, from })
}

Like this?

spankyed commented 3 years ago

I feel a little confused here too, and I think the API may be to blame. Prompting-For-Promotion-Piece is not really a fringe requirement. There should be a succinct way to do this. I think the way you just showed should be sufficient. Alternatively, a checkMove() method would make this task more readable.

const chess = Chess('7k/4P3/8/8/8/8/8/7K w - - 0 1') 

const from = 'e7'
const to = 'e8'

const validMove = chess.checkMove({ to, from })

if (validMove && validMove.isPromotion){
    const promotion = promptUserForPromotionPiece()
    chess.move({ to, from, promotion })
}

It may be hard to envision the perfect API for this. And if designing a better API that covers this fairly common requirement is too difficult, then at the least, there needs to be better documentation on the issue than the one solution provided on issue #49 that consists of instantiating a new Chess engine just to check if the move is a promotion move.

spankyed commented 3 years ago

131 related

jhlywa commented 3 years ago

Would allowing the user to supply a callback (to execute prior to promotion) help with this?

In the mean time, I'll add the example above to an FAQ section in the docs.

spankyed commented 3 years ago

I saw an older chess engine library somewhere that did this throughout the API and I found it interesting and very readable. But since callbacks aren't really used throughout this library I'm not sure this is the best idea. Could you share what the API would look like using supplied a callback?

designingSparks commented 3 years ago

@jhlywa Any news here? This is an important feature if building an interactive game.

knownasilya commented 2 years ago

I like the proposed chess.checkMove solution. Otherwise getting the value is simple, and would be different based on how you are building your game. Like it would be different for a Svelte implementation vs a React one or an Ember one.