Exemple #1
0
        private void PrivateNewRummy <RR>(ICustomBasicList <RR> output, ICustomBasicList <RR> objectList, int howMany, EnumRummyType whatRummy, bool minOnly, bool noWilds = false, bool minWilds = false)
            where RR : IRummmyObject <S, C>, new()
        {
            CustomBasicList <R> firstTemp = objectList.Cast <R>().ToCustomBasicList();

            GetTempList(firstTemp);
            CheckErrors();
            UseSecond = false;
            int wildsUsed = default;
            int maxWildsUsed;

            maxWildsUsed = 100;
            CustomBasicList <R> wildList;
            CustomBasicList <R> mainList = new CustomBasicList <R>();
            CustomBasicList <R> thisList;

            wildList = _tempList.Where(Items => Items.IsObjectWild == true).ToCustomBasicList();
            int WildsNeeded;
            int x;

            switch (whatRummy)
            {
            case EnumRummyType.Colors:
                var colorList = _tempList.Where(Items => Items.IsObjectIgnored == false && Items.IsObjectWild == false).GroupBy(Items => Items.GetColor).ToCustomBasicList();
                colorList.ForEach(thisColor =>
                {
                    thisList  = new CustomBasicList <R>();
                    int count = thisColor.Count();
                    if (count >= howMany)
                    {
                        thisList.AddRange(thisColor);
                        if (minOnly == false)
                        {
                            thisList.AddRange(wildList);
                        }
                    }
                    else if (minOnly == false & count + wildList.Count >= howMany & HasWild == true)
                    {
                        thisList.AddRange(thisColor);
                        thisList.AddRange(wildList);
                    }
                    else if (minOnly == true & HasWild == true & count + wildList.Count > howMany)
                    {
                        thisList.AddRange(thisColor);
                        WildsNeeded = howMany - count;
                        var loopTo  = WildsNeeded - 1;
                        for (x = 0; x <= loopTo; x++)
                        {
                            thisList.Add(wildList[x]);
                        }
                    }
                    if (thisList.Count > mainList.Count)
                    {
                        mainList = thisList;
                    }
                });
                output.ReplaceRange(mainList.Cast <RR>());
                if (output.Count == objectList.Count & UseAll == false)
                {
                    output.RemoveAt(0);
                }
                return;

            case EnumRummyType.Sets:
                int y;
                var setList = _tempList.Where(Items => Items.IsObjectIgnored == false && Items.IsObjectWild == false).GroupBy(Items => Items.ReadMainValue).ToCustomBasicList();
                setList.ForEach(thisSet =>
                {
                    thisList  = new CustomBasicList <R>();
                    int Count = thisSet.Count();
                    if (Count >= howMany)
                    {
                        thisList.AddRange(thisSet);
                        if (minOnly == true & thisList.Count > howMany)
                        {
                            y           = thisList.Count - 1;
                            var loopTo1 = y;
                            for (x = howMany; x <= loopTo1; x++)
                            {
                                thisList.RemoveAt(0);
                            }
                        }
                        if (minOnly == false & wildList.Count > 0 & minWilds == false)     //they did not do else if here.  hopefully still okay
                        {
                            thisList.AddRange(wildList);
                            wildsUsed = 0;
                        }
                    }
                    else if ((minOnly == false | minWilds == true) & Count + wildList.Count >= howMany & HasWild == true)
                    {
                        thisList.AddRange(thisSet);
                        thisList.AddRange(wildList);
                        wildsUsed = wildList.Count;
                    }
                    else if (minOnly == true & HasWild == true & Count + wildList.Count > howMany)
                    {
                        thisList.AddRange(thisSet);
                        WildsNeeded = howMany - Count;
                        var loopTo2 = WildsNeeded;
                        for (x = 1; x <= loopTo2; x++)
                        {
                            thisList.Add(wildList[x - 1]);
                        }
                    }
                    if (wildsUsed < maxWildsUsed & HasWild == true & setList.Count() == mainList.Count & (minOnly == true | minWilds == true))
                    {
                        mainList = thisList;
                    }
                    else if (thisList.Count > mainList.Count & (HasWild == false | wildsUsed < maxWildsUsed & HasWild == true & (minOnly == true | minWilds == true) | HasWild == true & minOnly == false))
                    {
                        mainList = thisList;
                    }
                });
                output.ReplaceRange(mainList.Cast <RR>());
                if (output.Count == objectList.Count & UseAll == false)
                {
                    output.RemoveAt(0);
                }
                return;

            case EnumRummyType.Runs:
                var ourObjects = objectList.Cast <R>().ToCustomBasicList();
                var finTemp    = StraightSet(ourObjects, howMany, minOnly, wildList, noWilds);
                output.ReplaceRange(finTemp.Cast <RR>());
                if (FirstUsed == 2 & HasSecond == true)
                {
                    S   suit;
                    var suitList = _tempList.Where(Items => Items.IsObjectIgnored == false && Items.IsObjectWild == false).GroupBy(Items => Items.GetSuit).ToCustomBasicList();
                    if (suitList.Count == 0)
                    {
                        throw new BasicBlankException("There are no suits available.  If there are really no suits; then fix this");
                    }
                    suit = suitList.First().Key;
                    var runTemp = objectList.Where(Items => Items.GetSuit.Equals(suit) && Items.GetSecondNumber == 1).FirstOrDefault();
                    if (runTemp != null)
                    {
                        output.InsertBeginning(runTemp);     //maybe this should be inserted at beginning (?)
                    }
                }
                return;

            default:
                throw new BasicBlankException("Cannot figure out what rummy type to do");
            }
        }