// Original:  Maximilian Stocker (maxstocker@reallyusefulcomputing.com)
// This script and many more are available free online at
// The JavaScript Source!! http://javascript.internet.com

var xxx = "/img/peketactoe-peke.gif";
// Location of where you uploaded your site's x.jpg image

var o = "/img/peketactoe-raccoon.gif";
// Location of where you uploaded your site's o.jpg image

var blank = "/img/peketactoe-blank.gif";
// Location of where you uploaded your site's blank.jpg image

var score = 0;
var winningsquares = "";
var disablegame = 0;

var pause = 0;
var all = 0;
var a = 0;
var b = 0;
var c = 0;
var d = 0;
var e = 0;
var f = 0;
var g = 0;
var h = 0;
var iii = 0;
var temp="";
var ok = 0;
var cf = 0;
var choice=9;
var aRandomNumber = 0;
var comp = 0;
var t = 0;
var wn = 0;
var ls = 0;
var ts = 0;

function help() {
alert("Welcome to Tic-Tac-Toe!  You play as the X's and the computer is the O's.  Select the square you want to put your X into by clicking them.  You cannot occupy a square that is already occupied. The first player to get three squares in a row wins.  Good Luck!!")
}
function logicOne() {
if ((a==1)&&(b==1)&&(c==1)) { all=1; winningsquares = "ABC"; }
if ((a==1)&&(d==1)&&(g==1)) { all=1; winningsquares = "ADG"; }
if ((a==1)&&(e==1)&&(iii==1)) { all=1; winningsquares = "AEI"; }
if ((b==1)&&(e==1)&&(h==1)) { all=1; winningsquares = "BEH"; }
if ((d==1)&&(e==1)&&(f==1)) { all=1; winningsquares = "DEF"; }
if ((g==1)&&(h==1)&&(iii==1)) { all=1; winningsquares = "GHI"; }
if ((c==1)&&(f==1)&&(iii==1)) { all=1; winningsquares = "CFI"; }
if ((g==1)&&(e==1)&&(c==1)) { all=1; winningsquares = "GEC"; }
if ((a==2)&&(b==2)&&(c==2)) { all=2; winningsquares = "ABC"; }
if ((a==2)&&(d==2)&&(g==2)) { all=2; winningsquares = "ADG"; }
if ((a==2)&&(e==2)&&(iii==2)) { all=2; winningsquares = "AEI"; }
if ((b==2)&&(e==2)&&(h==2)) { all=2; winningsquares = "BEH"; }
if ((d==2)&&(e==2)&&(f==2)) { all=2; winningsquares = "DEF"; }
if ((g==2)&&(h==2)&&(iii==2)) { all=2; winningsquares = "GHI"; }
if ((c==2)&&(f==2)&&(iii==2)) { all=2; winningsquares = "CFI"; }
if ((g==2)&&(e==2)&&(c==2)) { all=2; winningsquares = "GEC"; }
if ((a != 0)&&(b != 0)&&(c != 0)&&(d != 0)&&(e != 0)&&(f != 0)&&(g != 0)&&(h != 0)&&(iii != 0)&&(all == 0)) all = 3;
}
function logicTwo() {
if ((a==2)&&(b==2)&&(c== 0)&&(temp=="")) temp="C";
if ((a==2)&&(b== 0)&&(c==2)&&(temp=="")) temp="B";
if ((a== 0)&&(b==2)&&(c==2)&&(temp=="")) temp="A";
if ((a==2)&&(d==2)&&(g== 0)&&(temp=="")) temp="G";
if ((a==2)&&(d== 0)&&(g==2)&&(temp=="")) temp="D";
if ((a== 0)&&(d==2)&&(g==2)&&(temp=="")) temp="A";
if ((a==2)&&(e==2)&&(iii== 0)&&(temp=="")) temp="I";
if ((a==2)&&(e== 0)&&(iii==2)&&(temp=="")) temp="E";
if ((a== 0)&&(e==2)&&(iii==2)&&(temp=="")) temp="A";
if ((b==2)&&(e==2)&&(h== 0)&&(temp=="")) temp="H";
if ((b==2)&&(e== 0)&&(h==2)&&(temp=="")) temp="E";
if ((b== 0)&&(e==2)&&(h==2)&&(temp=="")) temp="B";
if ((d==2)&&(e==2)&&(f== 0)&&(temp=="")) temp="F";
if ((d==2)&&(e== 0)&&(f==2)&&(temp=="")) temp="E";
if ((d== 0)&&(e==2)&&(f==2)&&(temp=="")) temp="D";
if ((g==2)&&(h==2)&&(iii== 0)&&(temp=="")) temp="I";
if ((g==2)&&(h== 0)&&(iii==2)&&(temp=="")) temp="H";
if ((g== 0)&&(h==2)&&(iii==2)&&(temp=="")) temp="G";
if ((c==2)&&(f==2)&&(iii== 0)&&(temp=="")) temp="I";
if ((c==2)&&(f== 0)&&(iii==2)&&(temp=="")) temp="F";
if ((c== 0)&&(f==2)&&(iii==2)&&(temp=="")) temp="C";
if ((g==2)&&(e==2)&&(c== 0)&&(temp=="")) temp="C";
if ((g==2)&&(e== 0)&&(c==2)&&(temp=="")) temp="E";
if ((g== 0)&&(e==2)&&(c==2)&&(temp=="")) temp="G";
}
function logicThree() {
if ((a==1)&&(b==1)&&(c==0)&&(temp=="")) temp="C";
if ((a==1)&&(b==0)&&(c==1)&&(temp=="")) temp="B";
if ((a==0)&&(b==1)&&(c==1)&&(temp=="")) temp="A";
if ((a==1)&&(d==1)&&(g==0)&&(temp=="")) temp="G";
if ((a==1)&&(d==0)&&(g==1)&&(temp=="")) temp="D";
if ((a==0)&&(d==1)&&(g==1)&&(temp=="")) temp="A";
if ((a==1)&&(e==1)&&(iii==0)&&(temp=="")) temp="I";
if ((a==1)&&(e==0)&&(iii==1)&&(temp=="")) temp="E";
if ((a==0)&&(e==1)&&(iii==1)&&(temp=="")) temp="A";
if ((b==1)&&(e==1)&&(h==0)&&(temp=="")) temp="H";
if ((b==1)&&(e==0)&&(h==1)&&(temp=="")) temp="E";
if ((b==0)&&(e==1)&&(h==1)&&(temp=="")) temp="B";
if ((d==1)&&(e==1)&&(f==0)&&(temp=="")) temp="F";
if ((d==1)&&(e==0)&&(f==1)&&(temp=="")) temp="E";
if ((d==0)&&(e==1)&&(f==1)&&(temp=="")) temp="D";
if ((g==1)&&(h==1)&&(iii==0)&&(temp=="")) temp="I";
if ((g==1)&&(h==0)&&(iii==1)&&(temp=="")) temp="H";
if ((g==0)&&(h==1)&&(iii==1)&&(temp=="")) temp="G";
if ((c==1)&&(f==1)&&(iii==0)&&(temp=="")) temp="I";
if ((c==1)&&(f==0)&&(iii==1)&&(temp=="")) temp="F";
if ((c==0)&&(f==1)&&(iii==1)&&(temp=="")) temp="C";
if ((g==1)&&(e==1)&&(c==0)&&(temp=="")) temp="C";
if ((g==1)&&(e==0)&&(c==1)&&(temp=="")) temp="E";
if ((g==0)&&(e==1)&&(c==1)&&(temp=="")) temp="G";
}
function clearOut() {
document.getElementById("ptt-you").innerHTML = "0";
document.getElementById("ptt-computer").innerHTML = "0";
document.getElementById("ptt-ties").innerHTML = "0";
}
function checkSpace() {
if ((temp=="A")&&(a==0)) {
ok=1;
if (cf==0) a=1;
if (cf==1) a=2;
}
if ((temp=="B")&&(b==0)) {
ok=1;
if (cf==0) b=1;
if (cf==1) b=2;
}
if ((temp=="C")&&(c==0)) {
ok=1;
if (cf==0) c=1;
if (cf==1) c=2;
}
if ((temp=="D")&&(d==0)) {
ok=1;
if (cf==0) d=1;
if (cf==1) d=2;
}
if ((temp=="E")&&(e==0)) {
ok=1;
if (cf==0) e=1;
if (cf==1) e=2;
}
if ((temp=="F")&&(f==0)) {
ok=1
if (cf==0) f=1;
if (cf==1) f=2;
}
if ((temp=="G")&&(g==0)) {
ok=1
if (cf==0) g=1;
if (cf==1) g=2;
}
if ((temp=="H")&&(h==0)) {
ok=1;
if (cf==0) h=1;
if (cf==1) h=2;
}
if ((temp=="I")&&(iii==0)) {
ok=1;
if (cf==0) iii=1;
if (cf==1) iii=2;
}
}

