public int CompGuesses(int[] array) { int guesses = 1; if (array.Length != 1) { Console.WriteLine($"Is it {array[array.Length / 2]}?"); Console.WriteLine("-- 'c' -- Correct -- 'h' -- Too High -- 'l' -- Too Low"); int[] nextArray; ConsoleKey choice = Console.ReadKey().Key; switch (choice) { case ConsoleKey.C: break; case ConsoleKey.H: //reduces options to lower half Console.WriteLine("That's too high? Well then..."); nextArray = new int[array.Length / 2]; for (int i = 0; i < nextArray.Length; i++) { nextArray[i] = array[i]; } guesses += new BisectionAlgorithm().CompGuesses(nextArray); break; case ConsoleKey.L: Console.WriteLine("That's too low? Well then..."); if (array.Length % 2 == 0) //num elements is even; there is no single middle number, but a pair { nextArray = new int[(array.Length / 2) - 1]; for (int i = 0; i < nextArray.Length; i++) { nextArray[i] = array[i + nextArray.Length + 2]; } guesses += new BisectionAlgorithm().CompGuesses(nextArray); } else { nextArray = new int[array.Length / 2]; for (int i = 0; i < nextArray.Length; i++) { nextArray[i] = array[i + nextArray.Length + 1]; } guesses += new BisectionAlgorithm().CompGuesses(nextArray); } break; default: Console.WriteLine("Please enter a valid choice"); break; } } else { Console.WriteLine($"There's only one option left, so it must be {array[array.Length / 2]}!"); } return(guesses); }
public void CompPlays() { Console.WriteLine("Choose a number between 1 and 100! The computer will guess it for you!"); int[] list = new int[100]; for (int i = 0; i < list.Length; i++) { list[i] = i + 1; } int guessesOne = new BisectionAlgorithm().CompGuesses(list); Console.WriteLine($"Number of guesses: {guessesOne}! Let's try again!"); int guessesTwo = new BisectionAlgorithm().CompGuesses(list); Console.WriteLine($"Number of guesses: {guessesTwo}! The average is {(guessesOne + guessesTwo) / 2}!"); }
public void HumanPlay() { Random prng = new Random(); int compuNum = prng.Next(1, 1000); Console.WriteLine("The computer will randomly select a number between 1 and 1000. Try and guess it!"); int guessesOne = 0; for (bool foundCompuNum = false; !foundCompuNum;guessesOne++) { userInput = EnforceBounds(1, 1000); Console.WriteLine($"You chose :{userInput}"); foundCompuNum = new BisectionAlgorithm().CheckUserInput(userInput, compuNum); } Console.WriteLine($"Congratulations! Number of guesses: {guessesOne}! Let's try again!"); compuNum = prng.Next(1, 1000); Console.WriteLine("The computer will randomly select a number between 1 and 1000. Try and guess it!"); int guessesTwo = 0; for (bool foundCompuNum = false; !foundCompuNum; guessesTwo++) { userInput = EnforceBounds(1, 1000); Console.WriteLine($"You chose :{userInput}"); foundCompuNum = new BisectionAlgorithm().CheckUserInput(userInput, compuNum); } Console.WriteLine($"Congratulations! Number of guesses: {guessesTwo}! Your average is {(guessesOne+guessesTwo)/2}!"); }
public bool Bisect(int userInput, int[] array) {//searches for users number bool correct = false; if (array.Length % 2 == 0)//num elements is even; there is no single middle number, but a pair { Console.WriteLine($"Is it {array[array.Length / 2]}?"); if (userInput == array[array.Length / 2])//the higher of the pair { correct = true; } else if (userInput < array[array.Length / 2]) { Console.WriteLine("That's too high? Well then..."); int[] nextArray = new int[array.Length / 2]; for (int i = 0; i < nextArray.Length; i++) { nextArray[i] = array[i]; } correct = new BisectionAlgorithm().Bisect(userInput, nextArray); } else if (userInput > array[array.Length / 2]) { Console.WriteLine("That's too low? Well then..."); int[] nextArray = new int[(array.Length / 2) - 1]; for (int i = 0; i < nextArray.Length; i++) { nextArray[i] = array[i + nextArray.Length + 2]; } correct = new BisectionAlgorithm().Bisect(userInput, nextArray); } } else //num elements is odd; (array.length/2) gives the middle index as index n gives the n+1th element { Console.WriteLine($"Is it {array[array.Length / 2]}?"); if (userInput == array[array.Length / 2]) { correct = true; } else if (userInput < array[array.Length / 2]) { Console.WriteLine("That's too high? Well then..."); int[] nextArray = new int[array.Length / 2]; for (int i = 0; i < nextArray.Length; i++) { nextArray[i] = array[i]; } correct = new BisectionAlgorithm().Bisect(userInput, nextArray); } else if (userInput > array[array.Length / 2]) { Console.WriteLine("That's too low? Well then..."); int[] nextArray = new int[array.Length / 2]; for (int i = 0; i < nextArray.Length; i++) { nextArray[i] = array[i + nextArray.Length + 1]; } correct = new BisectionAlgorithm().Bisect(userInput, nextArray); } } return(correct); }