<!-- Original:  Ken Douglas (ken@ntlworld.com) -->
<!-- Web Site:  http://www.groveroad.herts.sch.uk -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
// static globals 
var maxheight = 9;
var maxwidth = maxheight;
var winscore = Math.round((maxheight * maxwidth / 2) + 0.5);
// dynamic globals
var player = 1; 
var won = 0;
function newGame() {
// sets all graphics back to default and clears scores
won = 0;
eval('document.squares.score1.value = 0');
eval('document.squares.score2.value = 0');
for (var y = 1; y <= maxheight; y ++ ) {
for (var x = 1; x <= maxwidth; x ++ ) {
document.images["x" + x + "y" + y].src = sqr[0].src;
document.images["vx" + x + "vy" + y].src = ver[0].src;
document.images["hx" + x + "hy" + y].src = hor[0].src;
   }
}
for (var a = 1; a <= maxheight; a ++ ) {
onemore = maxheight + 1;
document.images["vx" + onemore + "vy" + a].src = ver[0].src;
document.images["hx" + a + "hy" + onemore].src = hor[0].src;
   }
}
function preload() {
if (document.images) {
sqr = new makeArray(3);
sqr[0].src = "images/p0.gif";
sqr[1].src = "images/p1.gif";
sqr[2].src = "images/p2.gif";
ver = new makeArray(3);
ver[0].src = "images/v0.gif";
ver[1].src = "images/v1.gif";
ver[2].src = "images/v2.gif";
hor = new makeArray(3);
hor[0].src = "images/h0.gif";
hor[1].src = "images/h1.gif";
hor[2].src = "images/h2.gif";
sel = new makeArray(2);
sel[0].src = "images/notsel.gif";
sel[1].src = "images/sel.gif";
}
else {
alert("Sorry, this game needs a browser\nwhich supports the image object.");
   }
}
function makeArray(n) {
this.length = n;
for (i = 0; i < n; i ++) {
this[i] = new Image();
}
return this;
}
function go (type, a, b) {
// processes clicks on square verticals and horizontals...
hit = 0;
if (type == 1) {
if (document.images["hx" + a + "hy" + b].src == hor[1].src) {
alert("Already taken - try again");
return;
}
document.images["hx" + a + "hy" + b].src = hor[1].src;
// figure out if the square above is captured 
if (b != 1) {
var an = a + 1;
var bn = b - 1;
 if ((document.images["vx" + a + "vy" + bn].src == ver[1].src) && (document.images["vx" + an + "vy" + bn].src == ver[1].src) && (document.images["hx" + a + "hy" + bn].src == hor[1].src)) {
document.images["x" + a + "y" + bn].src = sqr[player].src;
scoresOnDoors();
hit = 1;
   }
}
//figure out if the square below is captured
if (b != maxheight + 1) {
var an = a + 1;
var bn = b + 1;
if ((document.images["vx" + a + "vy" + b].src == ver[1].src) && (document.images["vx" + an + "vy" + b].src == ver[1].src) && (document.images["hx" + a + "hy" + bn].src == hor[1].src)) {
document.images["x" + a + "y" + b].src = sqr[player].src;
scoresOnDoors();
hit = 1;
      }
   }
}
if (type == 2) {
if (document.images["vx" + a + "vy" + b].src == ver[1].src) {
alert("Already taken - try again");
return;
}
document.images["vx" + a + "vy" + b].src = ver[1].src;
// figure out if the square right is captured 
if (a != maxwidth + 1) {
var an = a + 1;
var bn = b + 1;
if ((document.images["hx" + a + "hy" + b].src == hor[1].src) && (document.images["hx" + a + "hy" + bn].src == hor[1].src) && (document.images["vx" + an + "vy" + b].src == ver[1].src)) {
document.images["x" + a + "y" + b].src = sqr[player].src;
scoresOnDoors();
hit = 1;
   }
}
//figure out if the left is captured
if (a != 1) {
var an = a - 1;
var bn = b + 1;
if ((document.images["hx" + an + "hy" + b].src == hor[1].src) && (document.images["hx" + an + "hy" + bn].src == hor[1].src) && (document.images["vx" + an + "vy" + b].src == ver[1].src)) {
document.images["x" + an + "y" + b].src = sqr[player].src;
scoresOnDoors();
hit = 1;
      }
   }
}
// change players if no hit
if (hit == 0) {
if (player != 1) {player = 1
}
else {
player = 2;
}
showPlayer();
}
return;
}
function showPlayer() {
// let the users jnow which player is "up" by switching on the appropriate graphic
if (player == 1) {
document.images["play2"].src = sqr[0].src;
document.images["play1"].src = sqr[1].src;
}
if (player == 2) {
document.images["play1"].src = sqr[0].src;
document.images["play2"].src = sqr[2].src;
}
return;
}
function scoresOnDoors() {
// simple score increment and check - note play can comtinue after a winner is declared
eval('tmp = document.squares.score' + player + '.value');
tmp = tmp * 1;
tmp += 1;
eval('document.squares.score' + player + '.value = tmp');
if (won == 0 && tmp >= winscore) {
alert("Player " + player + " wins");
won = 1;
}
return;
}
//  End -->
