# Guessing Game

• 12-05-2012, 03:15 PM
Freedox
Guessing Game
Hi, I'm working on Guessing game for my college assignment but I'm stuck on the last question and I have already spent a lot of hours trying to figure it out so I'm hoping that someone will help me.

The last part of my assignment is to store cookies for the game so that if you reload the page it will remember random number and attempts so you will continue to play. Does anyone have any idea?
Code:

```<!DOCTYPE html> <html> <head>         <meta content="en-ie" http-equiv="Content-Language">         <meta content="text/html; charset=utf-8" http-equiv="Content-Type">                 <link id="stylesheet" href="css/style.css" rel="stylesheet" type="text/css">         <script src="./JavaScript/Functions.js" type="text/javascript"></script>         <script src="./JavaScript/Persistence.js" type="text/javascript"></script>                 <script type="text/javascript">                                 // The number which the user will try to guess                 var Number = getRandomInt(1, 100);                                 // Max allowed guesses (Not used.. yet)                 var Guesses = 5;                 var Attempts = 0;                                 function GuessNumber()                 {                         if (Guesses == Attempts)                         {                                 window.alert("Game Over, the number was: " + Number + "") ;                                 return false;                                                }                         // 1. Read the value from the form                         var guess = document.guessForm.guess.value;                                                 //Add one to the times attempted to guess                         Attempts++;                                                 // 2. Check if number was guessed corrrectly                         if (guess == Number)                         {                         // If it was then inform the user using an alert and return true                                 window.alert("Congratulations, the number was: " + Number + "") ;                                 return true;                         }                         // If not then let user know if they were too low or too high and return false                         if (guess < Number)                         {                                 document.getElementById("wrongGuess").innerHTML = "Guess Too Low!";                                 return false;                                                }                         if (guess > Number)                         {                                 document.getElementById("wrongGuess").innerHTML = "Guess Too High!";                                 return false;                                                }                                // Note For incorrect guesses, feedback should be displayed                         // in the '<span id="wrongGuess"></span>' element (see below)                                         }                 function setCookie(ranNumber,value,exdays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=ranNumber + "=" + c_value; }         </script>                 </head>         <body onload="checkCookie()">                 <div id="content">                                         <h1>Guess The Number Game</h1>                                                         <!--        The Guess Number form                                                 Note the onsubmit event handler - you are required to write the JavaScript                                                 for the GuessNumber function                                                 The value returned by GuessNumber will determine if the form is reset (and the page reloaded) or not                                                 true - reset form                                                 false - form will not be reset                                                        Note that JavaScript values will be retained between guesses unless                                                 the form is reset or the page is reloaded.                                 -->                                 <form name="guessForm" method="" action="" onsubmit="return GuessNumber();">                                         <fieldset>                                                 <legend>Guess the number (between 1 and 100)</legend>                                                         <p></p>                                                         <p>                                                                 <label for="firstName">My Guess:</label>                                                                 <input type="text" name="guess"/>                                                                 <!-- Display Wrong guess feedback in the folowing element (hint: innerHTML)-->                                                                 <span id="wrongGuess"></span>                                                         </p>                                                         <p>                                                                 <input type="submit" name="SubmitForm" value="Guess" class="submit" />                                                         </p>                                                 </fieldset>                                 </form>                 </div>         </body> </html>```
• 12-05-2012, 04:29 PM
007Julien
1/- Use proven functions for cookies, (see, for example,these),
2/- store all the necessary variables in a cookie (with separator and string to split),
3/- Use an Init function or arguments for the function GessNumber to call It with the stored values if any.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.07902 seconds
• Memory Usage 2,381KB
• Queries Executed 11 (?)
Template Usage (20):
• (1)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates