public bool GetMove(out Move move)
        {
            move = Move.Rock;
            var answer = proc.Read(true).FirstOrDefault(s => R.TextMoveLegal(s));

            if (String.IsNullOrEmpty(answer))
            {
                return(false);
            }
            answer = answer.ToLower();
            if (answer == "rock")
            {
                move = Move.Rock;
                return(true);
            }
            else if (answer == "paper")
            {
                move = Move.Paper;
                return(true);
            }
            if (answer == "scissors")
            {
                move = Move.Scissors;
                return(true);
            }
            return(false);
        }
Пример #2
0
        static void Main(string[] args)
        {
            var foreground = Console.ForegroundColor;

            try
            {
                if (args.Any() && args[0] == "-d")
                {
                    debugPrint = true;
                }

                var names = R.GetProgramNames();
                int count = names.Count();


                if (count == 0)
                {
                    Console.WriteLine("Cannot find any executables to run");
                    return;
                }
                else
                {
                    Console.WriteLine("Participants: ");
                    foreach (var n in names)
                    {
                        Console.WriteLine($"   {n}");
                        players.Add(new Player {
                            filename = n
                        });
                    }
                }


                // round robin them
                for (var i = 0; i < count; ++i)
                {
                    for (var j = i + 1; j < count; ++j)
                    {
                        var player1 = players[i];
                        var player2 = players[j];
                        PlayGame(player1, player2);
                    }
                }

                // results:
                // todo - sort on win ratio?
                players.Sort((a, b) => - a.WinRatio().CompareTo(b.WinRatio()));

                Console.WriteLine("Win percentages");
                foreach (var p in players)
                {
                    Console.WriteLine($"  {(100 * p.WinRatio()):F0}% {p.filename}");
                }
            }
            catch
            {
                Console.ForegroundColor = foreground;
            }
        }
Пример #3
0
        static void PlayGame(Player player1, Player player2)
        {
            var player1Color = ConsoleColor.Blue;
            var player2Color = ConsoleColor.Red;
            var oldColor     = Console.ForegroundColor;

            Console.ForegroundColor = player1Color;
            Console.Write(player1.filename);
            Console.ForegroundColor = oldColor;
            Console.Write(" vs ");
            Console.ForegroundColor = player2Color;
            Console.Write(player2.filename);
            Console.WriteLine();

            for (var game = 0; game < gamesPerRound; ++game)
            {
                player1.Capture();
                player2.Capture();

                if (debugPrint)
                {
                    player1.pc.color = ConsoleColor.DarkGreen;
                    player2.pc.color = ConsoleColor.DarkBlue;
                }

                player1.pc.Write($"RESET {player2.filename}");
                player2.pc.Write($"RESET {player1.filename}");
                for (var round = 0; round < numRounds; ++round)
                {
                    var ans1 = player1.pc.Read(true).FirstOrDefault(s => R.TextMoveLegal(s));
                    var ans2 = player2.pc.Read(true).FirstOrDefault(s => R.TextMoveLegal(s));

                    player1.pc.Write(ans2);
                    player2.pc.Write(ans1);

                    var result = TallyScore(player1, player2, ans1, ans2);

                    //Console.ForegroundColor = player1Color;
                    //Console.Write(ans1);
                    //Console.ForegroundColor = oldColor;
                    //Console.Write(" vs ");
                    //Console.ForegroundColor = player2Color;
                    //Console.Write(ans2);
                    //Console.ForegroundColor = oldColor;
                    //Console.Write(" =>result: ");
                    //if (result == Result.Player1Win)
                    //    Console.ForegroundColor = player1Color;
                    //else if (result == Result.Player2Win)
                    //    Console.ForegroundColor = player2Color;
                    //else
                    //    Console.ForegroundColor = tieColor;
                    //Console.WriteLine(result);
                }
                player1.pc.Write("QUIT");
                player2.pc.Write("QUIT");
            }
            Console.ForegroundColor = oldColor;
        }
Пример #4
0
        // tally score, get result
        private static Result TallyScore(Player player1, Player player2, string ans1, string ans2)
        {
            var s = FindScore(player1.filename, player2.filename);

            Result result;

            if (!TextToMove(ans1, out Move move1))
            {
                s.player2Wins++; // default by error
                result = Result.Player2Win;
            }
            if (!TextToMove(ans2, out Move move2))
            {
                s.player1Wins++; // default by error
                result = Result.Player1Win;
            }
            else
            {
                result = R.Score(move1, move2);
            }
            switch (result)
            {
            case Result.Player1Win:
                s.player1Wins++;     // default by error
                player1.wins++;
                player2.losses++;
                break;

            case Result.Player2Win:
                s.player2Wins++;     // default by error
                player2.wins++;
                player1.losses++;
                break;

            case Result.Tie:
                s.tie++;     // default by error
                player1.ties++;
                player2.ties++;
                break;
            }
            return(result);
        }