Exemplo n.º 1
0
        private void InsertNewPiece(Piece piece, ChessPosition chessPosition)
        {
            ChessBoard.PutPiece(piece, chessPosition.ToPosition());

            OutOfGamePieces.Remove(piece);
            InGamePieces.Add(piece);
        }
Exemplo n.º 2
0
        private void RemovePiece(ChessPosition chessPosition, Piece piece)
        {
            RemovePiece(chessPosition);

            InGamePieces.Remove(piece);
            OutOfGamePieces.Add(piece);
        }
Exemplo n.º 3
0
        private void UndoMovement(ChessPosition origin, ChessPosition target, Piece removedPiece)
        {
            var oldOriginPiece = ChessBoard.GetPiece(target.ToPosition());

            RemovePiece(target);
            InsertPiece(oldOriginPiece, origin);
            oldOriginPiece.DecrementMovement();

            if (removedPiece != null)
            {
                InsertNewPiece(removedPiece, target);
            }
        }
Exemplo n.º 4
0
        public void ExecuteMovement(ChessPosition origin, ChessPosition target)
        {
            var originPiece = ChessBoard.GetPiece(origin.ToPosition());

            if (originPiece.IsPossibleMovement(target.ToPosition()))
            {
                RemovePiece(origin);
                var currentTargetPiece = ChessBoard.GetPiece(target.ToPosition());

                if (currentTargetPiece != null)
                {
                    RemovePiece(target, currentTargetPiece);
                }

                InsertPiece(originPiece, target);

                // Verify if the player CHECKED himself or if the player already is on CHECK
                if (IsInCheck(GetKing(CurrentPlayer)))
                {
                    UndoMovement(origin, target, currentTargetPiece);

                    if (Check)
                    {
                        throw new ChessMatchException("You are on CHECK!");
                    }
                    else
                    {
                        throw new ChessMatchException("You can't CHECK yourself");
                    }
                }

                // Verify if the player CHECKED the adversary
                Check = IsInCheck(GetKing(Adversary(CurrentPlayer)));

                // Verify if the player CHECKMATE the adversary
                if (Check)
                {
                    if (IsInCheckMate(GetKing(Adversary(CurrentPlayer))))
                    {
                        Finished   = true;
                        FinishedAt = DateTime.Now;
                    }
                }

                NextTurn(originPiece);
            }
            else
            {
                throw new ChessMatchException("This piece can't make this movement");
            }
        }
Exemplo n.º 5
0
 public void CheckOriginPosition(ChessPosition origin)
 {
     if (ChessBoard.PieceExists(origin.ToPosition()))
     {
         if (!ChessBoard.GetPiece(origin.ToPosition()).Color.Equals(CurrentPlayer))
         {
             throw new ChessMatchException("You can only movement your pieces");
         }
     }
     else
     {
         throw new ChessBoardException("Not exist a piece in this position");
     }
 }
Exemplo n.º 6
0
 private void RemovePiece(ChessPosition chessPosition)
 {
     ChessBoard.RemovePiece(chessPosition.ToPosition());
 }
Exemplo n.º 7
0
 private void InsertPiece(Piece piece, ChessPosition chessPosition)
 {
     ChessBoard.PutPiece(piece, chessPosition.ToPosition());
 }
Exemplo n.º 8
0
 public void CheckTargetPosition(ChessPosition target)
 {
     ChessBoard.ValidPosition(target.ToPosition());
 }