Skip to content

Commit

Permalink
Fixed #106
Browse files Browse the repository at this point in the history
  • Loading branch information
shaack committed Apr 20, 2023
1 parent ff7b49a commit 33d7505
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
8 changes: 4 additions & 4 deletions examples/extensions/promotion-dialog-extension.html
Expand Up @@ -36,10 +36,10 @@ <h2>Example of the cm-chessboard PromotionDialog extension</h2>
chessboard.enableMoveInput((event) => {
if (event.type === INPUT_EVENT_TYPE.validateMoveInput) {
if (event.squareTo.charAt(1) === "8" && event.piece.charAt(1) === "p") {
chessboard.showPromotionDialog(event.squareTo, COLOR.white, (event) => {
console.log("48a99d Piece selected", event.piece)
if (event.piece) {
chessboard.setPiece(event.square, event.piece, true)
chessboard.showPromotionDialog(event.squareTo, COLOR.white, (result) => {
console.log("Promotion result", result)
if (result.piece) {
chessboard.setPiece(result.square, result.piece, true)
} else {
chessboard.setPosition(position)
}
Expand Down
48 changes: 34 additions & 14 deletions examples/validate-moves.html
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="styles/examples.css"/>
<link rel="stylesheet" href="../assets/chessboard.css"/>
<link rel="stylesheet" href="../assets/extensions/markers/markers.css"/>
<link rel="stylesheet" href="../assets/extensions/promotion-dialog/promotion-dialog.css"/>
</head>
<body>
<h1><a href="../">cm-chessboard</a></h1>
Expand All @@ -25,6 +26,19 @@ <h2>Example: Input enabled, move validation with chess.js</h2>

const chess = new Chess()

function makeEngineMove(chessboard) {
const possibleMoves = chess.moves({verbose: true})
if (possibleMoves.length > 0) {
const randomIndex = Math.floor(Math.random() * possibleMoves.length)
const randomMove = possibleMoves[randomIndex]
setTimeout(() => { // smoother with 500ms delay
chess.move({from: randomMove.from, to: randomMove.to})
chessboard.setPosition(chess.fen(), true)
chessboard.enableMoveInput(inputHandler, COLOR.white)
}, 500)
}
}

function inputHandler(event) {
event.chessboard.removeMarkers(MARKER_TYPE.dot)
event.chessboard.removeMarkers(MARKER_TYPE.bevel)
Expand All @@ -42,30 +56,36 @@ <h2>Example: Input enabled, move validation with chess.js</h2>
}
return moves.length > 0
} else if (event.type === INPUT_EVENT_TYPE.validateMoveInput) {
const move = {from: event.squareFrom, to: event.squareTo}
const move = {from: event.squareFrom, to: event.squareTo, promotion: event.promotion}
const result = chess.move(move)
if (result) {
event.chessboard.disableMoveInput()
this.chessboard.state.moveInputProcess.then(() => { // wait for the move input process has finished
this.chessboard.setPosition(chess.fen(), true).then(() => { // update position, maybe castled and wait for animation has finished
const possibleMoves = chess.moves({verbose: true})
if (possibleMoves.length > 0) {
const randomIndex = Math.floor(Math.random() * possibleMoves.length)
const randomMove = possibleMoves[randomIndex]
setTimeout(() => { // smoother with 500ms delay
chess.move({from: randomMove.from, to: randomMove.to})
event.chessboard.enableMoveInput(inputHandler, COLOR.white)
event.chessboard.setPosition(chess.fen(), true)
}, 500)
}
makeEngineMove(event.chessboard)
})
})
} else {
console.warn("invalid move", move)
// promotion?
let possibleMoves = chess.moves({square: event.squareFrom, verbose: true})
for (const possibleMove of possibleMoves) {
if (possibleMove.promotion && possibleMove.to === event.squareTo) {
event.chessboard.showPromotionDialog(event.squareTo, COLOR.white, (result) => {
console.log("promotion result", result)
if (result) {
chess.move({from: event.squareFrom, to: event.squareTo, promotion: result.piece.charAt(1)})
event.chessboard.setPosition(chess.fen(), true)
makeEngineMove(event.chessboard)
} else {
event.chessboard.setPosition(chess.fen(), true)
event.chessboard.enableMoveInput(inputHandler, COLOR.white)
}
})
return true
}
}
}
return result
} else if (event.type === INPUT_EVENT_TYPE.validateMoveInput) {

}
}

Expand Down
Expand Up @@ -147,14 +147,14 @@ export class PromotionDialog extends Extension {
if(this.state.displayState === DISPLAY_STATE.shown) {
event.preventDefault()
this.setDisplayState(DISPLAY_STATE.hidden)
this.state.callback({square: this.state.square, piece: null})
this.state.callback(null)
}
}

contextMenu(event) {
event.preventDefault()
this.setDisplayState(DISPLAY_STATE.hidden)
this.state.callback({square: this.state.square, piece: null})
this.state.callback(null)
}

setDisplayState(displayState) {
Expand Down

0 comments on commit 33d7505

Please sign in to comment.