Доброго времени суток.
Помогите разобрать код. Только начал изучать JavaScript. Вот нашел пример игры, хочу ее переделать, по времени, с сохранением лучшего результата. Очень прошу, прокомментируйте код, а то я не пойму, как тут все взаимодействует.
<html>
<head>
<SCRIPT language=JavaScript type=text/javascript>
<!--
gallows = new Array("--------\n| |\n|\n|\n|\n|\n=====",
"--------\n| O\n|\n|\n|\n|\n=====",
"--------\n| O\n| |\n|\n|\n|\n=====",
"--------\n| O\n| \\|\n|\n|\n|\n=====",
"--------\n| O\n| \\|/\n|\n|\n|\n=====",
"--------\n| O\n| \\|/\n| |\n|\n|\n=====",
"--------\n| O\n| \\|/\n| |\n| /\n|\n=====",
"--------\n| O\n| \\|/\n| |\n| / \\\n|\n=====")
guessChoices = new
Array("JavaScript","Navigator","LiveConnect","LiveWire","Windows","Explorer","Microsoft","Idiot","Platform","Server","Browser","Function","Object","Array","Onmouse")
function startAgain() {
guesses = 0
max = gallows.length-1
guessed = " "
len = guessChoices.length - 1
toGuess =
guessChoices[Math.round(len*Math.random())].toUpperCase()
displayHangman()
displayToGuess()
displayGuessed()
}
function stayAway() {
document.game.elements[3].focus()
alert("Don't mess with this field element!")
}
function displayHangman() {
document.game.status.value=gallows[guesses]
}
function displayToGuess() {
pattern=""
for(i=0;i<toGuess.length;++i) {
if(guessed.indexOf(toGuess.charAt(i)) != -1) pattern +=
(toGuess.charAt(i)+" ")
else pattern += "_ "
}
document.game.toGuess.value=pattern
}
function displayGuessed() {
document.game.guessed.value=guessed
}
function badGuess(s) {
if(toGuess.indexOf(s) == -1) return true
return false
}
function winner() {
for(i=0;i<toGuess.length;++i) {
if(guessed.indexOf(toGuess.charAt(i)) == -1) return false
}
return true
}
function guess(s){
if(guessed.indexOf(s) == -1) guessed = s + guessed
if(badGuess(s)) ++guesses
displayHangman()
displayToGuess()
displayGuessed()
if(guesses >= max){
alert("You're dead. The word you missed was "+ toGuess +".")
startAgain()
}
if(winner()) {
alert("You won!, Clever!!!")
startAgain()
}
}
// -->
</SCRIPT>
</head>
<body>
<CENTER>
<H1>The Game Of Hangman</H1>
<P><B>Can You Get Any Of</B></P>
<P><B>The Fifteen Words</B></P></CENTER>
<HR>
<CENTER>
<FORM name=game><PRE><TEXTAREA cols=18 name=status onfocus=stayAway() rows=7></TEXTAREA>
</PRE>
<P><INPUT name=toGuess onfocus=stayAWAY()><B>Word to guess</B><BR></P>
<P> <INPUT
name=guessed onfocus=stayAway()><B> Letters guessed so far</B><BR></P>
<P><B>Enter your next guess.</B></P><INPUT onclick="guess('A')" type=button value=" A "><INPUT onclick="guess('B')" type=button value=" B "><INPUT onclick="guess('C')" type=button value=" C "><INPUT onclick="guess('D')" type=button value=" D "><INPUT onclick="guess('E')" type=button value=" E "><INPUT onclick="guess('F')" type=button value=" F "><INPUT onclick="guess('G')" type=button value=" G "><INPUT onclick="guess('H')" type=button value=" H "><INPUT onclick="guess('I')" type=button value=" I "><INPUT onclick="guess('J')" type=button value=" J "><INPUT onclick="guess('K')" type=button value=" K "><INPUT onclick="guess('L')" type=button value=" L "><INPUT onclick="guess('M')" type=button value=" M "><INPUT onclick="guess('N')" type=button value=" N "><INPUT onclick="guess('O')" type=button value=" O "><INPUT onclick="guess('P')" type=button value=" P "><INPUT onclick="guess('Q')" type=button value=" Q "><INPUT onclick="guess('R')" type=button value=" R "><INPUT onclick="guess('S')" type=button value=" S "><INPUT onclick="guess('T')" type=button value=" T "><INPUT onclick="guess('U')" type=button value=" U "><INPUT onclick="guess('V')" type=button value=" V "><INPUT onclick="guess('W')" type=button value=" W "><INPUT onclick="guess('X')" type=button value=" X "><INPUT onclick="guess('Y')" type=button value=" Y "><INPUT onclick="guess('Z')" type=button value=" Z ">
<P><INPUT name=restart onclick=startAgain() type=button value="-- Start Again --"></P>
<SCRIPT type=text/javascript>
<!--
startAgain()
// -->
</SCRIPT>
</FORM>
<HR>
</body>