Esempio n. 1
0
        static bool canIWin(int maxChoice, int winTotal)
        {
            Scenario.maxChoice = maxChoice;
            Scenario.winTotal  = winTotal;

            return(WinComputer.recursiveCanPlayerAWin(new Scenario(0, 0)));
        }
Esempio n. 2
0
        static bool recursiveCanPlayerAWin(Scenario scenario)
        {
            if (scenario.PlayerAHasWinningChoice())
            {
                return(true);
            }

            for (int i = 0; i <= maxChoice; i++)
            {
                if (scenario.IsInvalidChoice(i))
                {
                    continue;
                }

                scenario.PlayerAChoose(i);

                if (scenario.PlayerBHasWinningChoice())
                {
                    return(false);
                }

                for (int j = 0; j <= maxChoice; j++)
                {
                    if (scenario.IsInvalidChoice(j))
                    {
                        continue;
                    }

                    Scenario branch = scenario.Clone();
                    branch.PlayerBChoose(j);

                    if (ScenarioCache.HasDetermined(branch))
                    {
                        scenario.PlayerAHasWinningStrategy |= ScenarioCache.GetOutcome(branch);
                    }
                    else
                    {
                        scenario.PlayerAHasWinningStrategy |= WinComputer.recursiveCanPlayerAWin(branch);
                    }
                }
            }

            ScenarioCache.Store(scenario);

            return(scenario.PlayerAHasWinningStrategy);
        }