PDA

View Full Version : Word Guessing Game...Won't Work...Help Please



ao5431
Nov 20th, 2009, 03:37 AM
Good day. I'm working on a word guessing game. Simple premise, enter a letter, if it's in the mystery word it fills the asterick if not you keep guessing until it is guessed. However, I can't get it to fill in the astericks or to acknowledge when you've guessed it...soem help would be appreciated...thanks in advance.

ao5431

<!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>
<title>Guessing Game</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */
var mysteryWord = "suspicious";
function checkGuess() {
if (document.forms[0].letter.value != "") {
var progress = document.forms[0].mysteryWord.value;
var guess = document.forms[0].letter.value;
document.forms[0].letter.value = guess;
mysteryWordArray = mysteryWord.split(" ");
var progressArray = progress.split("");
for (var i=0; i<mysteryWord.length; ++i) {
var curLetter = mysteryWordArray[i];
if (curLetter == guess)
progressArray[i] = guess;
}
progress = progressArray.join("");
if (progress == mysteryWord)
document.forms[0].mysteryWord.value = "You guessed correctly! The mystery word is '" + progress + "'.";
else
document.forms[0].mysteryWord.value = progress;
}
else
window.alert("You must enter a letter!");
}
/* ]]> */
</script>
</head>
<body>
<h1>Guessing Game</h1><hr />
<form action="" method="get" enctype="application/x-www-form-urlencoded">
<p>Mystery word: <input type="text" name="mysteryWord" value="**********" size="100" readonly="readonly" style="border: 0" /></p>
<p>Enter a letter and click the Submit Letter button.</p>
<p><input type="text" name="letter" size="2" />
<input type="button" value="Submit Letter" onclick="checkGuess()" /></p>
</form><hr />
</body>
</html>

Old Pedant
Nov 20th, 2009, 03:46 AM
First thing I see is this:

mysteryWordArray = mysteryWord.split(" ");

You don't *have* any spaces in the mysteryWord, so that won't do anything.

I think you meant to do this:


mysteryWordArray = mysteryWord.split(""); // split into characters


Probably other errors, but have you used FireFox with Firebug installed?? That will give you a debugger and make finding errors MUCH MUCH quicker and easier.

ao5431
Nov 20th, 2009, 03:55 AM
I use the debigger tool also..I am just not getting any errors or warnings to lead me in the right direction...perhaps fixing the space in the quotes will have something sshow up...thanks.

jmrker
Nov 20th, 2009, 04:44 AM
You were very close.

I added a check to assure user did not enter CAPITAL letters as a guess
and some changes to cut out a number of mouse clicks.


<!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>
<title>Guessing Game</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */
var mysteryWord = "suspicious";
function checkGuess() {
if (document.forms[0].letter.value != "") { // check for input by user
var progress = document.forms[0].mysteryWord.value;
var guess = document.forms[0].letter.value.toLowerCase(); // comparison in case upper case entered
document.forms[0].letter.value = ''; // clear guess;
document.forms[0].letter.focus();
mysteryWordArray = mysteryWord.split("");
var progressArray = progress.split("");
for (var i=0; i<mysteryWord.length; ++i) {
var curLetter = mysteryWordArray[i];
if (curLetter == guess) progressArray[i] = guess;
}
progress = progressArray.join("");
if (progress == mysteryWord)
document.forms[0].mysteryWord.value = "You guessed correctly! The mystery word is '" + progress + "'.";
else
document.forms[0].mysteryWord.value = progress;
}
else
window.alert("You must enter a letter!");
}
/* ]]> */
</script>
</head>
<body>
<h1>Guessing Game</h1><hr />
<form action="" method="get" enctype="application/x-www-form-urlencoded">
<p> Mystery word:
<input type="text" name="mysteryWord" value="**********" size="100" readonly="readonly" style="border: 0" />
</p>
<p>Enter a letter and click the Submit Letter button.</p>
<p><input type="text" name="letter" size="1" />
<input type="button" value="Submit Letter" onclick="checkGuess()" /></p>
</form>
<hr />
</body>
</html>

Next improvement would be to allow multiple mystery words.

BTW: I makes it easier to read the code if you enclose it in
[ code] [ /code] tags (without the spaces).

jmrker
Nov 20th, 2009, 05:16 AM
It's fixed with changes in post #2 and full script of post #4. :thumbsup:

Are there two of you or just one with different forum names? :confused:

jmrker
Nov 20th, 2009, 05:25 AM
Thanks for help me

Who did we help ... 'jacksondfaniel', 'nopluert' or 'ao5431'?
Is this a study group?
:confused:

Philip M
Nov 20th, 2009, 08:36 AM
Who did we help ... 'jacksondfaniel', 'nopluert' or 'ao5431'?
Is this a study group?
:confused:

nopluert is the same French spammer as usual. He has been banned many times but keeps coming back. He seems pretty stupid as (a) his posts are always struck out by the mods within hours, and (b) few people visiting this forum are French-speaking.

jacksondfaniel looks like another spammer trying to get you to visit his link.