public void InitializeChessBoard()
 {
     ChessBoard = EmptyChessboard;
     pieces     = PlaceChessPieces(chessBoard);
     ListOfMovements.Add(new Movement("Chessboard", null, @"pack://application:,,,/ChessGame;component/Resources/chessboard.png"));
     listOfChessBoards.Add(GetCopyOfChessboard(ChessBoard));
     SetWaitingForOpponentMessage();
 }
        private void CheckIfCheckmate(ObservableCollection <ObservableCollection <Square> > chessBoard)
        {
            bool isCheckmate = true;

            foreach (var currentPiece in pieces)
            {
                if (currentPiece.IsWhite != SelectedSquare.Piece.IsWhite)
                {
                    if (!IsThereAnyMoveAvailable(currentPiece))
                    {
                        isCheckmate = false;
                    }
                }
            }
            if (isCheckmate)
            {
                var king = pieces.FirstOrDefault(k => k.ChessPieceType.Contains(Resources.King) && k.IsWhite != SelectedSquare.Piece.IsWhite);
                GetSelectedPiece(king.Location, chessBoard).Background = CheckBackground;
                ListOfMovements.Add(new Movement(string.Format("{0}: Checkmate! {1} wins!", ++noOfMovement, king.IsWhite ? "Black" : "White"), null, SelectedSquare.Piece.ChessPieceIcon));
                IsCheckmate = true;
            }
        }
        private void MakeCastle(Square previousSelectedPiece)
        {
            if (previousSelectedPiece != null && previousSelectedPiece.Piece.ChessPieceType != null && SelectedSquare != null && SelectedSquare.Piece.ChessPieceType != null)
            {
                if (previousSelectedPiece.Piece.ChessPieceType.Contains(Resources.King) && SelectedSquare.Piece.ChessPieceType.Contains(Resources.Rook) && SelectedSquare.Piece.IsWhite == previousSelectedPiece.Piece.IsWhite)
                {
                    if (!Movements.ContainsValue(previousSelectedPiece.Id) && !Movements.ContainsValue(previousSelectedPiece.Id))
                    {
                        int offset = 1;
                        if (SelectedSquare.Piece.Location[0] > previousSelectedPiece.Id[0])
                        {
                            offset = -1;
                        }
                        bool canMakeCastle = true;
                        for (int i = SelectedSquare.Piece.Location[0] + offset; i != previousSelectedPiece.Id[0]; i += offset)
                        {
                            string s = ((char)i).ToString() + previousSelectedPiece.Id[1].ToString();
                            if (pieces.Any(p => p.Location == s))
                            {
                                canMakeCastle = false;
                            }
                        }
                        if (canMakeCastle)
                        {
                            noOfMovement++;
                            ListOfMovements.Add(new Movement(string.Format("{0}: {1} Castle", noOfMovement, IsWhiteTurn ? "White: " : "Black: "), null, null));
                            ClearAvailableSquares(ChessBoard, markedAsAvailableSquares);
                            IsPieceMoved = true;
                            AddMoveSound = true;

                            var c          = mapper.StringToCoordinates[SelectedSquare.Piece.Location];
                            var rookTemp   = chessBoard[c.i][c.j].Piece;
                            var rookIdTemp = rookTemp.Location;
                            chessBoard[c.i][c.j].Piece = new ChessPiece();
                            string rook = ((char)(previousSelectedPiece.Piece.Location[0] - offset)).ToString() + previousSelectedPiece.Piece.Location[1].ToString();

                            rookTemp.Location = rook;
                            var piece = pieces.FirstOrDefault(p => p.Location == null && p.IsWhite == rookTemp.IsWhite);
                            piece = rookTemp;
                            c     = mapper.StringToCoordinates[rook];
                            chessBoard[c.i][c.j].Piece = rookTemp;

                            c = mapper.StringToCoordinates[previousSelectedPiece.Piece.Location];
                            var kingTemp   = chessBoard[c.i][c.j].Piece;
                            var kingIdTemp = kingTemp.Location;
                            chessBoard[c.i][c.j].Piece = new ChessPiece();

                            string king = ((char)(kingIdTemp[0] - 2 * offset)).ToString() + kingIdTemp[1].ToString();

                            kingTemp.Location = king;
                            piece             = pieces.FirstOrDefault(p => p.ChessPieceType.Contains(Resources.King) && p.IsWhite == kingTemp.IsWhite);
                            piece             = kingTemp;
                            c = mapper.StringToCoordinates[king];
                            chessBoard[c.i][c.j].Piece = kingTemp;

                            IsWhiteTurn = !IsWhiteTurn;
                            return;
                        }
                    }
                }
            }
        }
        public void DisplayAvailableSquares(String squareId)
        {
            IsPieceMoved    = false;
            IsPieceCaptured = false;
            AddMoveSound    = false;
            var previousSelectedPiece = SelectedSquare;

            SelectedSquare = GetSelectedPiece(squareId, ChessBoard);

            if (SelectedSquare == null)
            {
                return;
            }

            if (previousSelectedPiece != null && previousSelectedPiece.Background != null)
            {
                var c = mapper.StringToCoordinates[previousSelectedPiece.Id];
                chessBoard[c.i][c.j].Background = (c.i + c.j) % 2 == 0 ? WhiteBackground : BlackBackground;
            }

            if (CheckState != null && CheckState.Piece != null)
            {
                IsWhiteTurn = CheckState.Piece.IsWhite;
            }

            MakeCastle(previousSelectedPiece);

            if (markedAsAvailableSquares.Contains(SelectedSquare))
            {
                //an available move was selected
                AddMoveSound = true;

                ClearKingBackground();

                // check if the move can be made
                if (CanPieceBeMoved(SelectedSquare, previousSelectedPiece, ChessBoard, pieces, markedAsAvailableSquares, true))
                {
                    IsPieceMoved = true;
                    AddMoveSound = true;
                    Movements[SelectedSquare.Id] = previousSelectedPiece.Id;
                    listOfChessBoards.Add(GetCopyOfChessboard(ChessBoard));
                    noOfMovement++;
                    ListOfMovements.Add(new Movement(string.Format("{0}: {1} {2}", noOfMovement.ToString(), SelectedSquare.Piece.IsWhite ? "White" : "Black",
                                                                   SelectedSquare.Piece.ChessPieceType), previousSelectedPiece.Id + "-" + SelectedSquare.Id, SelectedSquare.Piece.ChessPieceIcon));

                    IsWhiteTurn = !IsWhiteTurn;
                    if (CheckState != null)
                    {
                        CheckState.Background = CheckBackground;
                    }
                    CheckIfCheckmate(ChessBoard);
                    SetWaitingForOpponentMessage();
                }
                else
                {
                    //is in chess state
                    if (CheckState != null && CheckState.Piece != null)
                    {
                        IsWhiteTurn           = CheckState.Piece.IsWhite;
                        CheckState.Background = CheckBackground;
                    }
                }
                return;
            }

            //clear
            ClearAvailableSquares(ChessBoard, markedAsAvailableSquares);

            MarkAvailableSquares(previousSelectedPiece);
        }