Hangman Game Troubles

Page 1 of 1

0 Replies - 1666 Views - Last Post: 19 April 2008 - 01:01 PM

#1 Stephanie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-April 08

Hangman Game Troubles

Posted 19 April 2008 - 01:01 PM

I'm pretty new to Javascript so I don't really know exactly what I'm doing.

I've been working on this Hangman game for a while now. My code is a bit of a mess. I can't seem to get a handle on the whole 'changing image' and 'check the letters' and 'reveal letters' bits.

I've looked around but I can't really seem to be able to get where I want to be.

Help would be much appreciated.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript" type="text/javascript">

var words;
var randy;
var ranword;
var mask;
var wordslong;
var pics;
var alpha;
var alreadlet;
var letter;
var misses;
var ipic;

alpha = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
alreadlet = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

pics = new Array("hang1.jpg","hang2.jpg","hang3.jpg","hang4.jpg","hang5.jpg","hang6.jpg","hang7.jpg","hangwin.jpg");

words = new Array("bollywood", "chalo", "dekho", "pagal", "rangeela");

	ipic=0;
	document.images["hangpic"].src = pics[ipic];

function pickword(word)
{
	misses = 0;
	
	// Pick random word from 'words' Array.
		randy = Math.floor(Math.random()*words.length);
		ranword = words[randy];

	// Create mask for 'ranword'.
		mask = "";
		wordslong = ranword.length;

	for (i = 0; i < wordslong; i ++) {
		mask += "*";
		}

	// Display 'ranword' in text box 'word'.
		document.hangman.word.value=mask;
}

function replaceChars(ranword, letter, guessletter) {

	var guessWord_array = guessletter.split("");

	var index = ranword.indexOf(guessletter, index+1);
		if(index == -1) {
			return false;
		}

	while(index != -1) {
		index = ranword.indexOf(guessletter, index+1); 
		guessWord_array[index] = guessletter;

	}

letter = "";

	for(var i = 0; i < guessWord_array.length; i++) {
		letter += guessWord_array[i];
	}
return letter;
}

function guesslet(){

	var guessletter=document.hangman.letter.value;

	var resultFound = replaceChars(ranword, letter, guessletter);
	
	if(!resultFound) {
			
		}
	if (resultFound) {

		}

}



</script>

</head>

<body>

<form name="hangman">

<img name="hangpic" src=" " />

<p>
<input type="text" name="word" /><br />

<input type="text" name="letter" /></p>

<p><input type="button" name="guessbut" onclick="guesslet();" value="Guess A Letter" /> <br />
<input type="button" name="playgame" onclick="pickword();" value="New Word"/></p>

</form>

</body>
</html>




Is This A Good Question/Topic? 0
  • +

Page 1 of 1