chess.js icon indicating copy to clipboard operation
chess.js copied to clipboard

Prompt for promotion piece

Open spankyed opened this issue 3 years ago • 7 comments

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.

spankyed avatar Aug 24 '21 00:08 spankyed

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?

jhlywa avatar Aug 24 '21 02:08 jhlywa

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 avatar Aug 24 '21 04:08 spankyed

#131 related

spankyed avatar Aug 24 '21 04:08 spankyed

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.

jhlywa avatar Aug 27 '21 02:08 jhlywa

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?

spankyed avatar Aug 27 '21 12:08 spankyed

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

designingSparks avatar Nov 12 '21 12:11 designingSparks

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.

knownasilya avatar Dec 31 '21 23:12 knownasilya