コード例 #1
0
ファイル: InteractiveRunner.cs プロジェクト: rba100/splendor
        public void Run()
        {
            Console.Write("Enter name: ");
            string playerName = Console.ReadLine();

            var ais = new ISpendorAi[] { new StupidSplendorAi("Skynet"), new StupidSplendorAi("Wopr") };
            var names = new[] { playerName }.Concat(ais.Select(a => a.Name)).ToArray();
            var gameState = new DefaultGameInitialiser(new DefaultCards()).Create(names);
            var game      = new Game(gameState);

            while (!game.IsGameFinished) // Process a turn in each iteration
            {
                var     turnPlayer   = game.State.CurrentPlayer;
                var     isPlayerTurn = turnPlayer.Name == playerName;
                IAction action       = null;
                if (isPlayerTurn)
                {
                    PrintState(game);
                    bool turnComplete = false;
                    while (!turnComplete)
                    {
                        Console.CursorLeft = marginLeft;
                        Console.Write(">");
                        var input = Console.ReadLine();
                        try
                        {
                            action = GetActionFromInput(input, game.State);
                            if (action != null)
                            {
                                game.CommitTurn(action);
                                turnComplete = true;
                                Console.Clear();
                            }
                        }catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                else // AI
                {
                    var ai = ais.Single(a => a.Name == game.State.CurrentPlayer.Name);
                    action = ai.ChooseAction(game.State);
                    game.CommitTurn(action);
                }

                var updatedTurnPlayer = game.State.Players.Single(p => p.Name == turnPlayer.Name);
                Console.WriteLine($"{updatedTurnPlayer.Name}, {action}");
            }
            PrintState(game);
            Console.WriteLine("****************************************");
            Console.WriteLine(game.TopPlayer.Name + " wins!");
            Console.WriteLine("****************************************");
        }
コード例 #2
0
ファイル: InteractiveRunner.cs プロジェクト: rba100/splendor
        public void Run()
        {
            ClearAndDrawFrame();
            string playerName = Query("What is your name?");

            var ais = new ISpendorAi[] { new StupidSplendorAi("Skynet"), new StupidSplendorAi("Wopr") };
            var names = new[] { playerName }.Concat(ais.Select(a => a.Name)).ToArray();
            var gameState = new DefaultGameInitialiser(new DefaultCards()).Create(names);
            var game      = new Game(gameState);

            while (!game.IsGameFinished) // Process a turn in each iteration
            {
                var     turnPlayer   = game.State.CurrentPlayer;
                var     isPlayerTurn = turnPlayer.Name == playerName;
                IAction action       = null;
                if (isPlayerTurn)
                {
                    bool   turnComplete = false;
                    string prompt       = "What do you want to do?";
                    var    promptColour = ConsoleColor.Yellow;
                    while (!turnComplete)
                    {
                        ClearAndDrawFrame();
                        PrintState(game);
                        var input = Query(prompt, promptColour);
                        try
                        {
                            action = GetActionFromInput(input, game.State);
                            if (action != null)
                            {
                                game.CommitTurn(action);
                                turnComplete = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            promptColour = ConsoleColor.Red;
                            prompt       = ex.Message;
                        }
                    }
                }
                else // AI
                {
                    var ai = ais.Single(a => a.Name == game.State.CurrentPlayer.Name);
                    action = ai.ChooseAction(game.State);
                    game.CommitTurn(action);
                }

                var updatedTurnPlayer = game.State.Players.Single(p => p.Name == turnPlayer.Name);
                //Console.WriteLine($"{updatedTurnPlayer.Name}, {action}");
            }
            PrintState(game);
            Query(game.TopPlayer.Name + " wins!", ConsoleColor.Red);
        }