+ Reply to Thread
Results 1 to 8 of 8

Thread: Number guesser

  1. #1
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Smile Number guesser

    So, after writing my first tutorial, I was quite happy with how it turned out, so im trying my hand at another one. I wrote this in class today for a friend, and cleaned it up a little when i got home. so here it is, the number guesser. The user thinks of a number between 1 and 100 and the computer guesses the number in 7 or less guesses (sounds impressive? ). So heres the code, HTML and all.
    Code:
    <html>
    <head>
    <title>Number Guesser</title>
    script>
    var max=101            //So heres our variables. Max, min, (both used for calculating next guess 
    var min=0              //the computers guess and amount of guesses the user has taken
    var cGuess=50;         //
    var Guesses=1;
    So, we have our html tags and our first variables, I'm hoping they are pretty self explanatory, max and min are the maximum and minimum values the computer can see the users number as being (well, not quite the min would actually be one more than whatever the min variable says, and the max is one less than the max variable, but this helps when finding the number in between the two). cGuess is the computers guess of what the users number is, default 50 (halfway between 1 and 100). and guesses just keeps track of how many guesses the computer takes (should never go over 7, hopefully... ...)
    Code:
    function Guess(n){		//okay, so we call this function when the user presses one of our 
      if(n==1){		//buttons if n==1 the user presses "Higher" in which case we make  
       min=cGuess		//theminimum value what the computer guess was
      }
      else{			//n!=1 (n==2) then the user pressed "Lower", thus a similar idea
       max=cGuess
      }
      Guesses=Guesses+1	//user made a guess, increase guesses
      cGuess=Math.floor((max+min)/2) //guess=the average of the max and min (this is why we use 101
      Guesser.value=cGuess		//not 100 as max, makes this start with better numbers
      guesses.value=Guesses		//enter the values into the inputs
    }
    The first function. Guess is called when a user presses either the "Higher" or "Lower" buttons (n==1 when they press higher and 2 when they press lower). We increase guesses by one and work out our new guess based on whether our guess was too high or too low. math.floor() is a function that returns the the highest integer less than or equal to the number that is enter. max+min/2 gives the number in between the maximum answer and the minimum and maximum answer.
    Code:
    function Start(){		//called when the page loads and when the user wants to start 
      max=101			//again. Refreshes variables, and inputs and is ready for
      min=0				//user to start guessing again.
      cGuess=50;
      Guesses=1;
      Guesser.value=cGuess
      guesses.value=Guesses
    }
    This is the second and last function. (called "start" ironically ) it is called when the page loads and when the user wants to try again. It refreshes variables and displays the new values in input boxes. Simple as that. Now for the HTML
    Code:
    </script>
    </head>
    
    <body bgcolor=black onload=Start()>
    <center><h1><font color=red>Number Guesser
    <table border=5 bordercolor=gold>
     <tr>
      <td colspan=2>
       <center>
       <font color=red size=4>
        Think of a number between 0 and 100. <br>Now the computer is going to guess it in 7 or less     guesses.
      </td>
     </tr>
     <tr>
      <td width=50%>
      <center>
       <font color=red size=4>
        No. Guesses:
      </td>
      <td>
       <center>
        <input value=1 id=guesses size=5>		<!--guesses the user has taken-->
      </td>
     </tr>
     <tr>
      <td>
       <center>
        <font color=red size=4>
         Computer Guess:
      </td>
      <td>
       <center>
        <input value=50 id=Guesser size=5>		<!--the computers guess-->
      </td>
     </tr>
     <tr>
      <td>
       <center>
        <input type=button value=Higher onclick=Guess(1)>		<!--//"Higher" button-->
      </td>
      <td>
       <center>
        <input type=button value=Lower onclick=Guess(2)>		<!--"Lower" button-->
      </td>
     </tr>
     <tr>
      <td colspan=2>
       <center>
        <input type=button value="Start Again" onclick=Start()>	<!--"start again" button-->
      </td>
     </tr>
    </table>
    </body>
    </html>
    .
    So, this just draws up our table with input boxes for computers guess, number of guesses, buttons for "higher", "lower" and "start again" and just some brief instructions for the user.

    Well, hopefully i didnt skip over too much, im not sure what level of user this is supposed to be aimed at. if anyone needs anything explained further, i'd me more than happy to help.

    Till next time, cyah!
    (Oh, heres the full code):
    Code:
    <html>
    <head>
    <title>Number Guesser</title>
    <!--OK, so heres a nice little spin-off of the old "guess the number game". In this one, the user thinks of a number and the computer guesses it. This is a very simple program that makes use of only a couple of javascript commands. Functions, variables, math.floor(), and an algorithm for finding the users guess quickly. Coded by Luke Batchelor-->
    <script>
    var max=101            //So heres our variables. Max, min, (both used for calculating next guess 
    var min=0              //the computers guess and amount of guesses the user has taken
    var cGuess=50;         //
    var Guesses=1;
    
    function Guess(n){		//okay, so we call this function when the user presses one of our 
      if(n==1){		//buttons if n==1 the user presses "Higher" in which case we make  
       min=cGuess		//theminimum value what the computer guess was
      }
      else{			//n!=1 (n==2) then the user pressed "Lower", thus a similar idea
       max=cGuess
      }
      Guesses=Guesses+1	//user made a guess, increase guesses
      cGuess=Math.floor((max+min)/2) //guess=the average of the max and min (this is why we use 101
      Guesser.value=cGuess		//not 100 as max, makes this start with better numbers
      guesses.value=Guesses		//enter the values into the inputs
    }
    
    function Start(){		//called when the page loads and when the user wants to start 
      max=101			//again. Refreshes variables, and inputs and is ready for
      min=0				//user to start guessing again.
      cGuess=50;
      Guesses=1;
      Guesser.value=cGuess
      guesses.value=Guesses
    }
    </script>
    </head>
    
    <body bgcolor=black onload=Start()>
    <center><h1><font color=red>Number Guesser
    <table border=5 bordercolor=gold>
     <tr>
      <td colspan=2>
       <center>
       <font color=red size=4>
        Think of a number between 0 and 100. <br>Now the computer is going to guess it in 7 or less     guesses.
      </td>
     </tr>
     <tr>
      <td width=50%>
      <center>
       <font color=red size=4>
        No. Guesses:
      </td>
      <td>
       <center>
        <input value=1 id=guesses size=5>		<!--guesses the user has taken-->
      </td>
     </tr>
     <tr>
      <td>
       <center>
        <font color=red size=4>
         Computer Guess:
      </td>
      <td>
       <center>
        <input value=50 id=Guesser size=5>		<!--the computers guess-->
      </td>
     </tr>
     <tr>
      <td>
       <center>
        <input type=button value=Higher onclick=Guess(1)>		<!--//"Higher" button-->
      </td>
      <td>
       <center>
        <input type=button value=Lower onclick=Guess(2)>		<!--"Lower" button-->
      </td>
     </tr>
     <tr>
      <td colspan=2>
       <center>
        <input type=button value="Start Again" onclick=Start()>	<!--"start again" button-->
      </td>
     </tr>
    </table>
    </body>
    </html>

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Number guesser

    Not bad! I noticed that some of your lines have ending syntax and some do not:

    Code:
    var max=101            //So heres our variables. Max, min, (both used for calculating next guess 
    var min=0              //the computers guess and amount of guesses the user has taken
    var cGuess=50;         //
    var Guesses=1;
    You should choose to add the ; or not and be consistent. I prefer to have them on all of my JS code.

    +rep!

  4. #3
    Hot_Milo23's Avatar
    Hot_Milo23 is offline Programmer
    Join Date
    Jun 2009
    Location
    Western Australia
    Posts
    120
    Rep Power
    11

    Re: Number guesser

    haha, yes, soz. A bit of a rushed effort. I haven't written JS in over a year till this morning. haha.

  5. #4
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Number guesser

    Nice little demo. Any reason you didn't do this as a tutorial? +rep anyway
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Number guesser

    Good Tutorial +rep
    Last edited by debtboy; 10-18-2009 at 10:32 AM.

  7. #6
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Number guesser

    Very cool. You know what you are doing here is utilizing the idea of a binary search algorithm.

    I was reading in a data structures book the theory of this. I don't really remember the math of it but when the range is 1-100 the computer will always guess the number in no more than 7 guesses.

  8. #7
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Number guesser

    logarithms
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  9. #8
    Hajjel's Avatar
    Hajjel is offline Newbie
    Join Date
    Oct 2009
    Posts
    22
    Rep Power
    0

    Re: Number guesser

    Nice, tnx! I've been looking for a script like this for a long time now.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Number with zero
    By lol33d in forum PHP Development
    Replies: 4
    Last Post: 04-21-2011, 01:42 AM
  2. Very big number?
    By Hamed in forum C and C++
    Replies: 35
    Last Post: 12-16-2010, 10:00 AM
  3. number counter...
    By speachy_15 in forum Java Help
    Replies: 13
    Last Post: 12-01-2010, 11:16 AM
  4. PHP: only number
    By usmanzm in forum Classes and Code Snippets
    Replies: 2
    Last Post: 08-22-2008, 09:10 AM
  5. NaN - Not a Number
    By funkey_monkey in forum General Programming
    Replies: 0
    Last Post: 03-31-2008, 07:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts