Hangman Game Using Javascript

Language

In this tutorial, we are going to make a simple hangman game using javascript and html. Just follow the instructions below and download the source code. You can you this in your projects or systems.

Instructions

Writing our html code

  1. <FORM NAME="f">
  2.  
  3. <TABLE BGCOLOR=#428bca BORDER=1>
  4. <TR>
  5. <TD COLSPAN=4 ALIGN=RIGHT>
  6. Score : <INPUT TYPE=TEXT NAME="score" VALUE="0" onfocus="score.blur();" SIZE=2>
  7. <BR>
  8. Fails (6): <INPUT TYPE=TEXT NAME="lives" VALUE="0" onfocus="lives.blur();" SIZE=2>
  9. </TD>
  10. <TD COLSPAN=7 ALIGN=CENTER>
  11. <INPUT TYPE=TEXT NAME="word" VALUE=" --- Hangman ---" onfocus="word.blur();" SIZE=25>
  12. <BR>
  13. <INPUT TYPE=TEXT NAME="tried" VALUE="Click GO to get a word." onfocus="tried.blur();" SIZE=25>
  14. </TD>
  15. <TD COLSPAN=2 ALIGN=CENTER>
  16. <INPUT TYPE=BUTTON onclick="new_word(this.form);" VALUE=" GO ">
  17. </TD>
  18. </TR>
  19. <TR>
  20. <TD><INPUT TYPE=BUTTON VALUE=" A " onclick="seek('A');"></TD>
  21. <TD><INPUT TYPE=BUTTON VALUE=" B " onclick="seek('B');"></TD>
  22. <TD><INPUT TYPE=BUTTON VALUE=" C " onclick="seek('C');"></TD>
  23. <TD><INPUT TYPE=BUTTON VALUE=" D " onclick="seek('D');"></TD>
  24. <TD><INPUT TYPE=BUTTON VALUE=" E " onclick="seek('E');"></TD>
  25. <TD><INPUT TYPE=BUTTON VALUE=" F " onclick="seek('F');"></TD>
  26. <TD><INPUT TYPE=BUTTON VALUE=" G " onclick="seek('G');"></TD>
  27. <TD><INPUT TYPE=BUTTON VALUE=" H " onclick="seek('H');"></TD>
  28. <TD><INPUT TYPE=BUTTON VALUE=" I " onclick="seek('I');"></TD>
  29. <TD><INPUT TYPE=BUTTON VALUE=" J " onclick="seek('J');"></TD>
  30. <TD><INPUT TYPE=BUTTON VALUE=" K " onclick="seek('K');"></TD>
  31. <TD><INPUT TYPE=BUTTON VALUE=" L " onclick="seek('L');"></TD>
  32. <TD><INPUT TYPE=BUTTON VALUE=" M " onclick="seek('M');"></TD>
  33. </TR>
  34. <TR>
  35. <TD><INPUT TYPE=BUTTON VALUE=" N " onclick="seek('N');"></TD>
  36. <TD><INPUT TYPE=BUTTON VALUE=" O " onclick="seek('O');"></TD>
  37. <TD><INPUT TYPE=BUTTON VALUE=" P " onclick="seek('P');"></TD>
  38. <TD><INPUT TYPE=BUTTON VALUE=" Q " onclick="seek('Q');"></TD>
  39. <TD><INPUT TYPE=BUTTON VALUE=" R " onclick="seek('R');"></TD>
  40. <TD><INPUT TYPE=BUTTON VALUE=" S " onclick="seek('S');"></TD>
  41. <TD><INPUT TYPE=BUTTON VALUE=" T " onclick="seek('T');"></TD>
  42. <TD><INPUT TYPE=BUTTON VALUE=" U " onclick="seek('U');"></TD>
  43. <TD><INPUT TYPE=BUTTON VALUE=" V " onclick="seek('V');"></TD>
  44. <TD><INPUT TYPE=BUTTON VALUE=" W " onclick="seek('W');"></TD>
  45. <TD><INPUT TYPE=BUTTON VALUE=" X " onclick="seek('X');"></TD>
  46. <TD><INPUT TYPE=BUTTON VALUE=" Y " onclick="seek('Y');"></TD>
  47. <TD><INPUT TYPE=BUTTON VALUE=" Z " onclick="seek('Z');"></TD>
  48. </TR>
  49.  
  50. </FORM>