function yourChoice(chName)
{

	if ( disablegame == 1 ) return;

	pause = 0;

	if (all!=0)
	{
		reset();
		return;
	}

	if (all==0)
	{
		cf = 0;
		ok = 0;
		temp=chName;
		checkSpace();
		if (ok==1)
		{
			document.images[chName].src = xxx;
		}
		if (ok==0)taken();
		process();
		if ((all==0)&&(pause==0)) myChoice();
   }
}

function taken() {
alert("That square is already occupied.  Please select another square.")
pause=1;
}
function myChoice() {
temp="";
ok = 0;
cf=1;
logicTwo();
logicThree();
checkSpace();
while(ok==0) {
aRandomNumber=Math.random()
comp=Math.round((choice-1)*aRandomNumber)+1;
if (comp==1) temp="A";
if (comp==2) temp="B";
if (comp==3) temp="C";
if (comp==4) temp="D";
if (comp==5) temp="E";
if (comp==6) temp="F";
if (comp==7) temp="G";
if (comp==8) temp="H";
if (comp==9) temp="I";
checkSpace();
}
document.images[temp].src= o;
process();
}
function ended() {
//alert("The game has already ended. To play a new game click the Play Again button.")
//playAgain();
//reset();
}
function process() {
logicOne();

// win
if (all==1)
{
	var gameletters = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I");
	var timercount = 1000; disablegame = 1; setTimeout ('disablegame = 0', 1450 );
	var notblank = 0;
	for ( letx = 0 ; letx < gameletters.length ; letx++ )
	{
		if ( winningsquares.indexOf(gameletters[letx] ) > -1 )
		{
			setTimeout (  'document.images.' + gameletters[letx] + '.src=blank' , 100 );
			setTimeout (  'document.images.' + gameletters[letx] + '.src=xxx' , 200 );
			setTimeout (  'document.images.' + gameletters[letx] + '.src=blank', 300 );
			setTimeout (  'document.images.' + gameletters[letx] + '.src=xxx' , 400 );
			setTimeout (  'document.images.' + gameletters[letx] + '.src=blank' , 500 );
			setTimeout (  'document.images.' + gameletters[letx] + '.src=xxx' , 600 );
		}
	}
	for ( letx = 0 ; letx < gameletters.length ; letx++ )
	{
		timercount += 50 ;
		setTimeout (  'document.images.' + gameletters[letx] + '.src="/img/peketactoe-win' + gameletters[letx] + '.gif"'  , timercount) ;
		if ( letx == gameletters.length-1 ) gameletters[letx] = 'iii' ; // long story, but it's a bug hack
		if ( eval ( gameletters[letx].toLowerCase() ) > 0 ) notblank++ ;
	}
	wn++;
	score += notblank * 54321;
}

// lose
if (all==2)
{
	var gameletters = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I");
	var timercount = 1000; disablegame = 1; setTimeout ('disablegame = 0', 1450 );
	var notblank = 0;
	for ( letx = 0 ; letx < gameletters.length ; letx++ )
	{
		if ( winningsquares.indexOf(gameletters[letx] ) > -1 )
		{
			setTimeout (  'document.images.' + gameletters[letx] + '.src=blank' , 100 );
			setTimeout (  'document.images.' + gameletters[letx] + '.src=o' , 200 );
			setTimeout (  'document.images.' + gameletters[letx] + '.src=blank', 300 );
			setTimeout (  'document.images.' + gameletters[letx] + '.src=o' , 400 );
			setTimeout (  'document.images.' + gameletters[letx] + '.src=blank' , 500 );
			setTimeout (  'document.images.' + gameletters[letx] + '.src=o' , 600 );
		}
	}
	for ( letx = 0 ; letx < gameletters.length ; letx++ )
	{
		timercount += 50 ;
		setTimeout (  'document.images.' + gameletters[letx] + '.src="/img/peketactoe-lose' + gameletters[letx] + '.gif"'  , timercount) ;
		if ( letx == gameletters.length-1 ) gameletters[letx] = 'iii' ; // long story, but it's a bug hack
		if ( eval ( gameletters[letx].toLowerCase() ) > 0 ) notblank++ ;
	}
	ls++;
	score -= notblank * 54321;
	if ( score < 0 ) score = 0;
}

// draw
if (all==3)
{
	var gameletters = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I");
	var timercount = 0; disablegame = 1; setTimeout ('disablegame = 0', 450 );
	var notblank = 0;
	for ( letx = 0 ; letx < gameletters.length ; letx++ )
	{
		timercount += 50;
		setTimeout (  'document.images.' + gameletters[letx] + '.src="/img/peketactoe-draw' + gameletters[letx] + '.gif"'  , timercount);
		if ( letx == gameletters.length-1 ) gameletters[letx] = 'iii' ; // long story, but it's a bug hack
		if ( eval ( gameletters[letx].toLowerCase() ) > 0 ) notblank++;
	}
	ts++;
	score -= Math.round ( notblank * 54321 / 10 ) ;
	if ( score < 0 ) score = 0;
}
if (all!=0) {
document.getElementById("ptt-you").innerHTML = wn;
document.getElementById("ptt-computer").innerHTML = ls;
document.getElementById("ptt-ties").innerHTML = ts;
document.getElementById("ptt-score").innerHTML = numbercommas(score);
document.pttsubmit.userscore.value = score;

// get lowest score on board
if(document.all) lowestscore = document.getElementById("ptt-scoreboard20").innerText;
else lowestscore = document.getElementById("ptt-scoreboard20").textContent;
lowestscore = lowestscore.replace(/,/g,"");

// decide
if ( score > lowestscore )
	{
		document.getElementById("ptt-score").className = "green";
		document.getElementById("ptt-scoresubmit").innerHTML = '<a href="#" onclick="javascript: document.getElementById(\'ptt-submit\').style.display= \'block\'; document.getElementById(\'ptt-text\').focus(); return false;" title="Your score qualifies for our scoreboard. Click to submit!">High Score!</a>';
	}
else
{
	document.getElementById("ptt-score").className = "black";
	document.getElementById("ptt-scoresubmit").innerHTML = "";
	document.getElementById("ptt-submit").style.display = "none";
}

   }
}
function playAgain() {
if (all==0) {
if(confirm("This will restart the game and clear all the current scores. OK?")) reset();
}
if (all>0) reset();
}
function reset() {
all = 0;
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
g = 0;
h = 0;
iii = 0;
temp="";
ok = 0;
cf = 0;
choice=9;
aRandomNumber = 0;
comp = 0;
document.images.A.src= blank;
document.images.B.src= blank;
document.images.C.src= blank;
document.images.D.src= blank;
document.images.E.src= blank;
document.images.F.src= blank;
document.images.G.src= blank;
document.images.H.src= blank;
document.images.I.src= blank;
if (t==0) { t=2; myChoice(); }
t--;
}