//tttAI
//AI is now fully working, but still could be improved
//Andy Harris, June 2003

init();

function init(){
  //set up X values
  xVals = new Array(130, 230, 330, 130, 230, 330, 130, 230, 330);
  yVals = new Array(120, 120, 120, 210, 210, 210, 300, 300, 300);
  board = new Array("blank", "blank", "blank", "blank", "blank", 
					  "blank", "blank", "blank", "blank");
  turn = "x";

  //winningCombo is an array of all winning combos
  winningCombo = new Array(
    new Array(0, 1, 2),
    new Array(3, 4, 5),
    new Array(6, 7, 8),
    new Array(0, 3, 6),
    new Array(1, 4, 7),
    new Array(2, 5, 8),
    new Array(0, 4, 8),
    new Array(2, 4, 6)
  );

  //rank is initial rank of each cell in board.  The heuristic will modify the
  //modify the ranking during game play
  rank = new Array(3, 2, 3, 2, 4, 2, 3, 2, 3);

  //build classes from cell
  for (i = 0; i < 9; i++){
    _root.attachMovie("cell", "cell_" + i, 100 + i);
    theCell = eval("cell_" + i);
    theCell._x = xVals[i];
    theCell._y = yVals[i];
    theCell.state = "blank";
    theCell.index = i;
    theCell.gotoAndStop(theCell.state);
    	  
    //add click event
    theCell.onMouseUp = function(){
      //respond only if mouse is over me (aaaargh!)
      if (this.hitTest(_root._xmouse, _root._ymouse, false)){
        currentSquare = this.index;
        if(board[currentSquare] == "blank"){
          board[currentSquare] = turn;
          this.state = turn;
          this.gotoAndStop(this.state);
          checkForWinner();
          showHint();
          if (turn == "x"){
            turn = "o";
            computerPlay();
          } else {
            turn = "x";
          } // end 'whos turn' if
        } // end 'board is blank' if
      } // end "hit this instance" if
    } // end mouseUp
  } // end for

  showHint();
} // end init

function checkForWinner(){
  //looks to see if the board matches any winning combos
  for (comboNum = 0; comboNum < 8; comboNum++){
    firstVal = winningCombo[comboNum][0];
    secondVal = winningCombo[comboNum][1];
    thirdVal = winningCombo[comboNum][2];

    if ((board[firstVal] == board[secondVal]) &&
      (board[secondVal] == board[thirdVal]) &&
      (board[firstVal] != "blank")){
      winner = board[firstVal];
      trace (winner + " wins!");
    } // end if

  } // end for
} // end checkForWinner
				 
function showHint(){
  //evaluates board, determines best move available
  //start with clean rank each time
  rank = new Array(3, 2, 3, 2, 4, 2, 3, 2, 3);
  
  bestCell = 0;
  highestRank = -999;
  	
  //raise ranking of any score that will win or lose game
  for (comboNum = 0; comboNum < 8; comboNum++){

    firstVal = winningCombo[comboNum][0];
    secondVal = winningCombo[comboNum][1];
    thirdVal = winningCombo[comboNum][2];

    if ((board[firstVal] == board[secondVal]) &&
	(board[firstVal] != "blank")){
      rank[thirdVal] += 5;
    } // end if

    if ((board[firstVal] == board[thirdVal]) &&
	(board[firstVal] != "blank")){
      rank[secondVal] += 5;
    } // end if

    if ((board[secondVal] == board[thirdVal]) &&
        (board[secondVal] != "blank")){
      rank[firstVal] += 5;
    } // end if  	  
  } // end for
  	
  //lower ranking of any cell currently containing anything but blank
  for (cellNum = 0; cellNum < 9; cellNum++){
    if (board[cellNum] != "blank"){
      rank[cellNum] -= 99;
    } // end if
  		
    //see if this is the highest ranking cell so far
    if (rank[cellNum] > highestRank){
      bestCell = cellNum;
      highestRank = rank[cellNum];
    } // end if

  } // end for

  traceRank();
  //trace ("try " + bestCell);
  //_root.outputBest = bestCell;
  	
} // end showHint

function traceRank(){
  //outputs the current rank structure for debugging purposes
  trace (rank[0] + ", " + rank[1] + ", " + rank[2]);
  trace (rank[3] + ", " + rank[4] + ", " + rank[5]);
  trace (rank[6] + ", " + rank[7] + ", " + rank[8]);
  trace("");
} // end traceRank

function computerPlay(){
  //computer simply chooses best ranking cell
  board[bestCell] = "o";
  theCell = eval("cell_" + bestCell);
  theCell.currentState = "o";
  theCell.gotoAndStop(theCell.currentState);
  turn = "x";
  showHint();
} // end computerPlay