Example #1
0
        public GameOver IsCheckMate()
        {
            var king       = board.GetKingByColor(Color);
            var enemyPiece = board.IsKingChecked(Color);
            var moves      = board.CalcPossibleMoves(king);

            if (enemyPiece != null)
            {
                var enemyPieces = board.GetAllPiecesByColor(Color).Where(x => !(x is King));

                //Can be blocked?
                foreach (var piece in enemyPieces)
                {
                    var pieceMoves = king.Point.AllMovesWithinDirection(enemyPiece.Point, king.ChooseRightDirection(enemyPiece.Point));
                    foreach (var end in pieceMoves)
                    {
                        var square = board.Squares.FirstOrDefault(x => x.Point.Equals(end));
                        if (piece.CanMoveWithoutColliding(square, board))
                        {
                            return(GameOver.None);
                        }
                    }
                }

                //Can be moved?
                if (moves.Count != 0)
                {
                    return(GameOver.None);
                }

                return(GameOver.Checkmate);
            }
            else
            {
                foreach (var piece in board.GetAllPiecesByColor(Color))
                {
                    if (board.CalcPossibleMoves(piece).Count != 0)
                    {
                        return(GameOver.None);
                    }
                }

                return(GameOver.Stalemate);
            }
        }