//ANROP: bool ok = SelfTest(); //UPPGIFT: Används vid deuggning. Metoden anropar ala metoder i // klassen och returnerar true om ingen bug hittades. public static bool SelfTest() { bool ok = true; ok = IsValidKey("2361") && !IsValidKey("2368") && !IsValidKey("ABCD") && !IsValidKey("+-*=") && !IsValidKey("2301") && !IsValidKey("23611") && !IsValidKey("231"); System.Diagnostics.Debug.WriteLine("IsValidKey; " + ok); for (int i = 0; i < 1000 && ok; ++i) { ok = IsValidKey(createSecretKey()); } System.Diagnostics.Debug.WriteLine("createSecretKey: " + ok); MasterMindModel model = new MasterMindModel(); ok = IsValidKey(model._secretKey); System.Diagnostics.Debug.WriteLine("MasterMindModell konstruktor: " + ok); ok = ok && ((MatchKeys("1222", "3111")) == new MatchResult(0, 1)); ok = ok && ((MatchKeys("1234", "1234")) == new MatchResult(4, 0)); ok = ok && ((MatchKeys("1234", "4321")) == new MatchResult(0, 4)); ok = ok && ((MatchKeys("1234", "1243")) == new MatchResult(2, 2)); ok = ok && ((MatchKeys("1234", "1212")) == new MatchResult(2, 0)); ok = ok && ((MatchKeys("1234", "5612")) == new MatchResult(0, 2)); ok = ok && ((MatchKeys("1444", "1144")) == new MatchResult(3, 0)); ok = ok && ((MatchKeys("5224", "4334")) == new MatchResult(1, 0)); ok = ok && ((MatchKeys("1223", "2245")) == new MatchResult(1, 1)); System.Diagnostics.Debug.WriteLine("MatchKeys: " + ok); return(ok); }
//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); }
//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); }