//computer plays game by itself public static void GuessNumber(int[] array, int inputValue) { int iterationCount = 0; string output = string.Empty; double maxIteration = Math.Log(array.Length, 2), median = 0; Console.WriteLine($">> int value = {inputValue}"); while (array != null && iterationCount <= maxIteration) { Console.WriteLine("\n>> bisection_search(value, list)"); GenerateArray.printArray(array); int sum = 0; for (int i = 0; i < array.Length; i++) { sum += array[i]; } median = (double)sum / array.Length; if (inputValue == (int)median) { iterationCount++; Console.WriteLine("The value searched for, {0}, has been found", inputValue); break; } else { if (inputValue > median) { Console.WriteLine($"value is higher than {(int)median}"); array = array.Skip((array.Length / 2)).ToArray(); iterationCount++; } else if (inputValue < median) { Console.WriteLine($"value is lower than {(int)median}"); array = array.Take(array.Length / 2).ToArray(); iterationCount++; } if (median % 1.0 == 0) { Console.WriteLine($"The middle value is {median}"); } else if (median % 1.0 != 0) { Console.WriteLine($"The middle value is {median}, but we are doing integer division"); } } } }
//computer set a random number, human make guess public static int GuessNumber(int[] array) { Console.WriteLine("\nComputer will pick a number, you make guess"); int iterationCount = 0, inputValue = 0, newArray = 0; double maxIteration = Math.Log(array.Length, 2); int computerRandom = Program.selectValue(array.Length); while (array != null && iterationCount < maxIteration) { GenerateArray.printArray(array); inputValue = int.Parse(Console.ReadLine()); if (inputValue == computerRandom) { iterationCount++; Console.WriteLine("The value searched for, {0}, has been found", computerRandom); break; } else { for (int i = 0; i < array.Length; i++) { if (inputValue == array[i]) { newArray = i; } } if (inputValue < computerRandom) { Console.WriteLine($"value is higher than {inputValue}"); array = array.Skip(newArray + 1).ToArray(); iterationCount++; } else if (inputValue > computerRandom) { Console.WriteLine($"value is lower than {inputValue}"); array = array.Take(newArray).ToArray(); iterationCount++; } } if (iterationCount > maxIteration) { Console.WriteLine("You ran out of chance"); } } return(iterationCount); }
//User pick a number, then computer will show steps to guess that number //Made this method to guess the number using Random public static int GuessUserNumber(int[] array) { Console.WriteLine($"\n\nYou pick a number, computer will guess.\nPick a random number between {array[0]} - {array[array.Length - 1]}"); int inputValue = int.Parse(Console.ReadLine()); Random r = new Random(); int iterationCount = 0, newArray = 0, computerRandom; double maxIteration = Math.Log(array.Length, 2); Console.WriteLine($">> int value = {inputValue}"); while (array != null && iterationCount <= maxIteration) { Console.WriteLine("\n>> bisection_search(value, list)"); GenerateArray.printArray(array); int sum = 0; for (int i = 0; i < array.Length; i++) { sum += array[i]; } computerRandom = r.Next(array[0], array[array.Length - 1]); if (inputValue == computerRandom) { iterationCount++; Console.WriteLine("The value searched for, {0}, has been found", inputValue); break; } else { Console.WriteLine($"System choose {computerRandom}"); Console.Write($"Was your number higher or lower than {computerRandom}? up ? down?: "); string direction = Console.ReadLine(); for (int i = 0; i < array.Length; i++) { if (computerRandom == array[i]) { newArray = i; } } if (direction == "up") { Console.WriteLine($"value is higher than {computerRandom}"); array = array.Skip(newArray + 1).ToArray(); iterationCount++; } else if (direction == "down") { Console.WriteLine($"value is lower than {computerRandom}"); array = array.Take(newArray).ToArray(); iterationCount++; } } if (iterationCount > maxIteration) { Console.WriteLine("System ran out of chance"); } } return(iterationCount); }