﻿// JScript File
// Author: S Miller
// Date: Feb. 6, 2007
// This code is written as one of development projects 
// for dbsoup.org and smifzone.org from the scratch. 
// For any reason if you have to use any part of this
// coding, please get a permission from 
// webadmin@smifzone.org.
//
// Content: Tic Tac Toe Game
//

var ticBox = new Array(9);
var timeOn = false;
var gameOn = false;
var choicePlayer = "";
var vFirst = false;

function initTicTacToe()
{
  var i;
  var temp;
    
  timeOn = true;
  gameOn = true;
  vFirst = true;
  document.getElementById('Text1').value = "";
  
  for(i=0;i<9;i++){
    ticBox[i] = 0;
    temp = "t" + (i+1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticW.gif";
  }
  
}


function checkOrd(userChoice)
{
  var ch=userChoice;
  
  choicePlayer = userChoice;
    
  if (choicePlayer == "me")
  {
    initTicTacToe();
  }
  else if (choicePlayer == "cp")
  {
    initTicTacToe();
    playComputerFirst();
  }
  
  return;

}

function findPlayer()
{
  if (choicePlayer == "cp")
  {
    initTicTacToe();
    playComputerFirst();
  }
  else
  {
    initTicTacToe();
  }

  return;
}




function switchImg(boxValue)
{
  var temp;
  var boxN = boxValue;
  var i;
  var gug1=0, gug2=0;
  var compStat=0;

  if ((!timeOn) || (!gameOn))
  {
    return;
  }
  //
  // ticbox value 1:  by a user
  // ticbox value 2:  by a computer
  //
  
  if ((ticBox[boxN] != 1) && (ticBox[boxN] != 2))
  { 
    temp = "t" + (boxN+1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticX.gif";
    ticBox[boxN] = 1;
    gug1 = checkIfAnyoneWon();
    if (gug1 == 0){
      gug2 = checkIfcanWin(); 
      if (gug2 == 0){
        byComputer(boxN);
      }
      else
      {
        completeBox(gug2);
        return;
      }
    }
    else
    {     
      return;
    }
  }
  else
  {
    return;
  }
  
}


function playComputerFirst()
{
  var rdm = Math.floor(Math.random()*10);
  var rdmN = rdm % 9;
  var i=0;
  var temp="";
    
  ticBox[rdmN] = 2;
  temp = "t" + (rdmN + 1);
  document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
 
  //
  vFirst = false;
  return;
  
}


function completeBox(ltc)
{
  var lineValue = ltc;
  var i;
  var temp="";
  
  if (lineValue == 1)
  {
    for (i=0;i<3;i++){
      if (ticBox[i] == 0)
      {
        temp = "t" + (i+1);
        document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
        ticBox[i] = 2;
      }
    }
  }

  if (lineValue == 2)
  {
    for (i=3;i<6;i++){
      if (ticBox[i] == 0)
      {
        temp = "t" + (i+1);
        document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
        ticBox[i] = 2;
      }
    }
  }

  if (lineValue == 3)
  {
    for (i=6;i<9;i++){
      if (ticBox[i] == 0)
      {
        temp = "t" + (i+1);
        document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
        ticBox[i] = 2;
      }
    }
  }

  if (lineValue == 4)
  {
    for (i=0;i<7;i=i+3){
      if (ticBox[i] == 0)
      {
        temp = "t" + (i+1);
        document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
        ticBox[i] = 2;
      }
    }
  }

  if (lineValue == 5)
  {
    for (i=1;i<8;i=i+3){
      if (ticBox[i] == 0)
      {
        temp = "t" + (i+1);
        document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
        ticBox[i] = 2;
      }
    }
  }

  if (lineValue == 6)
  {
    for (i=2;i<9;i=i+3){
      if (ticBox[i] == 0)
      {
        temp = "t" + (i+1);
        document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
        ticBox[i] = 2;
      }
    }
  }

  if (lineValue == 7)
  {
    for (i=0;i<9;i=i+4){
      if (ticBox[i] == 0)
      {
        temp = "t" + (i+1);
        document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
        ticBox[i] = 2;
      }
    }
  }

  if (lineValue == 8)
  {
    for (i=2;i<7;i=i+2){
      if (ticBox[i] == 0)
      {
        temp = "t" + (i+1);
        document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
        ticBox[i] = 2;
      }
    }
  }

  document.getElementById('Text1').value = "I(computer) Won!";
  gameOn = false;

  return;

}




function byComputer(B)
{
  var boxN = B;
  
  
  if (boxN == 0){
    ClickedA();    
  }
  
  if (boxN == 1){
    ClickedB();
  }

  if (boxN == 2){
    ClickedC();
  }

  if (boxN == 3){
    ClickedD();
  }

  if (boxN == 4){
    ClickedE();
  }

  if (boxN == 5){
    ClickedF();
  }

  if (boxN == 6){
    ClickedG();
  }

  if (boxN == 7){
    ClickedH();
  }

  if (boxN == 8){
    ClickedI();
  }
  
  vFirst = false;
  checkIfAnyoneWon();
  return;
}



function ClickedA()
{
  var i;
  var oneS1=0, twoS1=0;
  var oneS2=0, twoS2=0;
  var oneS3=0, twoS3=0;
  var found=0;
  var temp;
  var winPrty=0;
  var level=0, empty1=0, empty2=0, empty3=0;
  
    for(i=0;i<3;i++){
      if (ticBox[i] == 1){
        oneS1++;
      }
      else if (ticBox[i] == 2){
        twoS1++;
      }
      else {
        empty1++;
      }
    }
    
    for(i=0;i<7;i=i+3){
      if (ticBox[i] == 1){
        oneS2++;
      }
      else if (ticBox[i] == 2){
        twoS2++;
      }
      else {
        empty2++;
      }
    }
    
    for(i=0;i<9;i=i+4){
      if (ticBox[i] == 1){
        oneS3++;
      }
      else if (ticBox[i] == 2){
        twoS3++;
      }
      else {
        empty3++;
      }
    }
    
    //
    // if no one has won, proceed
    //
    //maxLine = findMax(oneS1, oneS2, oneS3);
    

    if ((vFirst == true) && (choicePlayer == "me"))
    {
      var rmd = Math.floor(Math.random()*10);
      rmd = (rmd % 3) + 1;
      maxLine = rmd;
    } 
    else if ((oneS1 >= oneS2) && (oneS1 >= oneS3))
    {
      if (empty1 > 0){
        maxLine = 1;
      }
      else {
        if (oneS2 >= oneS3)
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else if (empty3 > 0)
          {
            maxLine = 3;
          }
        }
        if (oneS3 > oneS2)
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else if (empty2 > 0)
          {
            maxLine = 2;
          }
        }
      }
    }
    else if ((oneS2 >= oneS1) && (oneS2 >= oneS3))
    {
      if (empty2 > 0){
        maxLine = 2;
      }
      else {
        if (oneS1 >= oneS3)
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else if (empty3 > 0)
          {
            maxLine = 3;
          }
        }
        if (oneS3 > oneS1)
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else if (empty1 > 0)
          {
            maxLine = 1;
          }
        }
      }
    }    
    else if ((oneS3 >= oneS2) && (oneS3 >= oneS1))
    {
      if (empty3 > 0){
        maxLine = 3;
      }
      else {
        if (oneS2 >= oneS1)
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else if (empty1 > 0)
          {
            maxLine = 1;
          }
        }
        if (oneS1 > oneS2)
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else if (empty2 > 0)
          {
            maxLine = 2;
          }
        }
      }
    }
    else
    {
      findEmpty()
      return;
    }
    
    
    if (maxLine == 1){
      for(i=0;i<3;i++){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 2){
      for(i=0;i<7;i=i+3){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 3){
      for(i=0;i<9;i=i+4){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    temp = "t" + (found + 1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
    ticBox[found] = 2;
    
}


function ClickedB()
{
  var i;
  var oneS1=0, twoS1=0;
  var oneS2=0, twoS2=0;
  var found=0;
  var temp="";
  var empty1=0; empty2=0;
  var winPrty=0;
  
    for(i=0;i<3;i++){
      if (ticBox[i] == 1){
        oneS1++;
      }
      else if (ticBox[i] == 2){
        twoS1++;
      }
      else{
        empty1++;
      }
    }
    
    for(i=1;i<8;i=i+3){
      if (ticBox[i] == 1){
        oneS2++;
      }
      else if (ticBox[i] == 2){
        twoS2++;
      }
      else {
        empty2++;
      }
    }
   
    //
    // take care of the situation when a computer can win 
    //
    
    //maxLine = Math.max(oneS1, oneS2);
    //
    //
    
    if ((vFirst == true) && (choicePlayer == "me"))
    {
      var rmd = Math.floor(Math.random()*10);
      rmd = (rmd % 2) + 1;
      maxLine = rmd;
    }  
    else if (oneS1 >= oneS2)
    {
      if (empty1 > 0){
        maxLine = 1;
      }
      else if (empty2 > 0){
        maxLine = 2;
      }
    }    
    else if (oneS2 > oneS1)
    {
      if (empty2 > 0){
        maxLine = 2;
      }
      else if (empty1 > 0){
        maxLine = 1;
      }
    }
    else
    {
      findEmpty();
      return;
    }
    
    
    //maxLine = findMax(oneS1, oneS2, 0);
    

    if (maxLine == 1){
      for(i=0;i<3;i++){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 2){
      for(i=1;i<8;i=i+3){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
        
    ticBox[found] = 2;
    temp = "t" + (found + 1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
    
}


function findEmpty()
{
  var i;
  var fnd=false;
  var temp="";
  
  for(i=0;((i<9) && (fnd=false));i++)
  {
    if (ticBox[i] == 0){
      fnd = true;
      temp = "t" + (i + 1);
      document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
      ticBox[i] = 2;
    }
  }

}

//
// checks whether to stop or to continue
//
function checkIfAnyoneWon()
{
  var i;
  var lvl = 0;
  var oneS=0, twoS=0, emptyB=0;
    
  for(i=0;i<3;i++){
    if (ticBox[i] == 1){
      oneS++;
    }
    else if (ticBox[i] == 2){
      twoS++;
    }
  }
  if (twoS == 3){
    lvl = 2;
  }
  else if (oneS == 3){
    lvl = 1;
  }
  
  twoS = 0;
  oneS = 0;
  if (lvl == 0){
    for(i=3;i<6;i++){
      if (ticBox[i] == 1){
        oneS++;
      }
      else if (ticBox[i] == 2){
        twoS++;
      }
    }
    if (twoS == 3){
      lvl = 2;
    }
    else if (oneS == 3){
      lvl = 1;
    }
  }
  
  if (lvl == 0){
    twoS = 0;
    oneS = 0;
    for(i=6;i<9;i++){
      if (ticBox[i] == 1){
        oneS++;
      }
      else if (ticBox[i] == 2){
        twoS++;
      }
    }  
    if (twoS == 3){
      lvl = 2;
    }
    else if (oneS == 3){
      lvl = 1;
    }
  }
  
  if (lvl == 0){
    twoS = 0;
    oneS = 0;
    for(i=0;i<7;i=i+3){
      if (ticBox[i] == 1){
        oneS++;
      }
      else if (ticBox[i] == 2){
        twoS++;
      }
    } 
    if (twoS == 3){
      lvl = 2;
    }
    else if (oneS == 3){
      lvl = 1;
    }
  }
  
  if (lvl == 0){
    twoS = 0;
    oneS = 0;
    for(i=1;i<8;i=i+3){
      if (ticBox[i] == 1){
        oneS++;
      }
      else if (ticBox[i] == 2){
        twoS++;
      }
    }
    if (twoS == 3){
      lvl = 2;
    }
    else if (oneS == 3){
      lvl = 1;
    }
  }
  
  if (lvl == 0){
    twoS = 0;
    oneS = 0;
    for(i=2;i<9;i=i+3){
      if (ticBox[i] == 1){
        oneS++;
      }
      else if (ticBox[i] == 2){
        twoS++;
      }
    }
    if (twoS == 3){
      lvl = 2;
    }  
    else if (oneS == 3){
      lvl = 1;
    }
  }
  
  if (lvl == 0){
    twoS = 0;
    oneS = 0;
  //
  // check the cross
  //
    for(i=0;i<9;i=i+4){
      if (ticBox[i] == 1){
        oneS++;
      }
      else if (ticBox[i] == 2){
        twoS++;
      }
    }
    if (twoS == 3){
      lvl = 2;
    }   
    else if (oneS == 3){
      lvl = 1;
    }
  }
  
  if (lvl == 0){ 
    twoS = 0;
    oneS = 0;
    for(i=2;i<7;i=i+2){
      if (ticBox[i] == 1){
        oneS++;
      }
      else if (ticBox[i] == 2){
        twoS++;
      }
    }
    if (twoS == 3){
      lvl = 2;
    }   
    else if (oneS == 3){
      lvl = 1;
    }
  }
  
  if (lvl == 2){
    document.getElementById('Text1').value = "I(computer) Won!";
    gameOn = false;
  } 
  else if (lvl == 1){
    document.getElementById('Text1').value = "You(user) Won!";
    gameOn = false;
  } 
  else
  {
    for(i=0;i<9;i++){
      if (ticBox[i] == 0){
        emptyB++;
      }
    }
  
    if (emptyB == 0){
      lvl = 3;
      document.getElementById('Text1').value = "Even!";
      gameOn = false;
      return;
    }  
     
  }
  
  return lvl;
   
}



function ClickedC()
{
  var i;
  var oneS1=0, twoS1=0;
  var oneS2=0, twoS2=0;
  var oneS3=0, twoS3=0;
  var found=0;
  var temp;
  var winPrty=0;
  var level=0, empty1=0, empty2=0, empty3=0;
  
    for(i=0;i<3;i++){
      if (ticBox[i] == 1){
        oneS1++;
      }
      else if (ticBox[i] == 2){
        twoS1++;
      }
      else {
        empty1++;
      }
    }
    
    for(i=2;i<7;i=i+2){
      if (ticBox[i] == 1){
        oneS2++;
      }
      else if (ticBox[i] == 2){
        twoS2++;
      }
      else {
        empty2++;
      }
    }
    
    for(i=2;i<9;i=i+3){
      if (ticBox[i] == 1){
        oneS3++;
      }
      else if (ticBox[i] == 2){
        twoS3++;
      }
      else {
        empty3++;
      }
    }
    //
    //
    //maxLine = findMax(oneS1, oneS2, oneS3);

    //
    if ((vFirst == true) && (choicePlayer == "me"))
    {
      var rmd = Math.floor(Math.random()*10);
      rmd = (rmd % 3) + 1;
      maxLine = rmd;
    }     
    else if ((oneS1 >= oneS2) && (oneS1 >= oneS3))
    {
      if (empty1 > 0){
        maxLine = 1;
      }
      else {
        if (oneS2 >= oneS3)
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else if (empty3 > 0)
          {
            maxLine = 3;
          }
        }
        if (oneS3 > oneS2)
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else if (empty2 > 0)
          {
            maxLine = 2;
          }
        }
      }
    }
    else if ((oneS2 >= oneS1) && (oneS2 >= oneS3))
    {
      if (empty2 > 0){
        maxLine = 2;
      }
      else {
        if (oneS1 >= oneS3)
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else if (empty3 > 0)
          {
            maxLine = 3;
          }
        }
        if (oneS3 > oneS1)
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else if (empty1 > 0)
          {
            maxLine = 1;
          }
        }
      }
    }    
    else if ((oneS3 >= oneS2) && (oneS3 >= oneS1))
    {
      if (empty3 > 0){
        maxLine = 3;
      }
      else {
        if (oneS1 >= oneS2)
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else if (empty2 > 0)
          {
            maxLine = 2;
          }
        }        
        if (oneS2 > oneS1)
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else if (empty1 > 0)
          {
            maxLine = 1;
          }
        }

      }
    }
    else
    {
      findEmpty()
      return;
    }
    
    
    if (maxLine == 1){
      for(i=0;i<3;i++){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 2){
      for(i=2;i<7;i=i+2){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 3){
      for(i=2;i<9;i=i+3){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    temp = "t" + (found + 1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
    ticBox[found] = 2;
    
}





function ClickedD()
{
  var i;
  var oneS1=0, twoS1=0;
  var oneS2=0, twoS2=0;
  var found=0;
  var temp="";
  var empty1=0; empty2=0;
  var winPrty=0;
  
    for(i=0;i<7;i=i+3){
      if (ticBox[i] == 1){
        oneS1++;
      }
      else if (ticBox[i] == 2){
        twoS1++;
      }
      else{
        empty1++;
      }
    }
    
    for(i=3;i<6;i++){
      if (ticBox[i] == 1){
        oneS2++;
      }
      else if (ticBox[i] == 2){
        twoS2++;
      }
      else {
        empty2++;
      }
    }
    

    //
    // take care of the situation when a computer can win 
    //
    
    //maxLine = Math.max(oneS1, oneS2);
    //
    if ((vFirst == true) && (choicePlayer == "me"))
    {
      var rmd = Math.floor(Math.random()*10);
      rmd = (rmd % 2) + 1;
      maxLine = rmd;
    }     
    else if (oneS1 >= oneS2)
    {
      if (empty1 > 0){
        maxLine = 1;
      }
      else if (empty2 > 0){
        maxLine = 2;
      }
    }    
    else if (oneS2 > oneS1)
    {
      if (empty2 > 0){
        maxLine = 2;
      }
      else if (empty1 > 0){
        maxLine = 1;
      }
    }
    else
    {
      findEmpty();
      return;
    }
    
    //maxLine = findMax(oneS1, oneS2, 0);
    

    if (maxLine == 1){
      for(i=0;i<7;i=i+3){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 2){
      for(i=3;i<6;i++){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
        
        
    ticBox[found] = 2;
    temp = "t" + (found + 1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
    
}



//
// 5 max
//
function ClickedE()
{
  var i;
  var oneS1=0, twoS1=0;
  var oneS2=0, twoS2=0;
  var oneS3=0, twoS3=0;
  var oneS4=0, twoS4=0;
  var found=0;
  var temp;
  var empty1=0, empty2=0, empty3=0, empty4=0;
  
  
  for(i=0;i<9;i=i+4){
    if (ticBox[i] == 1){
      oneS1++;
    }
    else if (ticBox[i] == 2){
      twoS1++;
    }
    else {
      empty1++;
    }
  }
  
  for(i=1;i<8;i=i+3){
    if (ticBox[i] == 1){
      oneS2++;
    }
    else if (ticBox[i] == 2){
      twoS2++;
    }
    else {
      empty2++;
    }
  }  
  
  for(i=2;i<7;i=i+2){
    if (ticBox[i] == 1){
      oneS3++;
    }
    else if (ticBox[i] == 2){
      twoS3++;
    }
    else {
      empty3++;
    }
  }  
  
  for(i=3;i<6;i++){
    if (ticBox[i] == 1){
      oneS4++;
    }
    else if (ticBox[i] == 2){
      twoS4++;
    }
    else {
      empty4++;
    }
  }  
    

    //
    // if no one has won, proceed
    //
    //maxLine = findMax(oneS1, oneS2, oneS3);
    //
    //NEED MORE WORK!!!

    //
    if ((vFirst == true) && (choicePlayer == "me"))
    {
      var rmd = Math.floor(Math.random()*10);
      rmd = (rmd % 4) + 1;
      maxLine = rmd;
    }     
    else if ((oneS1 >= oneS2) && (oneS1 >= oneS3) && (oneS1 >= oneS4))
    {
      if (empty1 > 0){
        maxLine = 1;
      }
      else {
        if ((oneS2 >= oneS3) && (oneS2 >= oneS4))
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else {
            if (oneS3 >= oneS4)
            {
              if (empty3 > 0)
              {
                maxLine = 3;
              }
              else if (empty4 > 0)
              {
                maxLine = 4;
              }
            }
            if (oneS4 > oneS3)
            {
              if (empty4 > 0)
              {
                maxLine =4;
              }
              else if (empty3 > 0)
              {
                maxLine = 3;
              }
            }
          }
        }
        //
        //
        else if ((oneS3 >= oneS2) && (oneS3 >= oneS4))
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else {
            if (oneS2 >= oneS4)
            {
              if (empty2 > 0)
              {
                maxLine = 2;
              }
              else if (empty4 > 0)
              {
                maxLine = 4;
              }
            }
            if (oneS4 > oneS2)
            {
              if (empty4 > 0)
              {
                maxLine =4;
              }
              else if (empty2 > 0)
              {
                maxLine = 2;
              }
            }
          }
        }
        else if ((oneS4 >= oneS3) && (oneS4 >= oneS2))
        {
          if (empty4 > 0)
          {
            maxLine = 4;
          }
          else {
            if (oneS2 >= oneS3)
            {
              if (empty2 > 0)
              {
                maxLine =2;
              }
              else if (empty3 > 0)
              {
                maxLine = 3;
              }
            }            
            if (oneS3 > oneS2)
            {
              if (empty3 > 0)
              {
                maxLine = 3;
              }
              else if (empty2 > 0)
              {
                maxLine = 2;
              }
            }

          }
        }
        //
        // 
      }
    }
    //  
    else if ((oneS2 >= oneS1) && (oneS2 >= oneS3) && (oneS2 >= oneS4))
    {
      if (empty2 > 0){
        maxLine = 2;
      }
      else {
        if ((oneS1 >= oneS3) && (oneS1 >= oneS4))
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else {
            if (oneS3 >= oneS4)
            {
              if (empty3 > 0)
              {
                maxLine = 3;
              }
              else if (empty4 > 0)
              {
                maxLine = 4;
              }
            }
            if (oneS4 > oneS3)
            {
              if (empty4 > 0)
              {
                maxLine =4;
              }
              else if (empty3 > 0)
              {
                maxLine = 3;
              }
            }
          }
        }
        //
        //
        else if ((oneS3 >= oneS1) && (oneS3 >= oneS4))
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else {
            if (oneS1 >= oneS4)
            {
              if (empty1 > 0)
              {
                maxLine = 1;
              }
              else if (empty4 > 0)
              {
                maxLine = 4;
              }
            }
            if (oneS4 > oneS1)
            {
              if (empty4 > 0)
              {
                maxLine =4;
              }
              else if (empty1 > 0)
              {
                maxLine = 1;
              }
            }
          }
        }
        else if ((oneS4 >= oneS3) && (oneS4 >= oneS1))
        {
          if (empty4 > 0)
          {
            maxLine = 4;
          }
          else {
            if (oneS1 >= oneS3)
            {
              if (empty1 > 0)
              {
                maxLine = 1;
              }
              else if (empty3 > 0)
              {
                maxLine = 3;
              }
            }            
            if (oneS3 > oneS1)
            {
              if (empty3 > 0)
              {
                maxLine = 3;
              }
              else if (empty1 > 0)
              {
                maxLine = 1;
              }
            }

          }
        }
        //
        // 
      }
    }   
    else if ((oneS3 >= oneS1) && (oneS3 >= oneS2) && (oneS3 >= oneS4))
    {
      if (empty3 > 0){
        maxLine = 3;
      }
      else {
        if ((oneS1 >= oneS2) && (oneS1 >= oneS4))
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else {
            if (oneS2 >= oneS4)
            {
              if (empty2 > 0)
              {
                maxLine = 1;
              }
              else if (empty4 > 0)
              {
                maxLine = 4;
              }
            }
            if (oneS4 > oneS2)
            {
              if (empty4 > 0)
              {
                maxLine =4;
              }
              else if (empty2 > 0)
              {
                maxLine = 2;
              }
            }
          }
        }
        //
        //
        else if ((oneS2 >= oneS1) && (oneS2 >= oneS4))
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else {
            if (oneS1 >= oneS4)
            {
              if (empty1 > 0)
              {
                maxLine = 1;
              }
              else if (empty4 > 0)
              {
                maxLine = 4;
              }
            }
            if (oneS4 > oneS1)
            {
              if (empty4 > 0)
              {
                maxLine =4;
              }
              else if (empty1 > 0)
              {
                maxLine = 1;
              }
            }
          }
        }
        else if ((oneS4 >= oneS1) && (oneS4 >= oneS2))
        {
          if (empty4 > 0)
          {
            maxLine = 4;
          }
          else {
            if (oneS1 >= oneS2)
            {
              if (empty1 > 0)
              {
                maxLine = 1;
              }
              else if (empty2 > 0)
              {
                maxLine = 2;
              }
            }
            if (oneS2 > oneS1)
            {
              if (empty2 > 0)
              {
                maxLine =2;
              }
              else if (empty1 > 0)
              {
                maxLine = 1;
              }
            }
          }
        }
        //
        // 
      }
    }      
    else if ((oneS4 >= oneS1) && (oneS4 >= oneS2) && (oneS4 >= oneS3))
    {
      if (empty4 > 0){
        maxLine = 4;
      }
      else {
        if ((oneS1 >= oneS2) && (oneS1 >= oneS3))
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else {
            if (oneS2 >= oneS4)
            {
              if (empty2 > 0)
              {
                maxLine = 2;
              }
              else if (empty4 > 0)
              {
                maxLine = 4;
              }
            }
            if (oneS4 > oneS2)
            {
              if (empty4 > 0)
              {
                maxLine =4;
              }
              else if (empty2 > 0)
              {
                maxLine = 2;
              }
            }
          }
        }
        //
        //
        else if ((oneS2 >= oneS1) && (oneS2 >= oneS3))
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else {
            if (oneS1 >= oneS3)
            {
              if (empty1 > 0)
              {
                maxLine = 1;
              }
              else if (empty3 > 0)
              {
                maxLine = 3;
              }
            }
            if (oneS3 > oneS1)
            {
              if (empty3 > 0)
              {
                maxLine =3;
              }
              else if (empty1 > 0)
              {
                maxLine = 1;
              }
            }
          }
        }
        else if ((oneS3 >= oneS1) && (oneS3 >= oneS2))
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else {
            if (oneS1 >= oneS2)
            {
              if (empty1 > 0)
              {
                maxLine = 1;
              }
              else if (empty2 > 0)
              {
                maxLine = 2;
              }
            }
            if (oneS2 > oneS1)
            {
              if (empty2 > 0)
              {
                maxLine =2;
              }
              else if (empty1 > 0)
              {
                maxLine = 1;
              }
            }
          }
        }
        //
        // 
      }
    }      
    else
    {
      findEmpty()
      return;
    }
    
    
    if (maxLine == 1){
      for(i=0;i<9;i=i+4){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 2){
      for(i=1;i<8;i=i+3){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
        
    if (maxLine == 3){
      for(i=2;i<7;i=i+2){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 4){
      for(i=3;i<6;i++){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    temp = "t" + (found + 1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
    ticBox[found] = 2;
    
    return;
    
}



function ClickedF()
{
  var i;
  var oneS1=0, twoS1=0;
  var oneS2=0, twoS2=0;
  var found=0;
  var temp="";
  var empty1=0; empty2=0;
  var winPrty=0;
  
    for(i=2;i<9;i=i+3){
      if (ticBox[i] == 1){
        oneS1++;
      }
      else if (ticBox[i] == 2){
        twoS1++;
      }
      else{
        empty1++;
      }
    }
    
    for(i=3;i<6;i++){
      if (ticBox[i] == 1){
        oneS2++;
      }
      else if (ticBox[i] == 2){
        twoS2++;
      }
      else {
        empty2++;
      }
    }
    

    //
    // take care of the situation when a computer can win 
    //
    //   
    if ((vFirst == true) && (choicePlayer == "me"))
    {
      var rmd = Math.floor(Math.random()*10);
      rmd = (rmd % 2) + 1;
      maxLine = rmd;
    } 
    else if (oneS1 >= oneS2)
    {
      if (empty1 > 0){
        maxLine = 1;
      }
      else if (empty2 > 0){
        maxLine = 2;
      }
    }    
    else if (oneS2 > oneS1)
    {
      if (empty2 > 0){
        maxLine = 2;
      }
      else if (empty1 > 0){
        maxLine = 1;
      }
    }
    else
    {
      findEmpty();
      return;
    }
    
    //maxLine = findMax(oneS1, oneS2, 0);
    

    if (maxLine == 1){
      for(i=2;i<9;i=i+3){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 2){
      for(i=3;i<6;i++){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
        
    ticBox[found] = 2;
    temp = "t" + (found + 1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
    
    return;
}





function ClickedG()
{
  var i;
  var oneS1=0, twoS1=0;
  var oneS2=0, twoS2=0;
  var oneS3=0, twoS3=0;
  var found=0;
  var temp;
  var winPrty=0;
  var level=0, empty1=0, empty2=0, empty3=0;
  
    for(i=0;i<7;i=i+3){
      if (ticBox[i] == 1){
        oneS1++;
      }
      else if (ticBox[i] == 2){
        twoS1++;
      }
      else {
        empty1++;
      }
    }
    
    for(i=2;i<7;i=i+2){
      if (ticBox[i] == 1){
        oneS2++;
      }
      else if (ticBox[i] == 2){
        twoS2++;
      }
      else {
        empty2++;
      }
    }
    
    for(i=6;i<9;i++){
      if (ticBox[i] == 1){
        oneS3++;
      }
      else if (ticBox[i] == 2){
        twoS3++;
      }
      else {
        empty3++;
      }
    }
    
    //
    //

    //
    if ((vFirst == true) && (choicePlayer == "me"))
    {
      var rmd = Math.floor(Math.random()*10);
      rmd = (rmd % 3) + 1;
      maxLine = rmd;
    }     
    else if ((oneS1 >= oneS2) && (oneS1 >= oneS3))
    {
      if (empty1 > 0){
        maxLine = 1;
      }
      else {
        if (oneS2 >= oneS3)
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else if (empty3 > 0)
          {
            maxLine = 3;
          }
        }
        if (oneS3 > oneS2)
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else if (empty2 > 0)
          {
            maxLine = 2;
          }
        }
      }
    }
    else if ((oneS2 >= oneS1) && (oneS2 >= oneS3))
    {
      if (empty2 > 0){
        maxLine = 2;
      }
      else {
        if (oneS1 >= oneS3)
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else if (empty3 > 0)
          {
            maxLine = 3;
          }
        }
        if (oneS3 > oneS1)
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else if (empty1 > 0)
          {
            maxLine = 1;
          }
        }
      }
    }    
    else if ((oneS3 >= oneS2) && (oneS3 >= oneS1))
    {
      if (empty3 > 0){
        maxLine = 3;
      }
      else {
        if (oneS1 >= oneS2)
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else if (empty2 > 0)
          {
            maxLine = 2;
          }
        }        
        if (oneS2 > oneS1)
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else if (empty1 > 0)
          {
            maxLine = 1;
          }
        }

      }
    }
    else
    {
      findEmpty()
      return;
    }
    
    
    if (maxLine == 1){
      for(i=0;i<7;i=i+3){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 2){
      for(i=2;i<7;i=i+2){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 3){
      for(i=6;i<9;i++){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    temp = "t" + (found + 1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
    ticBox[found] = 2;
    
}





function ClickedH()
{
  var i;
  var oneS1=0, twoS1=0;
  var oneS2=0, twoS2=0;
  var found=0;
  var temp="";
  var empty1=0; empty2=0;
  var winPrty=0;
  
    for(i=1;i<8;i=i+3){
      if (ticBox[i] == 1){
        oneS1++;
      }
      else if (ticBox[i] == 2){
        twoS1++;
      }
      else{
        empty1++;
      }
    }
    
    for(i=6;i<9;i++){
      if (ticBox[i] == 1){
        oneS2++;
      }
      else if (ticBox[i] == 2){
        twoS2++;
      }
      else {
        empty2++;
      }
    }

    //
    // take care of the situation when a computer can win 
    //
    

    //
    if ((vFirst == true) && (choicePlayer == "me"))
    {
      var rmd = Math.floor(Math.random()*10);
      rmd = (rmd % 2) + 1;
      maxLine = rmd;
    }     
    else if (oneS1 >= oneS2)
    {
      if (empty1 > 0){
        maxLine = 1;
      }
      else if (empty2 > 0){
        maxLine = 2;
      }
    }    
    else if (oneS2 > oneS1)
    {
      if (empty2 > 0){
        maxLine = 2;
      }
      else if (empty1 > 0){
        maxLine = 1;
      }
    }
    else
    {
      findEmpty();
      return;
    }
    
    
    //maxLine = findMax(oneS1, oneS2, 0);
    

    if (maxLine == 1){
      for(i=1;i<8;i=i+3){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 2){
      for(i=6;i<9;i++){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
        
    ticBox[found] = 2;
    temp = "t" + (found + 1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
    
}



function ClickedI()
{
  var i;
  var oneS1=0, twoS1=0;
  var oneS2=0, twoS2=0;
  var oneS3=0, twoS3=0;
  var found=0;
  var temp;
  var empty1=0, empty2=0, empty3=0;
  
    for(i=0;i<9;i=i+4){
      if (ticBox[i] == 1){
        oneS1++;
      }
      else if (ticBox[i] == 2){
        twoS1++;
      }
      else {
        empty1++;
      }
    }
    
    
    for(i=2;i<9;i=i+3){
      if (ticBox[i] == 1){
        oneS2++;
      }
      else if (ticBox[i] == 2){
        twoS2++;
      }
      else {
        empty2++;
      }
    }
    
    for(i=6;i<9;i++){
      if (ticBox[i] == 1){
        oneS3++;
      }
      else if (ticBox[i] == 2){
        twoS3++;
      }
      else {
        empty3++;
      }
    }
    
    //
    // if no one has won, proceed
    //
    //maxLine = findMax(oneS1, oneS2, oneS3);

    //
    if ((vFirst == true) && (choicePlayer == "me"))
    {
      var rmd = Math.floor(Math.random()*10);
      rmd = (rmd % 3) + 1;
      maxLine = rmd;
    }     
    else if ((oneS1 >= oneS2) && (oneS1 >= oneS3))
    {
      if (empty1 > 0){
        maxLine = 1;
      }
      else {
        if (oneS2 >= oneS3)
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else if (empty3 > 0)
          {
            maxLine = 3;
          }
        }
        if (oneS3 > oneS2)
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else if (empty2 > 0)
          {
            maxLine = 2;
          }
        }
      }
    }
    else if ((oneS2 >= oneS1) && (oneS2 >= oneS3))
    {
      if (empty2 > 0){
        maxLine = 2;
      }
      else {
        if (oneS1 >= oneS3)
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else if (empty3 > 0)
          {
            maxLine = 3;
          }
        }
        if (oneS3 > oneS1)
        {
          if (empty3 > 0)
          {
            maxLine = 3;
          }
          else if (empty1 > 0)
          {
            maxLine = 1;
          }
        }
      }
    }    
    else if ((oneS3 >= oneS2) && (oneS3 >= oneS1))
    {
      if (empty3 > 0){
        maxLine = 3;
      }
      else {
        if (oneS1 >= oneS2)
        {
          if (empty1 > 0)
          {
            maxLine = 1;
          }
          else if (empty2 > 0)
          {
            maxLine = 2;
          }
        }
        if (oneS2 > oneS1)
        {
          if (empty2 > 0)
          {
            maxLine = 2;
          }
          else if (empty1 > 0)
          {
            maxLine = 1;
          }
        }
      }
    }
    else
    {
      findEmpty()
      return;
    }
    
    
    if (maxLine == 1){
      for(i=0;i<9;i=i+4){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 2){
      for(i=2;i<9;i=i+3){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    if (maxLine == 3){
      for(i=6;i<9;i++){
        if (ticBox[i] == 0){
          found = i;
        }
      }
    }  
    
    temp = "t" + (found + 1);
    document.getElementById(temp).src = "../TicTacToe/Images/ticO.gif";
    ticBox[found] = 2;
    
    return;
    
}



function checkIfcanWin()
{
  var i;
  var twoS=0, emptyB=0;
  var completeLine = 0;
  
  for(i=0;i<3;i++)
  {
    if (ticBox[i] == 2)
    {  
      twoS++; 
    }
    if (ticBox[i] == 0)
    {
      emptyB++;
    }
  }
  
  if ((twoS == 2) && (emptyB == 1))
  {
    //complete 1st line
    completeLine = 1;
  }

  twoS = 0;
  emptyB = 0;
  
  //
  //  
  if (completeLine == 0)
  {
    for(i=3;i<6;i++)
    {
      if (ticBox[i] == 2)
      {  twoS++; }
      if (ticBox[i] == 0)
      {emptyB++;}
    }
  
    if ((twoS == 2) && (emptyB == 1))
    {
      //complete 1st line
      completeLine = 2;
    }
    twoS = 0;
    emptyB = 0;
  }
  

  //
  //
  
  if (completeLine == 0)
  {
    for(i=6;i<9;i++)
    {
      if (ticBox[i] == 2)
      {  twoS++; }
      if (ticBox[i] == 0)
      {emptyB++;}
    }
  
    if ((twoS == 2) && (emptyB == 1))
    {
      //complete 1st line
      completeLine = 3;
    }
    twoS = 0;
    emptyB = 0;  
  }
  
  //
  //
  if (completeLine == 0)
  {
    for(i=0;i<7;i=i+3)
    {
      if (ticBox[i] == 2)
      {  twoS++; }
      if (ticBox[i] == 0)
      {emptyB++;}
    }
  
    if ((twoS == 2) && (emptyB == 1))
    {
      //complete 1st line
      completeLine = 4;
    }
    twoS = 0;
    emptyB = 0;
  }
  
  //
  //
  if (completeLine == 0)  
  {
    for(i=1;i<8;i=i+3)
    {
      if (ticBox[i] == 2)
      {  twoS++; }
      if (ticBox[i] == 0)
      {emptyB++;}
    }
  
    if ((twoS == 2) && (emptyB == 1))
    {
      //complete 1st line
      completeLine = 5;
    }
    twoS = 0;
    emptyB = 0;
  }  
  //
  //
  if (completeLine == 0)  
  {
    for(i=2;i<9;i=i+3)
    {
      if (ticBox[i] == 2)
      {  twoS++; }
      if (ticBox[i] == 0)
      {emptyB++;}
    }
  
    if ((twoS == 2) && (emptyB == 1))
    {
      //complete 1st line
      completeLine = 6;
    }
    twoS = 0;
    emptyB = 0; 
  } 
  //
  //
  if (completeLine == 0)  
  {
    for(i=0;i<9;i=i+4)
    {
      if (ticBox[i] == 2)
      {  twoS++; }
      if (ticBox[i] == 0)
      {emptyB++;}
    }
  
    if ((twoS == 2) && (emptyB == 1))
    {
      //complete 1st line
      completeLine = 7;
    }
    twoS = 0;
    emptyB = 0;
  }
  //
  //
  if (completeLine == 0)
  {  
    for(i=2;i<7;i=i+2)
    {
      if (ticBox[i] == 2)
      {  twoS++; }
      if (ticBox[i] == 0)
      {emptyB++;}
    }
  
    if ((twoS == 2) && (emptyB == 1))
    {
      //complete 1st line
      completeLine = 8;
    }
    twoS = 0;
    emptyB = 0;
  }
  
  return completeLine;
    
}




function findMax(A, B, C)
{
  var a1=A, b1=B, c1=C;
  var maxV=0, rdm;
  var m1=0, m2=0, m3=0;
  
  
  if ((a1 == b1) && (b1 == c1))
  {
    maxV = a1;
  }
  else
  {
   // rdm = Math.floor(Math.random()*10);

    m1 = math.Max(a1, b1);
    m2 = math.Max(b1, c1);
    m3 = math.Max(a1, c1);
    
    if (m1 == m2)
    {
      maxV = m1; // b1 is max
    }
    else if (m2 > m1)
    {
      maxV = c1; 
    }
    else if (m1 > m2)
    {
      maxV = a1;
    }
      
  }
      
  return maxV;

}