Ejemplo n.º 1
0
        public static async Task Main(string[] args)
        {
            try
            {
                Print("2048 bot launched!");
                Print("Ready to beat high scores!");

                IKernel container = BuildContainer();

                IAutomatingControler controler = container.Get <IAutomatingControler>();
                await controler.OpenWebPage();

                IDecisionMaker decisionMaker = container.Get <IDecisionMaker>();
                IGridUpdater   gridUpdater   = container.Get <IGridUpdater>();

                while (true)
                {
                    Stopwatch watch = Stopwatch.StartNew();

                    LineBreak();
                    Print("Starting a new game");

                    Grid grid = new Grid();

                    while (!controler.DetectGameOver())
                    {
                        IEnumerable <GridUpdateInput> updateInputs = controler.ReadGridState();
                        gridUpdater.UpdateGrid(grid, updateInputs);

                        Direction nextDirection = decisionMaker.ChoseDirection(grid);
                        Print($"Moving {nextDirection}");
                        await controler.NextStep(nextDirection);
                    }

                    watch.Stop();

                    long duration = watch.ElapsedMilliseconds;
                    int  score    = controler.ReadScore();

                    Print($"Game finished in {duration}");
                    Print($"Score: {score}");

                    await controler.Replay();
                }
            }
            catch (Exception ex)
            {
                PrintError(ex);
                ExitOnClick();
            }
        }