Writing our Javascript Code

  1. <SCRIPT LANGUAGE="javascript">
  2.  
  3. /*
  4. Script by Mike Mcgrath- http://website.lineone.net/~mike_mcgrath
  5. Featured on JavaScript Kit (http://javascriptkit.com)
  6. For this and over 400+ free scripts, visit http://javascriptkit.com
  7. */
  8.  
  9. var alpha=new Array();
  10. var alpha_index=0;
  11.  
  12. var bravo=new Array();
  13. var bravo_index=0;
  14.  
  15. var running=0;
  16. var failnum=0;
  17. var advising=0;
  18.  
  19. function pick()
  20. {
  21. var choice="";
  22. var blank=0;
  23.  
  24. for (i=0; i<words[index].length; i++)
  25. {
  26. t=0;
  27. for(j=0; j<=alpha_index; j++)
  28. if(words[index].charAt(i)==alpha[j] || words[index].charAt(i)==alpha[j].toLowerCase()) t=1;
  29.  
  30. if (t) choice+=words[index].charAt(i)+" ";
  31. else
  32. {
  33. choice+="_ ";
  34. blank=1;
  35. }
  36. }
  37.  
  38. document.f.word.value=choice;
  39.  
  40. if (!blank)
  41. {
  42. document.f.tried.value=" === You Win! ===";
  43. document.f.score.value++;
  44. running=0;
  45. }
  46. }
  47.  
  48.  
  49. function new_word(form)
  50. {
  51. if(!running)
  52. {
  53. running=1;
  54. failnum=0;
  55. form.lives.value=failnum;
  56. form.tried.value="";
  57. form.word.value="";
  58. index=Math.round(Math.random()*10000) % 100;
  59. alpha[0]=words[index].charAt(0);
  60. alpha[1]=words[index].charAt(words[index].length-1);
  61. alpha_index=1;
  62. bravo[0]=words[index].charAt(0);
  63. bravo[1]=words[index].charAt(words[index].length-1);
  64. bravo_index=1;
  65. pick();
  66. }
  67. else advise("A word is already in play!");
  68. }
  69.  
  70. function seek(letter)
  71. {
  72. if (!running) advise(".....Click GO to start !");
  73. else
  74. {
  75. t=0;
  76. for (i=0; i<=bravo_index; i++)
  77. {
  78. if (bravo[i]==letter || bravo[i]==letter.toLowerCase()) t=1;
  79. }
  80.  
  81. if (!t)
  82. {
  83. document.f.tried.value+=letter+" "
  84. bravo_index++;
  85. bravo[bravo_index]=letter;
  86.  
  87. for(i=0;i<words[index].length;i++)
  88. if(words[index].charAt(i)==letter || words[index].charAt(i)==letter.toLowerCase()) t=1;
  89.  
  90. if(t)
  91. {
  92. alpha_index++;
  93. alpha[alpha_index]=letter;
  94. }
  95. else failnum++;
  96.  
  97. document.f.lives.value=failnum;
  98. if (failnum==6)
  99. {
  100. document.f.tried.value="You lose - Try again!";
  101. document.f.word.value=words[index];
  102. document.f.score.value--;
  103. running=0;
  104. }
  105. else pick();
  106. }
  107. else advise("Letter "+letter+" is already used!");
  108. }
  109. }
  110.  
  111. function advise(msg)
  112. {
  113. if (!advising)
  114. {
  115. advising=-1;
  116. savetext=document.f.tried.value;
  117. document.f.tried.value=msg;
  118. window.setTimeout("document.f.tried.value=savetext; advising=0;",1000);
  119. }
  120. }
  121.  
  122. var words = new Array("","acrimonious","allegiance","ameliorate","annihilate","antiseptic","articulate","authoritative","benefactor","boisterous","breakthrough","carcinogenic","censorious","chivalrous","collarbone","commendable","compendium","comprehensive","conclusive","conscientious","considerate","deferential","denouement","determinate","diffidence","disruption","earthenware","elliptical","entanglement","escutcheon","extinguish","extradition","fastidious","flamboyant","forethought","forthright","gregarious","handmaiden","honeysuckle","hypocritical","illustrious","infallible","lumberjack","mischievous","mollycoddle","nimbleness","nonplussed","obliterate","obsequious","obstreperous","opalescent","ostensible","pandemonium","paraphernalia","pawnbroker","pedestrian","peremptory","perfunctory","pernicious","perpetrate","personable","pickpocket","poltergeist","precipitous","predicament","preposterous","presumptuous","prevaricate","propensity","provisional","pugnacious","ramshackle","rattlesnake","reciprocate","recrimination","redoubtable","relinquish","remonstrate","repository","reprehensible","resolution","resplendent","restitution","retaliation","retribution","saccharine","salubrious","skulduggery","skyscraper","soothsayer","tearjerker","transcribe","turpentine","unassuming","underscore","undertaker","underwrite","unobtrusive","vernacular","waterfront","watertight");
  123.  
  124. </SCRIPT>
You have successfully created a simple hangman game using javascript. For more information, suggestions and queries feel free to comment below.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment