goocharlton Posted September 3, 2008 Share Posted September 3, 2008 Any ideas on how I can build an application that guesses the number that people are thinking of? I figured the way to do it it to get the script to ask questions that the user asks yes or no too till it gets to a small enough possibility to get the exact number. I.e. The script will ask, "Is you number 50 or higher?" -- if yes "Is your number 75 or higher? ---- if yes "Is your number..." ---- if no"Is your number..." -- if no "Is your number 25 or higher? ---- if yes "Is your number..." ---- if no"Is your number..." ...etc I can easily run the script in a way that goes through every single possibility and possible path running if and else but that will take days of going through every single possibility, can you think of a better way of doing it? Possibly using an array to array the possibilities > what are your thoughts? Quote Link to comment Share on other sites More sharing options...
zq29 Posted September 3, 2008 Share Posted September 3, 2008 I'd imagine you'd need to store the scripts guesses and the users input based on those guesses in an array, and calculate the next guess based on the information stored in the array. The user should have three options to click after the scripts guess: Higher, lower and correct. Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 Some pseudocode for guessing algorithm. Untested but should work. For storing ranges you should probably use session. $rangeLow = 0; $rangeHigh = 100; while($response != "correct") { $guess = $rangeLow + ($rangeHigh - $rangeLow) / 2; switch($response) { case "high": //number is higher than guess $rangeLow = ($rangeHigh - $rangeLow) / 2; case "low": // number is lower than guess $rangeHigh = ($rangeHigh - $rangeLow) / 2; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.