home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Archived / Internet / jvscript116beta / JvScript / !JvScript / Resources / Scripts / Games / 2 < prev    next >
Encoding:
Text File  |  1999-08-24  |  5.7 KB  |  229 lines

  1. #newformat
  2. _title:Hangman
  3. _author:Rick Glusick
  4. _source:http://javascript.internet.com
  5. _description:Play Hangman, with Presidents of the USA as the subject
  6.  
  7. _insert-in:inhead
  8. <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
  9. <!-- Begin
  10. function getCookie (name) {
  11. var dcookie = document.cookie;
  12. var cname = name + "=";
  13. var clen = dcookie.length;
  14. var cbegin = 0;
  15. while (cbegin < clen) {
  16. var vbegin = cbegin + cname.length;
  17. if (dcookie.substring(cbegin, vbegin) == cname) {
  18. var vend = dcookie.indexOf (";", vbegin);
  19. if (vend == -1) vend = clen;
  20. return unescape(dcookie.substring(vbegin, vend));
  21. }
  22. cbegin = dcookie.indexOf(" ", cbegin) + 1;
  23. if (cbegin == 0) break;
  24. }
  25. return null;
  26. }
  27. function setCookie (name, value, expires) {
  28. if (!expires) expires = new Date();
  29. document.cookie = name + "=" + escape (value) + "; expires=" + expires.toGMTString() +  "; path=/";
  30. }
  31. function delCookie (name) {
  32. var expireNow = new Date();
  33. document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
  34. }
  35. var Alphabet = new initAlphaArray()
  36. var NumOfWords = 42;
  37. var SaveData = "";
  38. var ImageNum = "";
  39. var LettersSelected = "";
  40. var RandomWord = "";
  41. var DisplayWord = "";
  42. var position = 0;
  43. var word = new WordList();
  44. var expdate = new Date();
  45. var RandomNumber = (expdate.getSeconds())%NumOfWords;
  46. function initAlphaArray() {
  47. this.length = 26
  48. this[0] = "A"
  49. this[1] = "B"
  50. this[2] = "C"
  51. this[3] = "D"
  52. this[4] = "E"
  53. this[5] = "F"
  54. this[6] = "G"
  55. this[7] = "H"
  56. this[8] = "I"
  57. this[9] = "J"
  58. this[10] = "K"
  59. this[11] = "L"
  60. this[12] = "M"
  61. this[13] = "N"
  62. this[14] = "O"
  63. this[15] = "P"
  64. this[16] = "Q"
  65. this[17] = "R"
  66. this[18] = "S"
  67. this[19] = "T"
  68. this[20] = "U"
  69. this[21] = "V"
  70. this[22] = "W"
  71. this[23] = "X"
  72. this[24] = "Y"
  73. this[25] = "Z"
  74. }
  75. function WordList() {
  76. this.length = NumOfWords;
  77. this[0] = "GEORGE WASHINGTON";
  78. this[1] = "JOHN ADAMS";
  79. this[2] = "THOMAS JEFFERSON";
  80. this[3] = "JAMES MADISON";
  81. this[4] = "JAMES MONROE";
  82. this[5] = "JOHN QUINCY ADAMS";
  83. this[6] = "ANDREW JACKSON";
  84. this[7] = "MARTIN VAN BUREN";
  85. this[8] = "WILLIAM HENRY HARRISON";
  86. this[9] = "JOHN TYLER";
  87. this[10] = "JAMES POLK";
  88. this[11] = "ZACHARY TAYLOR";
  89. this[12] = "MILLARD FILLMORE";
  90. this[13] = "FRANKLIN PIERCE";
  91. this[14] = "JAMES BUCHANAN";
  92. this[15] = "ABRAHAM LINCOLN";
  93. this[16] = "ANDREW JOHNSON";
  94. this[17] = "ULYSSES GRANT";
  95. this[18] = "RUTHERFORD HAYES";
  96. this[19] = "JAMES GARFIELD";
  97. this[20] = "CHESTER ARTHUR";
  98. this[21] = "GROVER CLEVELAND";
  99. this[22] = "BENJAMIN HARRISON";
  100. this[23] = "GROVER CLEVELAND";
  101. this[24] = "WILLIAM MCKINLEY";
  102. this[25] = "THEODORE ROOSEVELT";
  103. this[26] = "WILLIAM HOWARD TAFT";
  104. this[27] = "WOODROW WILSON";
  105. this[28] = "WARREN HARDING";
  106. this[29] = "CALVIN COOLIDGE";
  107. this[30] = "HERBERT HOOVER";
  108. this[31] = "FRANKLIN ROOSEVELT";
  109. this[32] = "HARRY TRUMAN";
  110. this[33] = "DWIGHT EISENHOWER";
  111. this[34] = "JOHN KENNEDY";
  112. this[35] = "LYNDON JOHNSON";
  113. this[36] = "RICHARD NIXON";
  114. this[37] = "GERALD FORD";
  115. this[38] = "JIMMY CARTER";
  116. this[39] = "RONALD REAGAN";
  117. this[40] = "GEORGE BUSH";
  118. this[41] = "BILL CLINTON";
  119. }
  120. function availableLetters(i) {
  121. if (LettersSelected.charAt(i)==Alphabet[i])
  122. document.write('<TD ALIGN=CENTER VALIGN=CENTER WIDTH=20 HEIGHT=12>' +
  123. '<B><A HREF="javascript:LoadNextPage('+i+',\''+Alphabet[i]+
  124. '\')">'+Alphabet[i]+'</A></B></TD>');
  125. else
  126. document.write('<TD ALIGN=CENTER VALIGN=CENTER WIDTH=20 HEIGHT=12> </TD>');
  127. }
  128. function LoadNextPage(selected,letter) {
  129. var j=0;
  130. var HoldLettersSelected = LettersSelected;
  131. LettersSelected = "";
  132. if (selected == 0) {
  133. for (j=1; j<=25; j++) {
  134. LettersSelected += HoldLettersSelected.charAt(j);
  135. }
  136. LettersSelected = "^" + LettersSelected;
  137. }
  138. else if (selected == 25) {
  139. for (j=0; j<=24; j++) {
  140. LettersSelected += HoldLettersSelected.charAt(j);
  141. }
  142. LettersSelected += "^";
  143. }
  144. else {
  145. for (j=0; j<selected; j++)
  146. {
  147. LettersSelected += HoldLettersSelected.charAt(j);
  148. }
  149. LettersSelected += "^";
  150. for (j=selected+1; j<=25; j++) {
  151. LettersSelected += HoldLettersSelected.charAt(j);
  152.    }
  153. }
  154.  
  155.  
  156. SaveData = ImageNum + LettersSelected + RandomWord + "*";
  157. setCookie("_HangMan", SaveData, expdate);
  158. history.go(0);
  159. }
  160.  
  161. // Sets a cookie that will expire in 10 days
  162.  
  163. expdate.setTime (expdate.getTime() + (1000*60*60*24*10));
  164. if(getCookie("_HangMan") == null)
  165. {
  166. ImageNum = "A";
  167. LettersSelected = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  168. RandomWord = word[RandomNumber];
  169. SaveData = ImageNum + LettersSelected + RandomWord + "*";
  170. setCookie("_HangMan", SaveData, expdate);
  171. }
  172. else {
  173. SaveData = getCookie("_HangMan");
  174. ImageNum = SaveData.charAt(0);
  175. for (position=1; position<=26; position++) {
  176. LettersSelected += SaveData.charAt(position);
  177. }
  178. for (position=27; position<SaveData.indexOf("*"); position++) {
  179. RandomWord += SaveData.charAt(position);
  180.    }
  181. }
  182. DisplayWord = "";
  183. for (i=0; i<RandomWord.length; i++) {
  184. if (RandomWord.charAt(i) == ' ') {
  185. DisplayWord += " "; 
  186. }
  187. else {
  188. MatchFound = false;
  189. for (j=0; j<=25; j++) {
  190. if ((LettersSelected.charAt(j) == "^") && (RandomWord.charAt(i) == Alphabet[j])) {
  191. DisplayWord += RandomWord.charAt(i);
  192. MatchFound = true;
  193.    }
  194. }
  195. if (!MatchFound) DisplayWord += "-";
  196.    }
  197. }
  198. if (ImageNum == "J") {
  199. document.write('<font color=red size=4>You Lost!<br>Answer:  "' + RandomWord + '"</font>');
  200. }
  201. else if (RandomWord == DisplayWord) {
  202. document.write('<font color=red size=8>You Win!</font>');
  203. }
  204. else {
  205. document.write('<table>');
  206. document.write('<tr>');
  207. for (i=0; i<13; i++) availableLetters(i);
  208. document.write('</tr>');
  209. document.write('<tr>');
  210. for (i=13; i<26; i++) availableLetters(i);
  211. document.write('</tr>');
  212. document.write('</table>');
  213. }
  214. document.write('<br>');
  215. document.write('<br>');
  216. document.write('<font size=9><tt>');
  217. document.write(DisplayWord);
  218. document.write('</tt></font>');
  219. document.write('<form>');
  220. document.write('<input type="button" VALUE="New Game"'+
  221. 'onClick="delCookie(\'_HangMan\');history.go(0);">');
  222. document.write('</form>');
  223. document.write('</center>');
  224. // End -->
  225. </SCRIPT>
  226. </td></tr>
  227. </table>
  228. </center>
  229. _end-insert: