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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.