# Programming Take-Home Assignment The assignment was to create a guessing game: > The computer picks a number between 1 and 100 and asks the user to guess > until he/she guesses the right answer. The computer tells the user whether > the guess was too high or too low and reports the total number of guesses > when the user wins. ## Getting Started A couple of notes: 1. You do not have to use the object notation indicated in the solution, but I did so just to (hopefully) clarify matters. 2. You will find it helpful to alert (using the built-in `alert` function) your program's answer while you are still debugging it. If your program gets caught in an infinite loop, you might have to force-quit your browser (Ctrl+Alt+Delete on PC; Cmd+Option+Escape on Mac). The following block of code will alert the number 7: var my_alerter = function() { alert(7); }; my_alerter(); 3. If you want to include apostrophes in a single-quoted string, e.g., you want the string `I haven't a clue`, you must precede them with a backslash. The same thing applies for double-quoted strings. E.g., var first = "I said \"Hey now, you're a rock star!\""; var second = 'It wasn\'t all that great'; Capisce? 4. You can use `Math.random()` to generate a random number for you between 0 and 1 (inclusive). And you can use `Math.round()` to round a number to the nearest integer. Thus, you might create the following function that generates a random number between 1 and `n`: var random_number = function(n) { return Math.round(n*Math.random()); }; [res]:http://uwwebpub.com/programming/ That said, here is my version of an object that handles the solution. As mentioned in (1) above, you might just create a bunch of functions that keep track of variables, but the "object" model discussed in class tends to make more sense (to me, anyhow). If you are interested in a "traditional" solution (i.e., one that is not object-oriented), please let me know via email, and I'd be happy to post one here. | | var guesser = { | max: 100, | // this uses the `random_number` function from above! | answer: random_number(this.max), | guesses: [], | question: function(){ | return 'Guess #' + this.guess_count() + ': ' + | 'Please pick a number between 1 and ' + | this.max ; | }, | too_high: function(){ | return 'Your guess ' + this.last_guess() + | ' was too high'; | }, | too_low: function(){ | return 'Your guess ' + this.last_guess() + | ' was too low'; | }, | correct: function(){ | return 'Your guess ' + this.last_guess() + | ' was correct!' + | ' You got it in ' + | this.guess_count() + ' tries!'; | }, | won: false, | response: function() { | var last = this.last_guess(); | var ans = this.answer; | if ( last > ans ) | { | return this.too_high(); | } else if ( last < ans ) | { | return this.too_low(); | } else | { | return this.correct(); | } | }, | last_guess: function() { | return this.guesses[this.guesses.length-1]; | }, | guess: function() { | var response = cons.geti(this.question()); | this.guesses[this.guesses.length] = response; | if ( response == this.answer ) | { | this.won = true; | } | return response; | }, | guess_count: function() { | return this.guesses.length | }, | }; | while ( ! guesser.won ) | { | cons.println(guesser.guess()); | } | cons.println( guesser.response() ); | | ## What Next? For a little more fun, try making the reverse game: have the user pick a secret number and make your program guess the answer, asking the user if its guess was too high or too low, until it gets the right answer. "Bonus points" if you figure out how to avoid simply asking for 1, then 2, then 3, etc. You might try always going mid-way between your last guess and the closest prior guess either above or below your last guess depending on the user's response. The key is that we always go midway between the maximum possible value and the minimum possible value. This is known as **binary search** (since we always go halfway between two possible values). It's more difficult than it initially appears to implement. To explain, **suppose the user's number is 17**. Here's how this plays out: Computer: (100 + 1)/2 = 50.5 = 50 (100 is the max, 1 is the minimum value allowed in our version of the game.) (Note we had to round, so we better look to see what we've already guessed to see if we should round up or down lest we go into an infinite loop.) User: Too high Computer: (49 + 1)/2 = 25 (50 was too high, so 49 is the new max and 1 is still the minimum it could be) User: Too high Computer: (24 + 1)/2 = 12.5 = 13 (25 was too high, so 24 is the maximum possible value it could be while 1 is still the minimum) User: Too low ... Computer: 17 User: Correct If you would like a more detailed solution to this last part, please send me an email at . If there is demand, I'd be happy to write it up. - - - Author: Ryan Timmons Last Modified: 06 August 2008 15:23:13 PDT URL: Also see the HTML version by changing the file extension to `.html`.