Exemple #1
0
        private void RankPlayers(List <CompetitionPosition> positions, int[] randomPositions, List <Player> players, List <int> availablePositions,
                                 List <int> rankedPositions)
        {
            var randomizer = new RandomPositionGenerator(randomPositions);

            while (players.Count > 0 && randomizer.CanTake())
            {
                var position = randomizer.Take();
                RankPlayer(positions, position, players, availablePositions, rankedPositions);
            }
        }
Exemple #2
0
        public PositioningItem[] Rank(int actualPlayersCount)
        {
            var playersCount = CalculatePlayersCount(actualPlayersCount);

            var codes = new Queue <string>();

            for (var i = 0; i < actualPlayersCount; i++)
            {
                codes.Enqueue("Q" + (i + 1));
            }


            if (actualPlayersCount < playersCount)
            {
                for (var i = 0; i < (playersCount - actualPlayersCount); i++)
                {
                    codes.Enqueue("BYE");
                }
            }

            var items  = new List <PositioningItem>(playersCount);
            var skip   = playersCount / 8;
            var first  = new List <int>();
            var second = new List <int>();
            var third  = new List <int>();

            for (var i = 0; i < playersCount; i += skip)
            {
                items[i] = new PositioningItem()
                {
                    Index = i, Code = codes.Dequeue()
                };
                first.Add(i);
                second.Add(i + 2);
                second.Add(i + 4);
                third.Add(i + 3);
                third.Add(i + 5);
            }
            var oddPlaces = new List <int>();

            for (var i = skip - 1; i < playersCount; i += skip)
            {
                oddPlaces.Add(i);
            }
            first.AddRange(oddPlaces);

            var randomizer = new RandomPositionGenerator(oddPlaces.ToArray());

            while (randomizer.CanTake())
            {
                var i = randomizer.Take();
                items[i] = new PositioningItem()
                {
                    Index = i, Code = codes.Dequeue()
                };
            }

            randomizer = new RandomPositionGenerator(second.ToArray());
            while (randomizer.CanTake())
            {
                var i = randomizer.Take();
                items[i] = new PositioningItem()
                {
                    Index = i, Code = codes.Dequeue()
                };
            }

            randomizer = new RandomPositionGenerator(third.ToArray());
            while (randomizer.CanTake())
            {
                var i = randomizer.Take();
                items[i] = new PositioningItem()
                {
                    Index = i, Code = codes.Dequeue()
                };
            }

            var available = Enumerable.Range(0, playersCount).Except(first).Except(second).Except(third);

            randomizer = new RandomPositionGenerator(available.ToArray());
            while (randomizer.CanTake())
            {
                var i = randomizer.Take();
                items[i] = new PositioningItem()
                {
                    Index = i, Code = codes.Dequeue()
                };
            }



            //for (int i = 0; i < playersCount; i++)
            //{
            //    var item = new PositioningItem() {Index = i};
            //    if (i%2 == 0)
            //    {
            //        item.Code = codes.Dequeue();
            //    }
            //    items.Add(item);
            //}



            //var empty = items.Where(i => string.IsNullOrEmpty(i.Code));
            //var randomizer = new RandomPositionGenerator(empty.Select(i=>i.Index).ToArray());
            //while (randomizer.CanTake())
            //{
            //    items[randomizer.Take()].Code = codes.Dequeue();
            //}

            return(items.ToArray());
        }
        public PositioningItem[] Rank(int actualPlayersCount, int qPlayers)
        {
            var playersCount  = PlayersCountCalculator.CalculatePlayersCount(actualPlayersCount);
            var rankedPlayers = playersCount / 4;


            var items = new List <PositioningItem>(playersCount);

            for (int i = 0; i < playersCount; i++)
            {
                items.Add(new PositioningItem()
                {
                    Index = i
                });
            }

            var codes = new Queue <string>();

            for (var i = 0; i < actualPlayersCount - qPlayers; i++)
            {
                codes.Enqueue("MD" + (i + 1));
            }

            for (int i = 0; i < qPlayers; i++)
            {
                codes.Enqueue("Q" + (i + 1));
            }

            if (actualPlayersCount < playersCount)
            {
                for (var i = 0; i < (playersCount - actualPlayersCount); i++)
                {
                    codes.Enqueue("BYE");
                }
            }

            var vsRanked = new Stack <int>();

            items[0].Code = codes.Dequeue();
            items.Last().Code = codes.Dequeue();
            vsRanked.Push(1);
            vsRanked.Push(items.Count - 2);

            var randomizer = default(RandomPositionGenerator);

            if (rankedPlayers > 2)
            {
                var p1 = items.Count / 4;
                var p2 = (items.Count - 1) - items.Count / 4;
                randomizer = new RandomPositionGenerator(new[] { p1, p2 });
                items[randomizer.Take()].Code = codes.Dequeue();
                items[randomizer.Take()].Code = codes.Dequeue();
                vsRanked.Push(p1 + 1);
                vsRanked.Push(p2 - 1);
            }

            if (rankedPlayers > 4)
            {
                var p = new[]
                {
                    items.Count / 2 - 1,
                    items.Count / 2,
                    items.Count / 4 - 1,
                    (items.Count) - items.Count / 4,
                };
                randomizer = new RandomPositionGenerator(p);
                items[randomizer.Take()].Code = codes.Dequeue();
                items[randomizer.Take()].Code = codes.Dequeue();
                items[randomizer.Take()].Code = codes.Dequeue();
                items[randomizer.Take()].Code = codes.Dequeue();

                vsRanked.Push(p[0] - 1);
                vsRanked.Push(p[1] + 1);
                vsRanked.Push(p[2] - 1);
                vsRanked.Push(p[3] + 1);
            }


            var emptySlots = items.Where(item => string.IsNullOrEmpty(item.Code));
            var positions  = emptySlots.Select(i => i.Index).Except(vsRanked).ToList();

            randomizer = new RandomPositionGenerator(positions.ToArray());

            while (randomizer.CanTake())
            {
                items[randomizer.Take()].Code = codes.Dequeue();
            }
            if (rankedPlayers > 4)
            {
                positions = new List <int>();
                positions.Add(vsRanked.Pop());
                positions.Add(vsRanked.Pop());
                positions.Add(vsRanked.Pop());
                positions.Add(vsRanked.Pop());
                randomizer = new RandomPositionGenerator(positions.ToArray());
                while (randomizer.CanTake())
                {
                    items[randomizer.Take()].Code = codes.Dequeue();
                }
            }

            if (rankedPlayers > 2)
            {
                positions = new List <int>();
                positions.Add(vsRanked.Pop());
                positions.Add(vsRanked.Pop());
                randomizer = new RandomPositionGenerator(positions.ToArray());
                while (randomizer.CanTake())
                {
                    items[randomizer.Take()].Code = codes.Dequeue();
                }
            }

            items[vsRanked.Pop()].Code = codes.Dequeue();
            items[vsRanked.Pop()].Code = codes.Dequeue();

            return(items.ToArray());
        }