using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Bagels { class Program { private Tools _Tools = new Tools(); private bool _Running; private bool _GameOver; private int[] _Digits = new int[3] { 0, 0, 0 }; private int _Score; private int AskForGuess(int attempt) { while (true) { int guess = _Tools.GetIntInput($"GUESS #{attempt}"); if (guess < 0) Console.WriteLine("PLEASE GUESS A NON-NEGATIVE NUMBER."); else if (guess < 100 || guess > 999) Console.WriteLine("PLEASE GUESS A NUMBER BETWEEN 100 AND 999."); else return guess; } } private void DisplayRules() { Console.WriteLine("I WILL THINK OF A THREE-DIGIT NUMBER. YOU WILL TRY TO"); Console.WriteLine("GUESS MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:"); Console.WriteLine(" PICO - ONE DIGIT CORRECT BUT IN THE WRONG POSITION"); Console.WriteLine(" FERMI - ONE DIGIT CORRECT AND IN THE RIGHT POSITION"); Console.WriteLine(" BAGELS - NO DIGITS ARE CORRECT\r\n"); Console.WriteLine("YOU GET TWENTY TRIES BEFORE YOU LOSE.\r\n"); } private void DisplayGameOver() { Console.WriteLine("OH WELL, THAT WAS TWENTY GUESSES."); Console.WriteLine($"MY NUMBER WAS {MakeIntFromArray(_Digits)}."); _GameOver = true; } private int MakeIntFromArray(int[] digits) { return (digits[0] * 100) + (digits[1] * 10) + digits[2]; } private int[] MakeArrayFromInt(int valIn) { int hundreds = valIn / 100; int tens = (valIn - (hundreds * 100)) / 10; int ones = (valIn - (hundreds * 100)) - (tens * 10); return new int[] { hundreds, tens, ones }; } private void CreateTheNumber() { bool gotDigit = false; _Digits[0] = _Tools.GenerateRandomNumber(1, 9, false); while(!gotDigit) { _Digits[1] = _Tools.GenerateRandomNumber(0, 9, false); if (_Digits[1] != _Digits[0]) gotDigit = true; } gotDigit = false; while (!gotDigit) { _Digits[2] = _Tools.GenerateRandomNumber(0, 9, false); if (_Digits[2] != _Digits[0] && _Digits[2] != _Digits[1]) gotDigit = true; } } private bool CheckGuess(int guess) { int[] temp = MakeArrayFromInt(guess); int[] vals = new int[3] { 0, 0, 0 }; for (int d = 0; d < 3; d++) if (temp[d] == _Digits[d]) vals[d] = 1; if (MakeIntFromArray(vals) == 111) return true; if(vals[0] == 0) if (temp[0] == _Digits[1] || temp[0] == _Digits[2]) vals[0] = 2; if (vals[1] == 0) if (temp[1] == _Digits[0] || temp[1] == _Digits[2]) vals[1] = 2; if (vals[2] == 0) if (temp[2] == _Digits[0] || temp[2] == _Digits[1]) vals[2] = 2; if (MakeIntFromArray(vals) == 0) Console.WriteLine("BAGELS"); else { string display = ""; for(int d = 0;d < 3; d++) { if (vals[d] == 1) display += "FERMI "; else if (vals[d] == 2) display += "PICO "; } Console.WriteLine(display); } return false; } public void Run() { Console.WriteLine("*** BAGELS : C# ***"); int guess = 0; int attempt; _Running = true; _Score = 0; if (_Tools.AskToSeeTheRules()) DisplayRules(); while(_Running) { attempt = 1; _GameOver = false; CreateTheNumber(); while(!_GameOver) { Console.WriteLine("OK, I HAVE A NUMBER IN MIND."); guess = AskForGuess(attempt); if(CheckGuess(guess)) { Console.WriteLine("YOU GOT IT!!!"); _Score++; _GameOver = true; } else { attempt++; if (attempt > 20) DisplayGameOver(); } } Console.WriteLine("*** GAME OVER ***"); _Running = _Tools.AskToPlayAgain(); } Console.WriteLine($"A {_Score} POINT BAGELS BUFF!"); Console.WriteLine("HOPE YOU HAD FUN. BYE!"); } static void Main(string[] args) { Program p = new Program(); p.Run(); Console.ReadLine(); } } }