示例#1
0
        // this method runs on a different thread. gameNumber can be changed by clicking chosing a new game from the UI thread.
        private static GameStatus AutoPlayRunner(MinesweeperGame game)
        {
            //long start = DateTime.Now.Ticks;

            SolverActionHeader solverActions;

            GameResult result = game.ProcessActions(firstPlay);

            SolverInfo solverInfo = new SolverInfo(game.description);

            while (result.status == GameStatus.InPlay)
            {
                solverInfo.AddInformation(result);                  // let the solver know what has happened

                solverActions = SolverMain.FindActions(solverInfo); // pass the solver info into the solver and see what it comes up with

                if (solverActions.solverActions.Count == 0)
                {
                    Write("No actions returned by the solver!!");
                    break;
                }

                result = game.ProcessActions(solverActions.solverActions);

                //Write("Game controller returned " + result.actionResults.Count + " results from this action");

                //foreach (SolverAction action in solverActions) {
                //    Write("Playing " + action.AsText() + " probability " + action.safeProbability);
                //}
            }

            //long end = DateTime.Now.Ticks;

            //Write("game took " + (end - start) + " ticks");


            return(result.status);
        }
示例#2
0
        // this method runs on a different thread. gameNumber is changed by clicking chosing a new game from the UI thread.
        private void AutoPlayRunner()
        {
            Dispatcher.Invoke(() => gameCanvas.Cursor = Cursors.Wait);
            logicalLock = true;

            int runningGameNumber = gameNumber;  // remember the game number we are solving

            //Utility.Write("AutoRunning thread starting");

            solverActions = SolverMain.FindActions(solverInfo);   // pass the solver info into the solver and see what it comes up with
            Dispatcher.Invoke(() => RenderHints());

            while (IsAutoPlayValid() && gameNumber == runningGameNumber)
            {
                long start = DateTime.Now.Ticks;

                GameResult result = game.ProcessActions(solverActions.solverActions);

                //Utility.Write("Game controller returned " + result.actionResults.Count + " results from this action");

                long end1 = DateTime.Now.Ticks;
                // do the rendering while we are stil playing the same game
                if (gameNumber == runningGameNumber)
                {
                    gameCanvas.Dispatcher.Invoke(() => RenderResults(result));
                }
                else
                {
                    break;
                }
                //Utility.Write("After RenderResults took " + (DateTime.Now.Ticks - start) + " ticks");

                solverInfo.AddInformation(result);  // let the solver know what has happened

                // nothing more to do if we have won or lost
                if (result.status == GameStatus.Lost || result.status == GameStatus.Won)
                {
                    Dispatcher.Invoke(() => DisplayFinalOutcome(result));
                    break;
                }

                solverActions = SolverMain.FindActions(solverInfo);   // pass the solver info into the solver and see what it comes up with

                //foreach (SolverAction action in solverActions) {
                //    Utility.Write("Playing " + action.AsText() + " probability " + action.safeProbability);
                //}

                //long end2 = DateTime.Now.Ticks;
                // do the rendering while we are stil playing the same game
                if (gameNumber == runningGameNumber)
                {
                    Dispatcher.Invoke(() => RenderHints());
                }
                else
                {
                    break;
                }
                //Utility.Write("After RenderHints took " + (DateTime.Now.Ticks - start) + " ticks");
                Utility.Write("Tiles remaining " + solverInfo.GetTilesLeft());

                long end = DateTime.Now.Ticks;

                int milliseconds = (int)((end - start) / 10000);

                int wait = Math.Max(0, autoPlayMinDelay - milliseconds);

                //Utility.Write("Autoplay processing took " + milliseconds + " milliseconds (" + (end - start) + " ticks)");
                //Console.WriteLine("Autoplay processing took " + milliseconds + " milliseconds");

                if (!IsAutoPlayValid())
                {
                    break;
                }

                Thread.Sleep(wait);  // wait until all the time is used up
            }

            logicalLock = false;
            Dispatcher.Invoke(() => gameCanvas.Cursor = Cursors.Arrow);

            //Utility.Write("AutoRunning thread ending");
        }