var strategy = [5, 1, 2, 7, 9, 3, 4, 6, 8];
var wins = [];
wins[0] = [1, 2, 3];
wins[1] = [4, 5, 6];
wins[2] = [7, 8, 9];
wins[3] = [1, 4, 7];
wins[4] = [2, 5, 8];
wins[5] = [3, 6, 9];
wins[6] = [1, 5, 9];
wins[7] = [3, 5, 7];
var board = [];
var me = 'O';
var you = 'X';
var gameover = false;

function newGame()
{
	for (i = 1; i < 10; i++)
	{
		setSquare(i, '');
		board[i] = '';
		document.getElementById('square' + i).style.backgroundColor = 'transparent';
	}
	document.getElementById('goButton').display = '';
	gameover = false;
	setMessage('Make your move!');
	if (me == 'X') {
		programMove();
	}
	
	// Choose a strategy for some game variety
	strategynum = Math.floor(Math.random() * 5);
	switch (strategynum)
	{
		case 0:
			// Standard Strategy
			strategy = [5, 1, 2, 7, 9, 3, 4, 6, 8];
			break;
			
		case 1:
			// Corner Strategy
			strategy = [7, 5, 4, 9, 1, 8, 6, 3, 2]; 
			break;
			
		case 2:
			// Losing Strategy
			strategy = [5, 3, 1, 7, 6, 4, 9, 2, 8];
			break;
			
		case 3:
			strategy = [5, 7, 8, 2, 4, 9, 3, 6, 1];
			break;
			
		case 4:
			strategy = [5, 9, 4, 1, 3, 6, 8, 4, 7];
			break;
	}		
}			

function setSquare(square, player)
{
	document.getElementById('square' + square).className = 'square val' + player;
	board[square] = player;
}

function playerMove(square)
{
	if (gameover) {return false;}
	if (board[square]) {
		setMessage('That space is taken!  Try again');
		return false;
	}
	
	setSquare(square, you);
	if (!checkForWin(you)) {
		checkForTie();
		programMove();
	}
}

function checkForWin(player)
{
	if (!player || gameover) {return false;}
	for (i = 0; i < 8; i++)
	{		
		if (board[wins[i][0]] == player && board[wins[i][1]] == player && board[wins[i][2]] == player) {
			lightRow(i, 'yellow');
			gameover = true;
			if (player == you) {
				setMessage('You win!  Great job!');
				return true;
			}
			if (player == me) {
				setMessage('I win!  Better luck next time!');
				return true;
			}
		}
	}
	return false;
}

function checkForTie()
{
	remain = 9;
	for (i = 1; i < 10; i++)
	{
		if (!board[i]) {return;}
	}
	setMessage('This game is a tie!');
	lightRow(0, 'yellow');
	lightRow(2, 'yellow');
	lightRow(3, 'yellow');
	gameover = true;
}	


function programMove()
{
	if (gameover) {return false;}
	move = 0;
	
	// See if I've got a win
	move = findWinFor(me);
	
	// See if I need to make a block
	if (!move) {move = findWinFor(you);}
	
	// Make a regular move
	if (!move) {move = findMove();}
	
	if (!move) {
		checkForTie()
	} else {
		setSquare(move, me);
		if (!checkForWin(me)) {
			checkForTie();
		}
	}
}

function findWinFor(player)
{
	for (i = 0; i < 8; i++)
	{
		have = 0;
		empty = 0;
		for (j = 0; j < 3; j++)
		{
			if (board[wins[i][j]] == player) {have++;}
			if (!board[wins[i][j]]) {empty = wins[i][j];}
		}
		if (have > 1 && empty > 0) {
			return empty;
		}
	}
}

function findMove()
{
	for (i = 0; i < 9; i++)
	{
		if (!board[strategy[i]]) {return strategy[i];}
	}
}

function lightRow(r, col)
{
	for (s = 0; s < 3; s++)
	{
		document.getElementById('square' + wins[r][s]).style.backgroundColor = col;
	}
}

function setMessage(message)
{
	document.getElementById('message').innerHTML = message;
}

function changeSides()
{
	tmp = you;
	you = me;
	me = tmp;
	setMessage('You are ' + you + ' and I am ' + me);	

	if (gameover) {
		newGame();
	} else {
		programMove();
	}
}