//ANROP:    int i  = playGame();
        //UPPGIFT:  returner antal försök uppgiften löstes på;
        //public static int playGame()
        public int playGame()
        {
            MasterMindConsolView mv = new MasterMindConsolView();
            MasterMindModel      mm = new MasterMindModel();

            mv.Reset();

            int    guesses   = 0;
            string secretKey = mm.SecretKey;
            bool   correct   = false;

            while (!correct)
            {
                guesses++;
                mv.EraseUsersTestKey();
                string testKey      = mv.GetUsersTestKey();
                bool   inputIsValid = MasterMindModel.IsValidKey(testKey);
                while (!inputIsValid)
                {
                    Console.SetCursorPosition(10, 16);
                    Console.Write("Felaktig inmatning");
                    mv.EraseUsersTestKey();
                    testKey      = mv.GetUsersTestKey();
                    inputIsValid = MasterMindModel.IsValidKey(testKey);
                }

                Console.SetCursorPosition(10, 16);
                Console.Write("                        ");

                MatchResult mr = MasterMindModel.MatchKeys(secretKey, testKey);
                mv.ShowTestKey(11 - guesses, MasterMindConsolView.insertSpaces(testKey));
                mv.ShowTestResult(11 - guesses, mr, true);

                if (mr.NumCorrect == 4)
                {
                    correct = true;
                }
                if (guesses == 10)
                {
                    guesses++;
                    correct = true;
                }
            }
            Console.SetCursorPosition(16, 20);
            Console.Write("Rätt svar: " + secretKey);

            return(guesses);
        }
示例#2
0
        //ANROP:    bool ok = SelfTest();
        //UPPGIFT:  Kör alla självtesterna och returnerar true om alla lyckas
        static bool SelfTest()
        {
            bool ok = MasterMindModel.SelfTest() &&
                      MasterMindConsolView.SelfTest() &&
                      MasterMindController.SelfTest();

            if (ok)
            {
                System.Diagnostics.Debug.WriteLine("Alla självtesterna lyckades");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Fel upptäcktes av slälvtest");
            }
            return(ok);
        }