コード例 #1
0
        public static string IsLegalMovement(PlayerMove currentMove, Board i_Board)
        {
            bool   isValidMovement = true;
            Square sourceSquare;
            Square destinationSquare;
            char   currentUserCoinType;
            char   otherUserCoinType;
            bool   tryingToJump;

            Coin[,] boardArray = i_Board.BoardArray;
            sourceSquare       = currentMove.CurrentSquare;
            destinationSquare  = currentMove.NextSquare;
            string errorMessage = string.Empty;

            if (!movementIndexesInRange(currentMove, i_Board.BoardSize))
            {
                isValidMovement = false;
                errorMessage    = ErrorMessageGenerator.OutOfBoundMessage();
            }
            else
            {
                Coin currentCoin = boardArray[sourceSquare.RowIndex, sourceSquare.ColumnIndex];
                if (i_Board.IsEmptyAtSquare(sourceSquare))
                {
                    isValidMovement = false;
                    errorMessage    = ErrorMessageGenerator.NoCoinToMoveMessage();
                }

                if (isValidMovement)
                {
                    currentUserCoinType = currentCoin.Type;
                    otherUserCoinType   = currentUserCoinType == Constants.k_FirstCoinType ? Constants.k_SecondCoinType : Constants.k_FirstCoinType;

                    if (!currentCoin.Type.Equals(currentUserCoinType))
                    {
                        isValidMovement = false;
                        errorMessage    = ErrorMessageGenerator.NoCoinToMoveMessage();
                    }
                    else if (currentCoin.IsKing)
                    {
                        tryingToJump = KingValidation.isTryingToJump_King(currentMove);
                        if (tryingToJump)
                        {
                            isValidMovement = IsValidJump(currentMove, otherUserCoinType, i_Board);
                            if (!isValidMovement)
                            {
                                errorMessage = ErrorMessageGenerator.InvalidJumpMessage();
                            }
                        }
                        else
                        {
                            if (!i_Board.IsEmptyAtSquare(destinationSquare))
                            {
                                isValidMovement = false;
                                errorMessage    = ErrorMessageGenerator.SquareIsBlockedMessage();
                            }
                            else
                            {
                                isValidMovement = KingValidation.isDiagonal_King(currentMove);
                                if (!isValidMovement)
                                {
                                    errorMessage = ErrorMessageGenerator.NotDiagonalMessage();
                                }
                            }
                        }
                    }
                    else if (IsTryingToJump(currentMove, Convert.ToString(currentUserCoinType)))
                    {
                        if (!IsValidJump(currentMove, otherUserCoinType, i_Board))
                        {
                            errorMessage = ErrorMessageGenerator.InvalidJumpMessage();
                        }
                    }
                    else if (!i_Board.IsEmptyAtSquare(destinationSquare))
                    {
                        isValidMovement = false;
                        errorMessage    = ErrorMessageGenerator.SquareIsBlockedMessage();
                    }
                    else if (!IsDiagonal(currentMove, currentUserCoinType))
                    {
                        isValidMovement = false;
                        errorMessage    = ErrorMessageGenerator.NotDiagonalMessage();
                    }
                }
            }

            return(errorMessage);
        }