static void Main(string[] args)
        {
            var invalidInputString = "Invalid input!";

            Console.WriteLine("Rock Paper Scissors");

            IPlayer playerOne = null;
            IPlayer playerTwo = null;

            var makeHumanChoice = new Func <HumanPlayer, Core.Rules.RockPaperScissors>(
                (player) =>
            {
                while (true)
                {
                    var playerString = player == playerOne ? "Player One" : "Player Two";

                    Console.WriteLine("Please select either rock, paper or scissors for {0}, press 'R' for Rock, 'P' for Paper or 'S' for Scissors", playerString);
                    var key = Console.ReadKey();
                    Console.WriteLine();
                    if (key.Key == ConsoleKey.R)
                    {
                        return(Core.Rules.RockPaperScissors.Rock);
                    }
                    else if (key.Key == ConsoleKey.P)
                    {
                        return(Core.Rules.RockPaperScissors.Paper);
                    }
                    else if (key.Key == ConsoleKey.S)
                    {
                        return(Core.Rules.RockPaperScissors.Scissors);
                    }
                    else
                    {
                        Console.WriteLine(invalidInputString);
                    }
                }
            }
                );

            IPlayer CreateUser(string message)
            {
                IPlayer player = null;

                while (player == null)
                {
                    Console.WriteLine(message);
                    var key = Console.ReadKey();
                    Console.WriteLine();
                    if (key.Key == ConsoleKey.C)
                    {
                        player = new ComputerPlayer();
                    }
                    else if (key.Key == ConsoleKey.H)
                    {
                        player = new HumanPlayer(makeHumanChoice);
                    }
                    else
                    {
                        Console.WriteLine(invalidInputString);
                    }
                }

                return(player);
            }

            while (true)
            {
                try
                {
                    playerOne = CreateUser("Please select the first player type, press 'C' for Computer or 'H' for Human");
                    playerTwo = CreateUser("Please select the second player type, press 'C' for Computer or 'H' for Human");

                    var game = new NotificationEnabledGame();
                    game.PlayerOneChoice += (sender, args) => { Console.WriteLine("Player One Choice Entered: {0}", Enum.GetName(typeof(Core.Rules.RockPaperScissors), args.PlayerChoice)); };
                    game.PlayerTwoChoice += (sender, args) => { Console.WriteLine("Player Two Choice Entered: {0}", Enum.GetName(typeof(Core.Rules.RockPaperScissors), args.PlayerChoice)); };
                    game.GameResult      += (sender, args) =>
                    {
                        if (args.Result.IsDraw)
                        {
                            Console.WriteLine("Game result: Draw");
                            return;
                        }

                        var winnerString        = (args.Result.Winner == playerOne) ? "Player One" : "Player Two";
                        var winningChoiceString = Enum.GetName(typeof(Core.Rules.RockPaperScissors), args.Result.WinningChoice);

                        Console.WriteLine("Game result: Winner {0}, Winning Choice {1}", winnerString, winningChoiceString);
                    };

                    var session = new Session(game);
                    var result  = session.Play(playerOne, playerTwo, new DefaultGameRules(), 3);

                    if (result.IsDraw)
                    {
                        Console.WriteLine("Session ended in a Draw");
                    }
                    else
                    {
                        Console.WriteLine("Winner of Session is: {0}", result.Winner == playerOne ? "Player One" : "Player Two");
                    }

                    Console.WriteLine("To play again press 'Y', to exit press any other key.");
                    var playAgainKey = Console.ReadKey();
                    Console.WriteLine();
                    if (playAgainKey.Key != ConsoleKey.Y)
                    {
                        break;
                    }
                }
                finally
                {
                    playerOne = null;
                    playerTwo = null;
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            int    ties = 0, playerWins = 0, computerWins = 0;
            string playerInput = "", input = "";

            Dictionary <string, Hands> hands = new Dictionary <string, Hands>
            {
                { "r", Hands.Rock },
                { "p", Hands.Paper },
                { "s", Hands.Scissors }
            };

            IChooser ComputerPlayer;

            while (!(input == "1" || input == "2"))
            {
                Console.WriteLine("Choose the AI to play against:\n(1) Random\n(2) Weighted");
                input = Console.ReadLine();
                Console.Clear();
            }

            if (input == "1")
            {
                ComputerPlayer = new RandomChooser();
            }
            else
            {
                ComputerPlayer = new WeightedChooser();
            }

            while (true)
            {
                Console.WriteLine($"Score\nPlayer wins: {playerWins}\nComputer wins: {computerWins}\nTies: {ties}");
                Console.WriteLine("Choose hand (r=Rock, p=Paper, s=Scissors, q=Quit):");
                playerInput = Console.ReadLine();
                Console.Clear();

                if (playerInput == "q")
                {
                    break;
                }

                if (!hands.ContainsKey(playerInput))
                {
                    Console.Clear();
                    continue;
                }
                Hands playerHand   = hands[playerInput];
                Hands computerHand = ComputerPlayer.Choose(playerHand);
                switch (compareHands(playerHand, computerHand))
                {
                case 0:
                    Console.WriteLine("You won!");
                    playerWins += 1;
                    break;

                case 1:
                    Console.WriteLine("You lost!");
                    computerWins += 1;
                    break;

                case 2:
                    Console.WriteLine("It's a tie");
                    ties += 1;
                    break;
                }
            }
            Console.WriteLine($"Final score\nPlayer wins: {playerWins}\nComputer wins: {computerWins}\nTies: {ties}");
        }