Jump to content

What number am I thiniking of


goocharlton

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/122471-what-number-am-i-thiniking-of/
Share on other sites

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.

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;
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.