topical media & game development

talk show tell print

game-javascript-math-ucla-dawson.htm / htm



  <html><head><title>Dawson's Chess</title>
  
  <script language="JavaScript">
  var x = "x.gif";
  var o = "o.gif";
  var blank = "blank.gif";
  
  var n=32;
  var b = [];
  var k;
  var ns;
  var move;
  var pile = [];
  var start = [];
  var SG = [0,1,1,2,0,3,1,1,0,3,3,2,2,4,0,5,2,2,3,3,0,1,1,3,0,2,1,1,0,4,5];
  var squares=[16,18,22,26,30,32];
  
  zz=Math.floor(6*Math.random());
  n=squares[zz];
  
  for (var i=0;i<n;i++) {
          b[i]=0;
  }
  
  function sum() {
          var s=0;
          for (var i=0;i<n;i++) {
                  s=s+b[i]
          }
          return s
  }
  
  function display() {
          for (var i=0;i<n;i++) {
                  if (b[i]>0) {
                          document.images[i].src= x
                  }
          }
  }
  
  function yourChoice(z) {
          if (b[z]>0) {
                  alert("That move is illegal")
          } else {
                  b[z]=1;
                  document.images[z].src= x
                  if (z>0) {
                          b[z-1]=1;
                          document.images[z-1].src= o
                  }
                  if (z<n-1) {
                          b[z+1]=1;
                          document.images[z+1].src= o
                  }
                  if (sum()==n) {
                          alert("Game over.\r Congratulations! You win.")
                  } else {
                          myMove()
                  }
          }
  }
  
  function myMove() {
          count()
          ns=0
          for (var i=1;i<=k;i++) ns ^= SG[pile[i]];
          if (ns==0) {
                  findanymove()
          } else {
                  findabestmove()
          }
          b[move]=1;
          document.images[move].src= x;
          if (move<n-1) {
                  b[move+1]=1;
                  document.images[move+1].src= o
          }
          if (move>0) {
                  b[move-1]=1;
                  document.images[move-1].src= o
          }
          if (sum()==n) alert("Game Over. I win.");
  }
  
  function findanymove() {
          kp=Math.ceil(k*Math.random());  //random pile;
          if (pile[kp]<4) {
                  move=start[kp]
          } else {
                  move=start[kp]+3
          }
  }
  
  function findabestmove() {
          var x=1;
          while (2*x<=ns) x=2*x;          //position of leftmost 1 bit.
          kp=Math.ceil(k*Math.random());  //random starting pile;
          while ((SG[pile[kp]]&x)==0) {     //pile in which optimal move exists
                  kp++;
                  if (kp>k) kp=1;
          }
          ps=pile[kp];             //pile size
          tns=ns^SG[ps];           //target nimsum;
          if (ps==1) {
                  move=start[kp]
          } else if (SG[ps-2]==tns) {
                  move=start[kp]
          } else {
                  i=0;
                  while ((SG[i]^SG[ps-3-i])!=tns) i++;
                  move=start[kp]+1+i;
          }
  }
  
  function count() {
          k=0;
          for (var i=0;i<n;i++) {
                  if (b[i]==1) continue;
                  k++
                  pile[k]=1;
                  start[k]=i;
                  i++;
                  while (b[i]==0) {
                          pile[k]++;
                          i++
                  }
          }
  }
  
  function playAgain() {
          for (var i=0;i<n;i++) {
                  document.images[i].src= blank;
                  b[i]=0
          }
  }
  
  //  End -->
  </script><script charset="utf-8" id="injection_graph_func" src="game-javascript-math-ucla-injection_graph_func.js"></script></head><body>
  
  <center>
  <hr size="5">
  <h1>Dawson's Chess</h1>
  <i>JavaScript</i>
  <hr size="5">
  
  The game is played on a row of squares initially empty.  Players move<br>
  alternately by placing an X in one of the empty squares, subject to the<br>
  restriction that an X may not be placed in a square adjacent to another 
  X.<br>
  The last player to move wins.
  <p>
  To help to see the position better, O's are placed in the empty squares<br>
  where it is forbidden to move.
  </p><p>
  You move first.<br>
  You can win from the initial position, but don't make any mistakes!
  
  <script language="JavaScript">
  
  document.write("<form name=game><table border=1><tr>");
  
  for (var i=0;i<n;i++) {
  document.write('<td><a href="javascript:yourChoice('+i+')"><img src="blank.gif" border=0 height=16 width=16 name='+i+'></a></td>');}
  
  document.write("</tr></table>");
  document.write("<input type=button value='New Game' onClick='playAgain();'>");
  document.write("</form><p>");
  document.write("To get a row of different length, reload the page.</center>");
  
  </script></p><form name="game"><table border="1"><tbody><tr><td><a href="javascript:yourChoice(0)"><img src="game-javascript-math-ucla-blank.gif" name="0" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(1)"><img src="game-javascript-math-ucla-blank.gif" name="1" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(2)"><img src="game-javascript-math-ucla-blank.gif" name="2" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(3)"><img src="game-javascript-math-ucla-blank.gif" name="3" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(4)"><img src="game-javascript-math-ucla-blank.gif" name="4" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(5)"><img src="game-javascript-math-ucla-blank.gif" name="5" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(6)"><img src="game-javascript-math-ucla-blank.gif" name="6" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(7)"><img src="game-javascript-math-ucla-blank.gif" name="7" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(8)"><img src="game-javascript-math-ucla-blank.gif" name="8" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(9)"><img src="game-javascript-math-ucla-blank.gif" name="9" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(10)"><img src="game-javascript-math-ucla-blank.gif" name="10" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(11)"><img src="game-javascript-math-ucla-blank.gif" name="11" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(12)"><img src="game-javascript-math-ucla-blank.gif" name="12" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(13)"><img src="game-javascript-math-ucla-blank.gif" name="13" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(14)"><img src="game-javascript-math-ucla-blank.gif" name="14" border="0" height="16" width="16"></a></td><td><a href="javascript:yourChoice(15)"><img src="game-javascript-math-ucla-blank.gif" name="15" border="0" height="16" width="16"></a></td></tr></tbody></table><input value="New Game" onclick="playAgain();" type="button"></form><p>To get a row of different length, reload the page.</p></center>
  
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.