Пример #1
0
        private static void StartTournament()
        {
            Console.WriteLine("Inform the number of players (4 or 8):");
            short players;

            if (!short.TryParse(Console.ReadKey().KeyChar.ToString(), out players))
            {
                Console.WriteLine("Invalid input.");
                StartUp();
                return;
            }
            if (players != 4 && players != 8)
            {
                Console.WriteLine("Invalid input.");
                StartUp();
                return;
            }
            Console.WriteLine(string.Empty);
            RpsPlayer winner;

            try
            {
                List <TournamentBracket> tournamentBracketList = new List <TournamentBracket>()
                {
                    new TournamentBracket()
                };
                for (var i = 0; i < players; i++)
                {
                    Console.WriteLine($"Inform the name of Player {i + 1}:");
                    var name = Console.ReadLine();
                    Console.WriteLine(string.Empty);
                    Console.WriteLine($"Inform the choice for Player {i + 1} [R/P/S]:");
                    var choice = Console.ReadKey().KeyChar.ToString();
                    Console.WriteLine(string.Empty);
                    var player = new RpsPlayer(name, choice);
                    if (!tournamentBracketList.Last().AddPlayer(player))
                    {
                        tournamentBracketList.Add(new TournamentBracket());
                        tournamentBracketList.Last().AddPlayer(player);
                    }
                }
                var tournament = new Tournament.Tournament(tournamentBracketList);
                var game       = new RpsGame();
                winner = game.GetTournamentWinner(tournament);
            }
            catch (Exception e)
            {
                Console.WriteLine($"**** ERROR: {e.Message}");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("The winner is:");
            Console.WriteLine(winner.PlayerName);
            Console.WriteLine($"With his choice of {winner.Choice}");
            Console.ReadKey();
        }
Пример #2
0
        /*
         * No arquivo PDF tem um requisito que diz o seguinte:
         *  You can assume that the initial tournament is well-formed (that is, there are 2^n
         *  players, and each one participates in exactly one match per round)
         * Portanto, eu não considerei no código que o torneio pudesse ter 3 ou 5 chaves de torneio (TournamentBracket), por exemplo.
         * Há um teste unitário validando o resultado proposto pelo arquivo pdf.
         *
         * O jogo pode ser executado e testado via console ou pelo projeto Tests.
         */

        public RpsPlayer GetTournamentWinner(Tournament.Tournament tournament)
        {
            RpsPlayer        winner       = null;
            List <RpsPlayer> phaseWinners = new List <RpsPlayer>();

            foreach (var bracket in tournament.Brackets)
            {
                var winner1 = GetWinner(bracket.Players1);
                if (!bracket.Players2.Any())
                {
                    //last tournament match
                    winner = winner1;
                    break;
                }
                var winner2 = GetWinner(bracket.Players2);
                phaseWinners.Add(winner1);
                phaseWinners.Add(winner2);
            }
            if (winner != null)
            {
                return(winner);
            }
            var nextBrackets = new List <TournamentBracket>()
            {
                new TournamentBracket()
            };

            foreach (var phaseWinner in phaseWinners)
            {
                if (nextBrackets.Last().AddPlayer(phaseWinner))
                {
                    continue;
                }
                nextBrackets.Add(new TournamentBracket());
                nextBrackets.Last().AddPlayer(phaseWinner);
            }
            Tournament.Tournament nextTournamentPhase = new Tournament.Tournament(nextBrackets);
            winner = GetTournamentWinner(nextTournamentPhase);
            return(winner);
        }