/    Sign up×
Community /Pin to ProfileBookmark

Number Guessing Game

Ok, I’m working on a number guessing game that gives you three tries to guess a number that is randomly selected from 1-10. If you get the answer right within three tries, you get a point. If you don’t, the computer gets a point. I need there to also be a save and load button that sets up and reads a cookie respectively that saves the current guess, the score, the random number, and the message. Here’s what I have so far. I haven’t established the cookie, or the save and load buttons. I’m just not sure how to go about setting it up. Cookies confuse me…?

[CODE]
<?xml version=”1.0″?>
<!DOCTYPE html
PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type”
content=”text/html; charset=ISO-8859-1″/>
<meta name=”Author” content=”Andy Lowe”/>
<meta http-equiv=”Content-Script-Type”
content=”application/ecmascript”/>
<script type=”application/ecmascript”>
/* <![CDATA[ */
function Onload() {
var guesses;
var random = Math.floor(Math.random() * 10) + 1;
var userpoints = parseFloat(0);
var comppoints = parseFloat(0);
form1 = document.forms[‘GameForm’];
form1.CompScore.value=comppoints;
form1.UserScore.value=userpoints;
}
/* ]]> */
</script>

<title>Number Game</title>
</head>
<body onload=Onload()>
<form name=”GameForm” action=”#”>
<table width=”650″ cellspacing=”1″ cellpadding=”2″ border=”0″ align=”center”>

<tr>
<th style=”text-align:center”>
<p>Secret Number Guessing Game</p>
</th>
</tr>

<td>
<p>Welcome to the Secret Number Guessing Game! Here are the rules:</p>
<p>
A number will be randomly selected from 1-10. You will have three tries to guess the number. If you guess correctly by the third try, you get a point! However, if you do not guess in three tries, the computer gets a point. You will be notified along the way whether you guess is too high or too low.</p>
</td>
</table>

<br/>

<table align=”center” cellpadding=”10″ border=”5″ cellspacing=”1″ width=”400″>
<tr>
<td style=”text-align:right”>
User Guess:
</td>
<td>
<input type=”text” name=”UserGuess” value=”” size=”30″ />
</td>
</tr>

<tr>
<td style=”text-align:right”>
Message:
</td>
<td>
<input type=”text” name=”Message” value=”Type your guess in the box above” size=”30″ />
</td>
</tr>
</table>

<br/>

<table align=”center”>
<tr>
<td>
<input type=”button” name=”Submit” value=”submit” onclick=Onclick() />
</td>
<tr>
</table>

<br/>

<table cols=”2″ align=”center” cellpadding=”10″ border=”5″ cellspacing=”1″ width=”400″>

<tr>
<th>
Computer Score
</th>
<th>
User Score
</th>
</tr>

<tr>
<td>
<input type=”text” name=”CompScore” value=”” />
</td>
<td>
<input type=”text” name=”UserScore” value=”” />
</td>
</tr>

</table>

</form>
</body>
</html>
[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@nap0leonDec 11.2007 — This article does a good job of explaining how cookies work and has sample functions to create, write, and read cookie values.

http://www.quirksmode.org/js/cookies.html
Copy linkTweet thisAlerts:
@JMRKERDec 11.2007 — Personally I would not worry about cookie setup, loads, saves, etc.

until you got the program working to your satisfaction.

Bad idea to define an onclick function as onclick

Check the error console ?
Copy linkTweet thisAlerts:
@Ct6mblueauthorDec 11.2007 — Personally I would not worry about cookie setup, loads, saves, etc.

until you got the program working to your satisfaction.

Bad idea to define an onclick function as onclick

Check the error console ?[/QUOTE]


Error console is clean. I've done that before and not had a problem...
Copy linkTweet thisAlerts:
@mrhooDec 11.2007 — You have at least 27 errors in the markup. Fix them, or change the doctype.
×

Success!

Help @Ct6mblue spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...