Exemplo n.º 1
0
        private bool CanSelectGridItem(GridItemView gridItem)
        {
            if (gridItem == null || gridItem.IsEmpty)
            {
                return(false);                  // invalid
            }
            if (_selectedGridItems.Count == 0)
            {
                return(true);                   // first selection
            }
            var itemConfig = GameConfig.GetGridItem(gridItem.ID);

            if (itemConfig.MatchType == GridItemMatchType.None)
            {
                return(false);
            }

            var previousItemConfig = _selectedGridItems[_selectedGridItems.Count - 1].GridItemConfig;

            if (itemConfig.MatchType == GridItemMatchType.Category || previousItemConfig.MatchType == GridItemMatchType.Category)
            {
                if (itemConfig.Category != previousItemConfig.Category)
                {
                    return(false);
                }
            }
            else if (itemConfig.MatchType == GridItemMatchType.Exact)
            {
                if (itemConfig.ID != previousItemConfig.ID)
                {
                    return(false);
                }
            }

            if (_selectedGridItems.Contains(gridItem))
            {
                return(false);                  // already selected
            }
            var matchRules = GameConfig.GetMatchRules(_levelConfig.matchRules);

            if (matchRules.MaxSelection > matchRules.MinSelection && _selectedGridItems.Count > matchRules.MaxSelection)
            {
                return(false);
            }

            if (GridIndex.IsNeighbor(gridItem.Index, _selectedGridItems[_selectedGridItems.Count - 1].Index))
            {
                return(true);                   // must neighbor the last selected item
            }
            return(false);
        }
Exemplo n.º 2
0
        private bool IsValidMatch(List <GridIndex> indices, out List <Tuple <int, GridNodeState> > matchedItems)
        {
            var matchRules = GameConfig.GetMatchRules(Config.matchRules);

            matchedItems = null;

            if (indices == null)
            {
                return(false);
            }

            if (indices.Count < matchRules.MinSelection)
            {
                return(false);
            }

            if (matchRules.MaxSelection > matchRules.MinSelection && indices.Count > matchRules.MaxSelection)
            {
                return(false);
            }

            int pointsMultiplier = 1;

            var matchedNodes = new List <GridNodeState>(indices.Count);

            for (int i = 0; i < indices.Count; i++)
            {
                var matchedNode = TryGetGridNodeState(indices[i]);
                if (matchedNode == null || !matchedNode.IsFilled())
                {
                    return(false);                    // must select valid items
                }
                matchedNodes.Add(matchedNode);

                pointsMultiplier *= matchedNode.GridNodeConfig.MatchPointsMatchMultiplier;
            }

            matchedItems = new List <Tuple <int, GridNodeState> >(indices.Count)
            {
                new Tuple <int, GridNodeState> (
                    (matchRules.MatchPointsBase + matchedNodes[0].GridItemConfig.MatchPoints) * matchedNodes[0].GridNodeConfig.MatchPointsItemMultiplier * pointsMultiplier,
                    matchedNodes[0]
                    )
            };

            var previousItem = matchedNodes[0];

            for (int i = 1; i < matchedNodes.Count; i++)
            {
                var item       = TryGetGridNodeState(indices[i]);
                var itemConfig = GameConfig.GetGridItem(item.ItemId);

                if (itemConfig.MatchType == GridItemMatchType.None)
                {
                    return(false);
                }

                if (itemConfig.MatchType == GridItemMatchType.Category || previousItem.GridItemConfig.MatchType == GridItemMatchType.Category)
                {
                    if (itemConfig.Category != previousItem.GridItemConfig.Category)
                    {
                        return(false);
                    }
                }
                else if (itemConfig.MatchType == GridItemMatchType.Exact)
                {
                    if (item.ItemId != previousItem.GridItemConfig.ID)
                    {
                        return(false);
                    }
                }

                if (!GridIndex.IsNeighbor(item.Index, previousItem.Index))
                {
                    return(false);                      // must select neighbors
                }
                matchedItems.Add(new Tuple <int, GridNodeState> (
                                     (matchRules.MatchPointsBase + matchedNodes[i].GridItemConfig.MatchPoints + (matchRules.MatchPointsIncrement * i)) * matchedNodes[i].GridNodeConfig.MatchPointsItemMultiplier * pointsMultiplier,
                                     matchedNodes[i]
                                     ));

                previousItem = item;
            }

            if (matchRules.WordCheck)
            {
                var word = new StringBuilder();
                foreach (var item in matchedItems)
                {
                    word.Append((char)GameConfig.GetGridItem(item.Item2.ItemId).MatchIndex);
                }

                if (!WordMap.Words.FindWord(word.ToString().ToLower(), out var wordTypes))
                {
                    return(false);
                }
            }

            return(true);
        }