private bool AnyValidDrops(PlayerColor playerColor, DropCheckmateLevel recursionLevel, IEntitiesDB entitiesDB)
        {
            bool returnValue = false;
            List <HandPieceEV> teamHandPieces = handService.FindAllTeamHandPieces(playerColor, entitiesDB).Where(handPiece =>
                                                                                                                 handPiece.HandPiece.NumPieces.value > 0).ToList();

            for (int rank = 0; rank < BoardConst.NUM_FILES_RANKS; ++rank)
            {
                for (int file = 0; file < BoardConst.NUM_FILES_RANKS; ++file)
                {
                    foreach (HandPieceEV handPiece in teamHandPieces)
                    {
                        Vector2 location = new Vector2(file, rank);

                        if (DropPossible(handPiece, location, playerColor, recursionLevel, entitiesDB))
                        {
                            returnValue = true;
                            goto ReturnLocation; // Break out of all the for loops
                        }
                    }
                }
            }

ReturnLocation:
            return(returnValue);
        }
        private bool DropPossible(
            HandPieceEV handPiece, Vector2 location, PlayerColor playerColor, DropCheckmateLevel recursionLevel, IEntitiesDB entitiesDB)
        {
            List <PieceEV> piecesAtLocation = pieceFindService.FindPiecesByLocation(location, entitiesDB);
            bool           isFrontValid     = preDropService.IsValidFrontDrop(ref handPiece, location, piecesAtLocation, entitiesDB);
            bool           isBackValid      = preDropService.IsValidBackDrop(ref handPiece, location, piecesAtLocation, entitiesDB);
            PieceEV        pieceToDrop      = pieceFindService.FindFirstPieceByLocationAndType(
                BoardConst.HAND_LOCATION, handPiece.HandPiece.PieceType, handPiece.HandPiece.Back, entitiesDB);

            return((isFrontValid && DropReleasesCheck(
                        pieceToDrop,
                        location,
                        playerColor,
                        PieceSide.FRONT,
                        handPiece,
                        recursionLevel,
                        entitiesDB)) ||
                   (isBackValid && DropReleasesCheck(
                        pieceToDrop,
                        location,
                        playerColor,
                        PieceSide.BACK,
                        handPiece,
                        recursionLevel,
                        entitiesDB)));
        }
        private bool DropReleasesCheck(
            PieceEV pieceToDrop,
            Vector2 location,
            PlayerColor playerColor,
            PieceSide side,
            HandPieceEV handPiece,
            DropCheckmateLevel recursionLevel,
            IEntitiesDB entitiesDB)
        {
            bool returnValue;

            PieceEV?topPieceAtLocation = pieceFindService.FindTopPieceByLocation(location, entitiesDB);

            pieceSetService.SetTopOfTowerToFalse(topPieceAtLocation, entitiesDB);

            int tier = topPieceAtLocation.HasValue ? topPieceAtLocation.Value.Tier.Tier + 1 : 1;

            pieceSetService.SetPieceLocationAndTier(pieceToDrop, location, tier, entitiesDB);
            pieceSetService.SetPiecePlayerOwner(pieceToDrop, playerColor, entitiesDB);
            pieceSetService.SetPieceSide(pieceToDrop, side, entitiesDB);
            handService.DecrementHandPiece(ref handPiece);

            PlayerColor enemyPlayerColor = TurnService.CalcOtherTurnPlayer(playerColor);

            returnValue = DropCheckmateNotViolated(
                pieceToDrop,
                location,
                playerColor,
                side,
                handPiece,
                recursionLevel,
                entitiesDB);

            if (returnValue)
            {
                returnValue = !checkService.IsCommanderInCheck(playerColor, entitiesDB);
            }

            handService.IncrementHandPiece(ref handPiece);
            pieceSetService.SetPieceLocationToHandLocation(pieceToDrop, entitiesDB);

            if (topPieceAtLocation.HasValue)
            {
                pieceSetService.SetTopOfTower(topPieceAtLocation.Value, entitiesDB);
            }

            return(returnValue);
        }
        private bool DropCheckmateNotViolated(
            PieceEV pieceToDrop,
            Vector2 location,
            PlayerColor playerColor,
            PieceSide side,
            HandPieceEV handPiece,
            DropCheckmateLevel recursionLevel,
            IEntitiesDB entitiesDB)
        {
            bool        returnValue      = true;
            PlayerColor enemyPlayerColor = TurnService.CalcOtherTurnPlayer(playerColor);

            if (recursionLevel != DropCheckmateLevel.FINAL_TURN_PLAYER_CHECK && // Prevent infinite recursion
                AbilityToPiece.HasAbility(DropAbility.CANNOT_DROP_CHECKMATE, pieceToDrop.Piece.PieceType) &&
                checkService.IsCommanderInCheck(enemyPlayerColor, entitiesDB))
            {
                recursionLevel = recursionLevel == DropCheckmateLevel.FIRST_TURN_PLAYER_CHECK
                    ? DropCheckmateLevel.SECOND_ENEMY_PLAYER_CHECK : DropCheckmateLevel.FINAL_TURN_PLAYER_CHECK;
                returnValue = AnyValidMoves(enemyPlayerColor, entitiesDB, recursionLevel);
            }

            return(returnValue);
        }
 private bool AnyValidMoves(PlayerColor playerColor, IEntitiesDB entitiesDB, DropCheckmateLevel recursionLevel)
 {
     return(AnyValidMobileMoves(playerColor, entitiesDB) || AnyValidImmobileMoves(playerColor, entitiesDB) || AnyValidDrops(playerColor, recursionLevel, entitiesDB));
 }