$(document).ready(function() {
	var playerIsBlack;
	var board;
	var score;
	var clicked = function(n) {
		return function(e) {
			if(makeMove(n%8,Math.floor(n/8),playerIsBlack?'black':'white',$(this))) {
				playerIsBlack = !playerIsBlack;
				if(!playerIsBlack) findMove(false,true);
				if(!findMove(playerIsBlack,false)) {
					info("no move for "+(playerIsBlack?'black':'white'),2000);
					playerIsBlack = !playerIsBlack;
					if(!playerIsBlack) findMove(false,true);
				}
			}
		}
	};
	$('#reset').click(function() {
		var n = 0;
		$('#board>div').html('').each(function() {
			$(this).unbind();
		});
		initBoard();
	});
	initBoard();

	function initBoard() {
		board = [[],[],[],[],[],[],[],[]];
		score = {white:0,black:0};
		playerIsBlack = true;
		var n = 0;
		$('#board>div').addClass('inplay').each(function() {
			$(this).click(clicked(n++));
		});
		makeMove(3,3,"white");
		makeMove(4,4,"white");
		makeMove(3,4,"black");
		makeMove(4,3,"black");
	}

	function info(text,time) {
		$('#info').text(text).fadeIn().fadeOut(time);
	}

	function makeMove(x,y,color,elm) {
		if(!elm) elm=$('#board>div:eq('+(y*8+x)+')');
		if(!validMove(x,y,color)) {
			info("invalid move",1000);
			return false;
		}
		elm.removeClass('inplay').html("<div class='peice "+color+"'></div>").unbind();
		board[x][y] = color;
		score[color]++;
		writeScore();
		return true;
	}

	function writeScore() {
		$('#black').text("black: "+score.black);
		$('#white').text("white: "+score.white);
	}

	function findMove(isBlack,doMove) {
		var moves = {};
		for(var x=0;x<8;x++)
			for(var y=0;y<8;y++) {
				var m = moveWorth(x,y,isBlack);
				if(m>0) {
					if(!doMove) return true;
					if(typeof moves[m] == 'undefined') moves[m] = [];
					moves[m].push({x:x,y:y});
				}
			}
		var max = 0;
		for(var i in moves) if(i>max) max=i;
		if(max<1) {
			info("no move for "+(playerIsBlack?'black':'white'),2000);
			playerIsBlack = !playerIsBlack;
			return false;
		}
		var r = Math.floor(Math.random()*moves[max].length);
		var move = moves[max][r];
		makeMove(move.x,move.y,isBlack?'black':'white');
		playerIsBlack = !playerIsBlack;
		return true;
	}

	function moveWorth(x,y,isBlack) {
		if(typeof board[x][y] != 'undefined') return 0;
		var re = 0;
		for(var xd=-1;xd<=1;xd++) {
			for(var yd=-1;yd<=1;yd++) {
				var t = trace(x,y,xd,yd,isBlack?'black':'white',false);
				if(t>0) re+=t;
			}
		}
		return re;
	}

	function validMove(x,y,color) {
		var valid = false;
		if(typeof board[x][y] != 'undefined') return false;
		if((x==3||x==4)&&(y==3||y==4)) return true;
		for(var xd=-1;xd<=1;xd++) {
			for(var yd=-1;yd<=1;yd++) {
				if(trace(x,y,xd,yd,color,true)>0) valid = true;
			}
		}
		return valid;
	}

	function trace(x,y,xd,yd,color,flip,depth) {
		if(typeof depth == "undefined") depth=0;
		var acolor = color=='white'?'black':'white';
		var nx = x+xd, ny = y+yd;
		if(nx<0||nx>=8||ny<0||ny>=8||typeof board[nx][ny]=="undefined") return -1;
		if(board[nx][ny]!=acolor) {
			if(depth==0) return -1;
			if(board[nx][ny]==color) return 0;
		}
		var next = trace(nx,ny,xd,yd,color,flip,depth+1);
		if(next==-1) return -1;
		if(flip) {
			$('#board>div:eq('+(ny*8+nx)+')').removeClass('inplay').html("<div class='peice "+color+"'></div>");
			board[nx][ny] = color;
			score[color]++;
			score[acolor]--;
		}
		return 1+next;
	}
});

