I have tried to figure out a function or algorithm with no use of library
so far I have a function for when there is a check, but can't wrap my head around the checkmate function... I know I'm supposed to look for all the possible moves, but can't really make it work
getKingPosition(squares, player) {
return squares.reduce((acc, current, i) =>
acc || //King may be the only one, if we had found it, returned his position
((current //current square mustn't be a null
&& (current.getPlayer() === player)) //we are looking for aspecical king
&& (current instanceof King)
&& i), // returned position if all conditions are completed
null)
}
isCheckForPlayer(squares, player) {
const opponent = player === 1 ? 2 : 1
const playerKingPosition = this.getKingPosition(squares, player)
const canPieceKillPlayerKing = (piece, i) => piece.isMovePossible(playerKingPosition, i, squares)
return (
squares.reduce((acc, current, index) =>
acc ||
(current &&
(current.getPlayer() === opponent) &&
canPieceKillPlayerKing(current, index)
&& true),
false)
)
}
isCheckmate(isCheckForPlayer, isMovePossible){
const KingUnabletoMove = this.getKingPosition(squares, player)
const CheckMate =
}
I have started on the function above, but currently a bit clueless..
below is the code for, blocking the possibility of a potential move made because there is a check
if (isMovePossible) {
if (squares[i] !== null) {
if (squares[i].player === 1) {
whitepiecestaken.push(squares[i]);
}
else {
blackpiecestaken.push(squares[i]);
}
}
squares[i] = squares[this.state.sourceSelection];
squares[this.state.sourceSelection] = null;
const isCheckMe = this.isCheckForPlayer(squares, this.state.player)
if (isCheckMe) {
this.setState(oldState => ({
status: "Wrong selection. Choose Valid source and destination again. Now you have a check!",
sourceSelection: -1,
}))
} else {
let player = this.state.player === 1 ? 2 : 1;
let turn = this.state.turn === 'white' ? 'black' : 'white';
this.setState(oldState => ({
sourceSelection: -1,
squares,
whitepiecestaken: [...oldState.whitepiecestaken, ...whitepiecestaken],
blackpiecestaken: [...oldState.blackpiecestaken, ...blackpiecestaken],
player,
status: '',
turn
}));
}
}
else {
this.setState({
status: "Wrong selection. Choose valid source and destination again.",
sourceSelection: -1,
});
}
}
}
Also included the kings ability to move below
import Piece from './piece.js';
import { isSameDiagonal, isSameRow } from '../helpers';
export default class King extends Piece {
constructor(player) {
super(player, (player === 1 ?
"https://upload.wikimedia.org/wikipedia/commons/4/42/Chess_klt45.svg" :
"https://upload.wikimedia.org/wikipedia/commons/f/f0/Chess_kdt45.svg"
));
}
isMovePossible(src, dest) {
return ((src - 9 === dest && isSameDiagonal(src, dest)) ||
src - 8 === dest ||
(src - 7 === dest && isSameDiagonal(src, dest)) ||
(src + 1 === dest && isSameRow(src, dest)) ||
(src + 9 === dest && isSameDiagonal(src, dest)) ||
src + 8 === dest ||
(src + 7 === dest && isSameDiagonal(src, dest)) ||
(src - 1 === dest && isSameRow(src, dest)))
}
/**
* always returns empty array because of one step
* @return {[]}
*/
getSrcToDestPath(src, dest) {
return [];
}
}
Hope someone can help me