home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Archived / Internet / jvscript116beta / JvScript / !JvScript / Resources / Scripts / Games / 6 < prev    next >
Encoding:
Text File  |  1999-09-15  |  4.3 KB  |  156 lines

  1. #newformat
  2. _title:Tic-Tac-Toe
  3. _author:TJS
  4. _description:Noughts 'n' Crosses in JavaScript
  5.  
  6. _insert-in:inhead
  7. <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
  8. <!-- Begin
  9. step = 0;
  10. diff=3;
  11. function clear_all(form) {
  12. step = 0;
  13. for (i=0;i<9; ++i) {
  14. position="a"+i;
  15. form[position].value="";
  16.    }
  17. }
  18. function clickit(field) {
  19. if (step == -1) {alert("Reset to play again"); return;}
  20. position=field.name.substring(1,2,1);
  21. position = 'a'+position;
  22. if (field.form[position].value !="") {alert("Can't go there"); return;}
  23. field.form[position].value="X";
  24. if (eval_pos(field.form)) {
  25. field.form.output.value="You Win!"; 
  26. step = -1;
  27. return;
  28. }
  29. position=get_move(field.form);
  30. field.form.output.value='I moved to ' + position.substring(1,2,1);
  31. if (position=="") {
  32. field.form.output.value="No Winner."; 
  33. step = -1;
  34. return;
  35. }
  36. field.form[position].value="O";
  37. if (eval_pos(field.form)) {
  38. field.form.output.value="You Lose!";
  39. step = -1;
  40.    }
  41. }
  42. function eval_pos(form) {
  43. if ((form.a0.value!="" && form.a0.value==form.a3.value && form.a0.value==form.a6.value)||
  44. (form.a0.value!="" && form.a0.value==form.a1.value && form.a0.value==form.a2.value) ||
  45. (form.a0.value!="" && form.a0.value==form.a4.value && form.a0.value==form.a8.value) ||
  46. (form.a1.value!="" && form.a1.value==form.a4.value && form.a1.value==form.a7.value) ||
  47. (form.a2.value!="" && form.a2.value==form.a5.value && form.a2.value==form.a8.value) ||
  48. (form.a2.value!="" && form.a2.value==form.a4.value && form.a2.value==form.a6.value) ||
  49. (form.a3.value!="" && form.a3.value==form.a4.value && form.a3.value==form.a5.value) ||
  50. (form.a6.value!="" && form.a6.value==form.a7.value && form.a6.value==form.a8.value))
  51. return true;
  52. else    
  53. return false;
  54. }
  55. function f(a) {
  56. if (a == "") return "."; else return a;
  57. }
  58. function comp_move(form,player,weight,depth) {
  59. var cost;
  60. var bestcost=-2;
  61. var position;
  62. var newplayer;
  63. if (player=="X") newplayer="O"; else newplayer="X";
  64. if (depth==diff) return 0;
  65. if (eval_pos(form)) return 1;
  66. for (var i=0; i<9; ++i) {
  67. position='a'+i;
  68. if (form[position].value != "")
  69. continue;
  70. form[position].value=player;
  71. cost = comp_move(form,newplayer, -weight, depth+1);
  72. if (cost > bestcost) {
  73. bestcost=cost;
  74. if (cost==1) i=9;
  75. }
  76. form[position].value="";
  77. }
  78. if (bestcost==-2) bestcost=0;
  79. return(-bestcost);
  80. }
  81. function get_move(form) {
  82. var cost;
  83. var bestcost=-2;
  84. bestmove="";
  85. if (step++ == 0)
  86. if (form.a4.value=="") 
  87. return "a4";
  88. else 
  89. if (form.a0.value=="") 
  90. return "a0";
  91. for (var i=0; i<9; ++i) {
  92. localposition='a'+i;
  93. if (form[localposition].value != "")
  94. continue;
  95. form[localposition].value="O";
  96. cost=comp_move(form,"X", -1, 0);
  97. if (cost > bestcost) {
  98. if (cost==1) i=9;
  99. bestmove=localposition;
  100. bestcost=cost;
  101. }
  102. form[localposition].value="";
  103. }
  104. return bestmove;
  105. }
  106. function complain(field) {
  107. field.form.output.focus();
  108. alert("Don't change the game fields directly!");
  109. }
  110. // End -->
  111. </SCRIPT>
  112. _end-insert:
  113.  
  114. _insert-in:inbody
  115. <CENTER>
  116. Click on the button next to each position to place an X. 
  117. <HR>
  118. <FORM>
  119. <INPUT SIZE=1 NAME="a0" OnFocus="complain(this)"> 
  120. <INPUT TYPE="button" NAME="b0" OnClick="clickit(this)">
  121. <INPUT SIZE=1 NAME="a1" OnFocus="complain(this)"> 
  122. <INPUT TYPE="button" NAME="b1" OnClick="clickit(this)">
  123. <INPUT SIZE=1 NAME="a2" OnFocus="complain(this)"> 
  124. <INPUT TYPE="button" NAME="b2" OnClick="clickit(this)">
  125. <BR>
  126. <INPUT SIZE=1 NAME="a3" OnFocus="complain(this)"> 
  127. <INPUT TYPE="button" NAME="b3" OnClick="clickit(this)">
  128. <INPUT SIZE=1 NAME="a4" OnFocus="complain(this)">
  129. <INPUT TYPE="button" NAME="b4" OnClick="clickit(this)">
  130. <INPUT SIZE=1 NAME="a5" OnFocus="complain(this)"> 
  131. <INPUT TYPE="button" NAME="b5" OnClick="clickit(this)">
  132. <BR>
  133. <INPUT SIZE=1 NAME="a6" OnFocus="complain(this)"> 
  134. <INPUT TYPE="button" NAME="b6" OnClick="clickit(this)">
  135. <INPUT SIZE=1 NAME="a7" OnFocus="complain(this)"> 
  136. <INPUT TYPE="button" NAME="b7" OnClick="clickit(this)">
  137. <INPUT SIZE=1 NAME="a8" OnFocus="complain(this)"> 
  138. <INPUT TYPE="button" NAME="b8" OnClick="clickit(this)">
  139. <BR>
  140. <BR>
  141. Message: <INPUT NAME="output" TYPE="text"><BR>
  142. Difficulty: <SELECT NAME="difficulty" 
  143. OnChange="diff=form.difficulty[form.difficulty.selectedIndex].value;">
  144. <OPTION VALUE=1> Very Easy
  145. <OPTION VALUE=2> Easy
  146. <OPTION VALUE=3 SELECTED> Medium
  147. <OPTION VALUE=4> Hard (may think a long time)
  148. </SELECT>
  149. <BR>
  150. <INPUT TYPE="button" VALUE="Computer Moves First" OnClick="
  151. if (!step++) this.form.a4.value='O';">
  152. <BR>
  153. <INPUT TYPE="reset" VALUE="Restart" OnClick="clear_all(this.form)">
  154. </FORM>
  155. </CENTER>
  156. _end-insert: