www.webdeveloper.com
Results 1 to 2 of 2

Thread: Guessing Game

  1. #1
    Join Date
    Dec 2012
    Posts
    1

    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>

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,273
    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.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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



Recent Articles