コード例 #1
0
        private void InsertNewPiece(Piece piece, ChessPosition chessPosition)
        {
            ChessBoard.PutPiece(piece, chessPosition.ToPosition());

            OutOfGamePieces.Remove(piece);
            InGamePieces.Add(piece);
        }
コード例 #2
0
        private void RemovePiece(ChessPosition chessPosition, Piece piece)
        {
            RemovePiece(chessPosition);

            InGamePieces.Remove(piece);
            OutOfGamePieces.Add(piece);
        }
コード例 #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);
            }
        }
コード例 #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");
            }
        }
コード例 #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");
     }
 }
コード例 #6
0
 private void RemovePiece(ChessPosition chessPosition)
 {
     ChessBoard.RemovePiece(chessPosition.ToPosition());
 }
コード例 #7
0
 private void InsertPiece(Piece piece, ChessPosition chessPosition)
 {
     ChessBoard.PutPiece(piece, chessPosition.ToPosition());
 }
コード例 #8
0
 public void CheckTargetPosition(ChessPosition target)
 {
     ChessBoard.ValidPosition(target.ToPosition());
 }