Exemplo n.º 1
0
        public bool MoveResolvesCheck(Color color, Point move)
        {
            ChessPiece[,] board = ChessBoard.GetBoard;

            int originX = (int)Coordinates.X;
            int originY = (int)Coordinates.Y;

            int moveX = (int)move.X;
            int moveY = (int)move.Y;

            ChessPiece chessPieceAtMove = board[moveX, moveY];

            bool moveResolvesCheck = false;

            if ((chessPieceAtMove != null && chessPieceAtMove.GetType().Name != "King") || chessPieceAtMove == null)
            {
                board[moveX, moveY]     = board[originX, originY];
                board[originX, originY] = null;

                board[moveX, moveY].SetCoordinates = move;

                if (!ChessBoard.KingIsChecked(color))
                {
                    moveResolvesCheck = true;
                }

                board[originX, originY] = board[moveX, moveY];
                board[moveX, moveY]     = chessPieceAtMove;

                board[originX, originY].SetCoordinates = new Point(originX, originY);
            }
            return(moveResolvesCheck);
        }
Exemplo n.º 2
0
        private King GetKing(Color color)
        {
            ChessPiece[,] board = GetBoard;
            King king = null;

            for (int i = 0; i < board.GetLength(0); i++)
            {
                for (int j = 0; j < board.GetLength(1); j++)
                {
                    ChessPiece chessPiece = board[i, j];

                    if (chessPiece != null)
                    {
                        if (chessPiece.GetType().Name == "King" && chessPiece.GetColor == color)
                        {
                            king = (King)chessPiece;
                        }
                    }
                }
            }

            return(king);
        }
Exemplo n.º 3
0
        private void MoveChessPiece(object sender, MouseButtonEventArgs e)
        {
            Border border      = (Border)sender;
            Border highlighted = (Border)mainWindow.FindName("highlighted");

            if (highlighted != null)
            {
                int originX = Grid.GetRow(highlighted);
                int originY = Grid.GetColumn(highlighted);

                ChessPiece currentChessPiece = GetBoard[originX, originY];

                if (border.Child == null || currentChessPiece.GetColor == currentTurnColor)
                {
                    bool kingIsChecked = KingIsChecked(currentTurnColor);

                    int destinationX = Grid.GetRow(border);
                    int destinationY = Grid.GetColumn(border);

                    if (currentChessPiece.MoveResolvesCheck(currentTurnColor, new Point(destinationX, destinationY)))
                    {
                        List <Point> validMoves = null;

                        if (!KingIsChecked(currentTurnColor))
                        {
                            validMoves = currentChessPiece.GetValidMoves();
                        }
                        else
                        {
                            validMoves = currentChessPiece.GetValidMovesIfKingIsChecked();
                        }

                        if (validMoves.Where(point => point.X == destinationX && point.Y == destinationY).Any())
                        {
                            if (currentChessPiece.GetType().Name == "Pawn")
                            {
                                ((Pawn)currentChessPiece).SetHasStepped = true;
                            }
                            currentChessPiece.SetCoordinates = new Point(destinationX, destinationY);

                            GetBoard[destinationX, destinationY] = GetBoard[originX, originY];
                            GetBoard[originX, originY]           = null;

                            currentTurnColor = currentTurnColor == Color.White ? Color.Black : Color.White;

                            UpdateBoard();

                            mainWindow.UnregisterName(highlighted.Name);

                            if (IsCheckMate(currentTurnColor))
                            {
                                MessageBox.Show(currentTurnColor.ToString() + " has lost");
                            }
                        }
                        else
                        {
                            ColorTile(highlighted);

                            List <Point> highlightedValidMoves = null;

                            if (!KingIsChecked(currentTurnColor))
                            {
                                highlightedValidMoves = currentChessPiece.GetValidMoves();
                            }
                            else
                            {
                                highlightedValidMoves = currentChessPiece.GetValidMovesIfKingIsChecked();
                            }

                            UnshowValidMoves(highlightedValidMoves);
                            mainWindow.UnregisterName(highlighted.Name);
                        }
                    }
                    else
                    {
                        ColorTile(highlighted);

                        List <Point> highlightedValidMoves = null;

                        if (!KingIsChecked(currentTurnColor))
                        {
                            highlightedValidMoves = currentChessPiece.GetValidMoves();
                        }
                        else
                        {
                            highlightedValidMoves = currentChessPiece.GetValidMovesIfKingIsChecked();
                        }
                        UnshowValidMoves(highlightedValidMoves);
                        mainWindow.UnregisterName(highlighted.Name);
                    }
                }
            }
        }