Exemplo n.º 1
0
        public void DoShuffle()
        {
            // Remove 3 cup next to actual
            var nextCup1 = actualCup.NextCup;
            var nextCup2 = nextCup1.NextCup;
            var nextCup3 = nextCup2.NextCup;

            actualCup.NextCup = nextCup3.NextCup;

            // Find destination cup: actual minus one value
            var searchedCupNo = actualCup.CupNo - 1;

            if (searchedCupNo < lowestValueOnAnyCup)
            {
                searchedCupNo = highestValueOnAnyCup;
            }
            while (searchedCupNo == nextCup1.CupNo || searchedCupNo == nextCup2.CupNo || searchedCupNo == nextCup3.CupNo)
            {
                searchedCupNo--;
                if (searchedCupNo < lowestValueOnAnyCup)
                {
                    searchedCupNo = highestValueOnAnyCup;
                }
            }

            // Put removed cups to place
            var destinationCup             = cupsList[searchedCupNo];
            var destinationOriginalNextCup = destinationCup.NextCup;

            destinationCup.NextCup = nextCup1;
            nextCup3.NextCup       = destinationOriginalNextCup;

            // Step actual cup one
            actualCup = actualCup.NextCup;
        }
Exemplo n.º 2
0
        public static int[] Play(
            string start,
            int rounds,
            int totalCups = 9)
        {
            var cups = new Dictionary <int, Cup>();

            cups[start[0] - '0'] = new Cup(start[0] - '0');

            for (var i = 1; i < start.Length; i++)
            {
                cups[start[i - 1] - '0'].Next = cups[start[i] - '0'] = new(start[i] - '0');
            }

            if (start.Length < totalCups)
            {
                cups[start[^ 1] - '0'].Next = cups[start.Length + 1] = new Cup(start.Length + 1);
Exemplo n.º 3
0
        public CrabGame(IEnumerable <int> cupsNumbers)
        {
            highestValueOnAnyCup = cupsNumbers.Max();
            lowestValueOnAnyCup  = 1;
            cupsList             = new Cup[highestValueOnAnyCup + 1];
            var startCup = new Cup(cupsNumbers.First());

            cupsList[startCup.CupNo] = startCup;
            var previousCup = startCup;

            foreach (var cupsNumber in cupsNumbers.Skip(1))
            {
                var newCup = new Cup(cupsNumber);
                cupsList[newCup.CupNo] = newCup;
                previousCup.NextCup    = newCup;
                previousCup            = newCup;
            }
            previousCup.NextCup = startCup;
            actualCup           = startCup;
        }