public bool IsPlayerCheckmated(PlayerTypes player, IEnumerable <Piece> rows, Dictionary <Piece, Point[]> piecesMovesDic) { foreach (var piece in piecesMovesDic) { foreach (var newPos in piece.Value) { //Simulate board after move var oldPos = piece.Key.Position; Piece destPiece = Board.At(newPos); //To restore the old board ChangePiecePos(Board.At(oldPos), newPos); Board[oldPos.Y][oldPos.X] = null; var enemyPossibleMoves = GetPossibleMoves(rows, CurrPlayer); var isPlayerChecked = IsPlayerChecked(player, rows, enemyPossibleMoves); //Restore old board ChangePiecePos(Board.At(newPos), oldPos); Board[newPos.Y][newPos.X] = destPiece; if (!isPlayerChecked) { return(false); } } } return(true); }
private void InitValidations() { _validations = new List <FuncEnumPair <Point, Statuses> > { new FuncEnumPair <Point, Statuses>((origin, dest) => !(ChessUtils.IsValidPosition(origin) && ChessUtils.IsValidPosition(origin)), Statuses.Invalid_Indexes), new FuncEnumPair <Point, Statuses>((origin, dest) => Board.At(origin)?.Player != CurrPlayer, Statuses.Invalid_OriginIsNotCurrentPlayerPiece), new FuncEnumPair <Point, Statuses>((origin, dest) => Board.At(dest)?.Player == CurrPlayer, Statuses.Invalid_DestIsCurrentPlayerPiece), new FuncEnumPair <Point, Statuses>((origin, dest) => origin.Equals(dest), Statuses.Invalid_OriginIsEqualToDest), new FuncEnumPair <Point, Statuses>((origin, dest) => !Board.At(origin).GetPossibleMoves(Board).Contains(dest), Statuses.Invalid_PieceMovement), new FuncEnumPair <Point, Statuses>((origin, dest) => Board.At(origin).GetPossibleMoves(Board).Contains(dest), Statuses.Valid) }; }
private bool TryPerformMove(Point origin, Point dest) { Piece destPiece = Board.At(dest); //to restore the old board ChangePiecePos(Board.At(origin), dest); Board[origin.Y][origin.X] = null; var rows = Board.SelectMany(row => row); if (IsPlayerChecked(CurrPlayer, rows)) { ChangePiecePos(Board.At(dest), origin); Board[dest.Y][dest.X] = destPiece; return(false); } return(true); }