Internet Explorer 5.0, Part III: Favorites-Persistent Game Code - Doc JavaScript | WebReference

Internet Explorer 5.0, Part III: Favorites-Persistent Game Code - Doc JavaScript


Favorites-Persistent Game Code

<HTML>
<HEAD>
<META NAME="save" CONTENT="favorite">
<STYLE>
  .saveFavorite{behavior:url(#_IE_);}
</STYLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
var lastPlayedBy = "x";
var grid = new Array();
grid[1] = new Array();
grid[2] = new Array();
grid[3] = new Array();
for (var i = 1; i <= 3; i++)
  for (var j = 1; j <= 3; j++)
    grid[i][j] = "";
function rowComplete() {
  var lastPlayedBy = grid[1][1];
  if ((lastPlayedBy != "") &&
      (grid[2][1] == lastPlayedBy) &&
      (grid[3][1] == lastPlayedBy)) return(true);
  lastPlayedBy = grid[1][2];
  if ((lastPlayedBy != "") &&
      (grid[2][2] == lastPlayedBy) &&
      (grid[3][2] == lastPlayedBy)) return(true);
  lastPlayedBy = grid[1][3];
  if ((lastPlayedBy != "") &&
      (grid[2][3] == lastPlayedBy) &&
      (grid[3][3] == lastPlayedBy)) return(true);
  lastPlayedBy = grid[1][1];
  if ((lastPlayedBy != "") &&
      (grid[1][2] == lastPlayedBy) &&
      (grid[1][3] == lastPlayedBy)) return(true);
  lastPlayedBy = grid[2][1];
  if ((lastPlayedBy != "") &&
      (grid[2][2] == lastPlayedBy) &&
      (grid[2][3] == lastPlayedBy)) return(true);
  lastPlayedBy = grid[3][1];
  if ((lastPlayedBy != "") &&
      (grid[3][2] == lastPlayedBy) &&
      (grid[3][3] == lastPlayedBy)) return(true);
  lastPlayedBy = grid[1][1];
  if ((lastPlayedBy != "") &&
      (grid[2][2] == lastPlayedBy) &&
      (grid[3][3] == lastPlayedBy)) return(true);
  lastPlayedBy = grid[1][3];
  if ((lastPlayedBy != "") &&
      (grid[2][2] == lastPlayedBy) &&
      (grid[3][1] == lastPlayedBy)) return(true);
}
function itsAtie() {
  for( var i = 1; i <= 3; i++)
    for( var j = 1; j <= 3; j++)
      if (grid[i][j] == "") return(false);
  return(true);
}
function handleBoxClick(i, j) {
    if (eval("box" + i + j + ".src").indexOf('initialbutton.gif') < 0) return;
    if (lastPlayedBy == "o") {
      eval("document.all.box" + i + j + ".src = 'xbutton.gif'");
      lastPlayedBy = "x";
    }
    else {  // lastPlayedBy = "x"
      eval("document.all.box" + i + j + ".src = 'obutton.gif'");
      lastPlayedBy = "o";
    }
    grid[i][j] = lastPlayedBy;
    if (rowComplete()) {
      alert("The " + lastPlayedBy + " wins");
      window.location.reload();
    }
    else if (itsAtie()) {
           alert("It's a tie");
           window.location.reload();
         }
}
function handleBodySave() {
  for( var i = 1; i <= 3; i++)
    for( var j = 1; j <= 3; j++) {
      game.setAttribute("box" + i + j + "srcPersistAttr",
        eval("box" + i + j + ".src")); // join with previous line
      game.setAttribute("grid" + i + j + "PersisitAttr", grid[i][j]);
    }
}
function handleBodyLoad() {
  for( var i = 1; i <= 3; i++)
    for( var j = 1; j <= 3; j++) {
      if (game.getAttribute("box" + i + j + "srcPersistAttr"))
        eval("box" + i + j + ".src = game.getAttribute('box"
          + i + j + "srcPersistAttr')"); // join with previous line
      if (game.getAttribute("grid" + i + j + "PersisitAttr"))
        grid[i][j] = game.getAttribute("grid" + i + j + "PersisitAttr");
    }
}
// -->
</SCRIPT>
</HEAD>
<BODY CLASS="saveFavorite" ID="game" onload="handleBodyLoad()"
  onsave="handleBodySave()"> // join with previous line
<IMG ID="box11" onClick="handleBoxClick(1,1)" SRC="initialbutton.gif">
<IMG ID="box21" onClick="handleBoxClick(2,1)" SRC="initialbutton.gif">
<IMG ID="box31" onClick="handleBoxClick(3,1)" SRC="initialbutton.gif"><BR>
<IMG ID="box12" onClick="handleBoxClick(1,2)" SRC="initialbutton.gif">
<IMG ID="box22" onClick="handleBoxClick(2,2)" SRC="initialbutton.gif">
<IMG ID="box32" onClick="handleBoxClick(3,2)" SRC="initialbutton.gif"><BR>
<IMG ID="box13" onClick="handleBoxClick(1,3)" SRC="initialbutton.gif">
<IMG ID="box23" onClick="handleBoxClick(2,3)" SRC="initialbutton.gif">
<IMG ID="box33" onClick="handleBoxClick(3,3)" SRC="initialbutton.gif">
</BODY>

https://www.internet.com


Created: August 28, 1998
Revised: August 28, 1998

URL: https://www.webreference.com/js/column24/code2.html