cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <head>
  <title>Tic Tac Toe</title>    
</head>

<body onload="reset();">
  <div class="container text-center" id="controlpanel">
    <div id="gameState">Who will play first?</div><br>
    <span id= "charset" class="row">     
    <button id="xbutton" class="controls" onclick="useX()">First player has X</button>
    <button id="obutton" class="controls" onclick="useO()">First player has O</button>   
    </span>
    <span id= "maskset" class="row">     
    <button id="maskbutton1" class="controls" onclick="useX()">&nbsp</button>
    <button id="maskbutton2" class="controls" onclick="useO()">&nbsp</button>   
    </span>
     <span id= "aiset" class="row">     
    <button id="randbutton" class="controls" onclick="randAI()">Stupid</button>
    <button id="novbutton" class="controls" onclick="noviceAI()">Not so stupid</button>
    <button id="masbutton" class="controls" onclick="masterAI()">Clever</button>  
    </span>
    <span id= "playset" class="row">     
    <button id="0button" class="controls" onclick="zeroPlayers()">Zero</button>
    <button id="1button" class="controls" onclick="onePlayer()">One</button>
    <button id="2button" class="controls" onclick="twoPlayers()">Two</button>  
    </span>
    <span id= "res" class="row">     
    <button id="reset" class="controls" onclick="reset()">Reset</button>
    </span>
    </div>
     <table align="center">
    <tr>
        <td><button class="square" id="s1" onclick="one()">&nbsp</button></td>
        <td><button class="square" id="s2" onclick="two()">&nbsp</button></td>
        <td><button class="square" id="s3" onclick="three()">&nbsp</button></td>
    </tr>
    <tr>
        <td><button class="square" id="s4" onclick="four()">&nbsp</button></td>
        <td><button class="square" id="s5" onclick="five()">&nbsp</button></td>
        <td><button class="square" id="s6" onclick="six()">&nbsp</button></td>
    </tr>
    <tr>
        <td><button class="square" id="s7" onclick="seven()">&nbsp</button></td>
        <td><button class="square" id="s8" onclick="eight()">&nbsp</button></td>
        <td><button class="square" id="s9"onclick="nine()">&nbsp</button></td>
    </tr>
                    <span id= "res" class="row">     
    <button id="reset" class="controls" onclick="reset()">Reset</button>
    </span>  
</table>
  </div>
    <footer class="text-center">
    Made by <a href="https://www.freecodecamp.com/radekjohn" target="_blank">Radek John</a>.
  </footer>
</body>
            
          
!
            
              body{
  background-color: #4464AD;
  color: white;
  margin: 0px;
  padding: 0px;
}

a {
  color: white;
  text-decoration: underline;
}

a:hover {
  color: white;
  background: black;
  text-decoration: underline;
}

table { 
    border-collapse: collapse;
    border-style: hidden;
    font-size: 250%;
}

table td, table th {
    border: 5px solid white;
}

td {
height: 150px;
width: 150px;
}

button {
  background-color: #ffffff;
  font-size: 150%;  
}

table button {
    background: none;
    border: none;
    width: 100%;
    color: white;
}
button:focus {
    outline: 0;
}
#tests{
  font-size: 20%;
}

.container{
  font-size: 300%;
}

.controls{
font-size: 30%;
}

#controlpanel {
height: 20%;  
display:block;
height:30%;
margin-bottom: 1em;  
}

#controlpanel button{
border: 0;
padding: 2em;
background-color: #B2C9FF;
color: #082213;   
}

#charset{
display: unset;
}
#maskset{
display: none;
}
#maskset button{
background-color: #4464AD;  
display: none;
}
#aiset{
display: none;
}
#playset{
display: none;
}
#WinnerTest{
  display: none;
}
#reset{
  margin: 0 auto;
  display: block;
  display: none;
}
#maskbutton{
  margin: 0 auto;
  display: block;
  display: none;
}
            
          
!
            
              var players = 1;
var clicks = 0; // Counts clicks on tic-tac-toe squares
var board = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var freeSquares = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // Stores free squares
var char = ""; // Character used in particular turn – X or O
var rand = 0; // First random number
var rand1 = 0; // Second random number
// Characters used by first and second player
var firstChar = "X";
var secondChar = "◯";
// Squares owned by players
var playerOne = [];
var playerTwo = [];
// Victory state
var victory = false;
var winner = "none";
var AiLevel = 0;
// Minimax parameters
var score = 1000;
var scoreArr = [];

// Set number of players
function zeroPlayers() {
    players = 0;
    getChar();
    document.getElementById("playset").style.display = "none";
    document.getElementById("gameState").innerHTML = "Select AI level:";
    document.getElementById("aiset").style.display = "unset";
}

function onePlayer() {
    players = 1;
    document.getElementById("playset").style.display = "none";
    document.getElementById("gameState").innerHTML = "Game on.";
    document.getElementById("playset").style.display = "none";
    document.getElementById("gameState").innerHTML = "Select AI level:";
    document.getElementById("aiset").style.display = "unset";
}

function twoPlayers() {
    getChar();
    players = 2;
    prepareBoard();
    document.getElementById("playset").style.display = "none";
    document.getElementById("maskset").style.display = "unset";
    document.getElementById("gameState").innerHTML = "Game on.";
}
// Set AI level
function randAI() {
    AiLevel = 0;
    document.getElementById("gameState").innerHTML = "Game on.";
    getChar();
    order();
    prepareBoard();
    if (players == 1) {
        if (rand < 5) {
            computerTurn();
        }
    } else {
        computerTurn();
    }
    document.getElementById("aiset").style.display = "none";
    document.getElementById("maskset").style.display = "unset";
}

function noviceAI() {
    AiLevel = 1;
    prepareBoard();
    order();
    if (players == 1) {
        if (rand < 5) {
            computerTurn();
        }
    } else {
        computerTurn();
    }
    document.getElementById("aiset").style.display = "none";
    document.getElementById("maskset").style.display = "unset";
    document.getElementById("gameState").innerHTML = "Game on.";
}

function masterAI() {
    AiLevel = 2;
    prepareBoard();
    order();
    if (players == 1) {
        if (rand < 5) {
            computerTurn();
        }
    } else {
        computerTurn();
    }
    document.getElementById("aiset").style.display = "none";
    document.getElementById("maskset").style.display = "unset";
    document.getElementById("gameState").innerHTML = "Game on.";
}

// Set playing order
function order() {
    rand = Math.floor(Math.random() * 10);
}
// Generates random number
function dice() {
    rand1 = Math.floor(Math.random() * 10);
}

// Set character preferences
function useX() {
    firstChar = "X";
    secondChar = "◯";
    controlsSwitch();
    document.getElementById("charset").style.display = "none";
    document.getElementById("playset").style.display = "unset";
    document.getElementById("gameState").innerHTML = "Select the number of players:";
}

function useO() {
    firstChar = "◯";
    secondChar = "X";
    controlsSwitch();
    document.getElementById("charset").style.display = "none";
    document.getElementById("playset").style.display = "unset";
    document.getElementById("gameState").innerHTML = "Select the number of players:";
}

// Use X or O based on odd or even turn
function getChar() {
    if (clicks % 2 !== 0) {
        char = firstChar;
    } else {
        char = secondChar;
    }
}
// Removes occupied square from the list of free squares
function remove(val) {
    var index = freeSquares.indexOf(val);
    if (index > -1) {
        freeSquares.splice(index, 1);
    }
}
// Saves squares occupied by each player
function store(val) {
    if (clicks % 2 !== 0) {
        playerOne.push(val);
    } else {
        playerTwo.push(val);
    }
}
// Turn of controls
function controlsSwitch() {
    document.getElementById("xbutton").disabled = true;
    document.getElementById("obutton").disabled = true;
}
// Turn off all squares
function masterSwitch() {
    document.getElementById("s1").disabled = true;
    document.getElementById("s2").disabled = true;
    document.getElementById("s3").disabled = true;
    document.getElementById("s4").disabled = true;
    document.getElementById("s5").disabled = true;
    document.getElementById("s6").disabled = true;
    document.getElementById("s7").disabled = true;
    document.getElementById("s8").disabled = true;
    document.getElementById("s9").disabled = true;
}

function prepareBoard() {
    document.getElementById("s1").disabled = false;
    document.getElementById("s2").disabled = false;
    document.getElementById("s3").disabled = false;
    document.getElementById("s4").disabled = false;
    document.getElementById("s5").disabled = false;
    document.getElementById("s6").disabled = false;
    document.getElementById("s7").disabled = false;
    document.getElementById("s8").disabled = false;
    document.getElementById("s9").disabled = false;
}

function AI() {
    // First turn  
    if (freeSquares.length === 9) {
        clicker(5);
    } else if (freeSquares.length === 8 && freeSquares.indexOf(5) > -1) {
        clicker(5);
        // Second turn
    } else if (freeSquares.length === 8 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length === 8) {
        randomMove();
    }
    // Third turn  
    else if (freeSquares.length == 7 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length == 7 && playerOne.indexOf(1) > -1) {
        dice();
        if (playerTwo.IndexOf(2) > -1) {
            clicker(4);
        } else if (playerTwo.IndexOf(4) > -1) {
            clicker(2);
        } else if (rand1 < 5) {
            clicker(4);
        } else {
            clicker(2);
        }
    } else if (freeSquares.length == 7 && playerOne.indexOf(2) > -1) {
        dice();
        if (playerTwo.IndexOf(3) > -1) {
            clicker(1);
        } else if (playerTwo.IndexOf(1) > -1) {
            clicker(3);
        } else if (rand1 < 5) {
            clicker(1);
        } else {
            clicker(3);
        }
    } else if (freeSquares.length == 7 && playerOne.indexOf(3) > -1) {
        dice();
        if (playerTwo.IndexOf(6) > -1) {
            clicker(2);
        } else if (playerTwo.IndexOf(2) > -1) {
            clicker(6);
        } else if (rand1 < 5) {
            clicker(6);
        } else {
            clicker(2);
        }
    } else if (freeSquares.length == 7 && playerOne.indexOf(4) > -1) {
        dice();
        if (playerTwo.IndexOf(1) > -1) {
            clicker(7);
        } else if (playerTwo.IndexOf(7) > -1) {
            clicker(1);
        } else if (rand1 < 5) {
            clicker(7);
        } else {
            clicker(1);
        }
    } else if (freeSquares.length == 7 && playerOne.indexOf(5) > -1) {
        randomMove();
    } else if (freeSquares.length == 7 && playerOne.indexOf(6) > -1) {
        dice();
        if (playerTwo.IndexOf(3) > -1) {
            clicker(9);
        } else if (playerTwo.IndexOf(9) > -1) {
            clicker(3);
        } else if (rand1 < 5) {
            clicker(9);
        } else {
            clicker(3);
        }
    } else if (freeSquares.length == 7 && playerOne.indexOf(7) > -1) {
        dice();
        if (playerTwo.IndexOf(8) > -1) {
            clicker(4);
        } else if (playerTwo.IndexOf(4) > -1) {
            clicker(7);
        } else if (rand1 < 5) {
            clicker(4);
        } else {
            clicker(7);
        }
    } else if (freeSquares.length == 7 && playerOne.indexOf(8) > -1) {
        dice();
        if (playerTwo.IndexOf(9) > -1) {
            clicker(7);
        } else if (playerTwo.IndexOf(7) > -1) {
            clicker(9);
        } else if (rand1 < 5) {
            clicker(9);
        } else {
            clicker(7);
        }
    } else if (freeSquares.length == 7 && playerOne.indexOf(9) > -1) {
        dice();
        if (playerTwo.IndexOf(6) > -1) {
            clicker(8);
        } else if (playerTwo.IndexOf(8) > -1) {
            clicker(6);
        } else if (rand1 < 5) {
            clicker(8);
        } else {
            clicker(6);
        }
    }
    // Hopeless situation
    else if (freeSquares.length == 7) {
        randomMove();
    }
    // Fourth turn 
    //Rows
    // First row
    else if (freeSquares.length == 6 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(2) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 6 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 6 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    }
    // Second row
    else if (freeSquares.length == 6 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    } else if (freeSquares.length == 6 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    } else if (freeSquares.length == 6 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third row
    else if (freeSquares.length == 6 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 6 && playerOne.indexOf(8) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 6 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    }

    // Columns
    // First column
    else if (freeSquares.length == 6 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(4) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 6 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(7) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 6 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(7) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    }
    // Second column
    else if (freeSquares.length == 6 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    } else if (freeSquares.length == 6 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    } else if (freeSquares.length == 6 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third column
    else if (freeSquares.length == 6 && playerOne.indexOf(3) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 6 && playerOne.indexOf(6) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 6 && playerOne.indexOf(3) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    }
    // Hopeless situation
    else if (freeSquares.length == 6) {
        randomMove();
    }

    // Diagonals
    else if (freeSquares.length == 6 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 6 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 6 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length == 6 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 6 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 6 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Fifth turn
    // Player One attack
    // Rows
    // First row
    else if (freeSquares.length == 5 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(2) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 5 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 5 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    }
    // Second row
    else if (freeSquares.length == 5 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    } else if (freeSquares.length == 5 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    } else if (freeSquares.length == 5 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third row
    else if (freeSquares.length == 5 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 5 && playerOne.indexOf(8) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 5 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    }

    // Columns
    // First column
    else if (freeSquares.length == 5 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(4) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 5 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(7) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 5 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(7) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    }
    // Second column
    else if (freeSquares.length == 5 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    } else if (freeSquares.length == 5 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    } else if (freeSquares.length == 5 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third column
    else if (freeSquares.length == 5 && playerOne.indexOf(3) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 5 && playerOne.indexOf(6) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 5 && playerOne.indexOf(3) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    }

    // Diagonals
    else if (freeSquares.length == 5 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 5 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 5 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length == 5 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 5 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 5 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Player One Defence   
    //Rows
    // First row
    else if (freeSquares.length == 5 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(2) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    }
    // Second row
    else if (freeSquares.length == 5 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third row
    else if (freeSquares.length == 5 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(8) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    }

    // Columns
    // First column
    else if (freeSquares.length == 5 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(4) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(7) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(7) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    }
    // Second column
    else if (freeSquares.length == 5 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third column
    else if (freeSquares.length == 5 && playerTwo.indexOf(3) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(6) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(3) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    }

    // Diagonals
    else if (freeSquares.length == 5 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 5 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Hopeless situation
    else if (freeSquares.length == 5) {
        randomMove();
    }

    // Sixthth turn
    // Player Two attack
    // Rows
    // First row
    else if (freeSquares.length == 4 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(2) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    }
    // Second row
    else if (freeSquares.length == 4 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third row
    else if (freeSquares.length == 4 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(8) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    }

    // Columns
    // First column
    else if (freeSquares.length == 4 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(4) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(7) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(7) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    }
    // Second column
    else if (freeSquares.length == 4 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third column
    else if (freeSquares.length == 4 && playerTwo.indexOf(3) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(6) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(3) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    }

    // Diagonals
    else if (freeSquares.length == 4 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 4 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Player Two defence
    // Rows
    // First row
    else if (freeSquares.length == 4 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(2) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 4 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 4 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    }
    // Second row
    else if (freeSquares.length == 4 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    } else if (freeSquares.length == 4 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    } else if (freeSquares.length == 4 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third row
    else if (freeSquares.length == 4 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 4 && playerOne.indexOf(8) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 4 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    }

    // Columns
    // First column
    else if (freeSquares.length == 4 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(4) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 4 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(7) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 4 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(7) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    }
    // Second column
    else if (freeSquares.length == 4 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    } else if (freeSquares.length == 4 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    } else if (freeSquares.length == 4 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third column
    else if (freeSquares.length == 4 && playerOne.indexOf(3) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 4 && playerOne.indexOf(6) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 4 && playerOne.indexOf(3) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    }

    // Diagonals
    else if (freeSquares.length == 4 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 4 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 4 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length == 4 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 4 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 4 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Hopeless situation
    else if (freeSquares.length == 4) {
        randomMove();
    }

    // Seventhth turn
    // Player One attack
    // Rows
    // First row
    else if (freeSquares.length == 3 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(2) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 3 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 3 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    }
    // Second row
    else if (freeSquares.length == 3 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    } else if (freeSquares.length == 3 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    } else if (freeSquares.length == 3 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third row
    else if (freeSquares.length == 3 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 3 && playerOne.indexOf(8) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 3 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    }

    // Columns
    // First column
    else if (freeSquares.length == 3 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(4) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 3 && playerOne.indexOf(4) !== -1 && playerOne.indexOf(7) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 3 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(7) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    }
    // Second column
    else if (freeSquares.length == 3 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    } else if (freeSquares.length == 3 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    } else if (freeSquares.length == 3 && playerOne.indexOf(2) !== -1 && playerOne.indexOf(8) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third column
    else if (freeSquares.length == 3 && playerOne.indexOf(3) !== -1 && playerOne.indexOf(6) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 3 && playerOne.indexOf(6) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 3 && playerOne.indexOf(3) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    }

    // Diagonals
    else if (freeSquares.length == 3 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 3 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 3 && playerOne.indexOf(1) !== -1 && playerOne.indexOf(9) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length == 3 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(5) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 3 && playerOne.indexOf(5) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 3 && playerOne.indexOf(7) !== -1 && playerOne.indexOf(3) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Player One defence
    // Rows
    // First row
    else if (freeSquares.length == 3 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(2) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    }
    // Second row
    else if (freeSquares.length == 3 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third row
    else if (freeSquares.length == 3 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(8) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    }

    // Columns
    // First column
    else if (freeSquares.length == 3 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(4) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(7) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(7) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    }
    // Second column
    else if (freeSquares.length == 3 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third column
    else if (freeSquares.length == 3 && playerTwo.indexOf(3) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(6) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(3) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    }

    // Diagonals
    else if (freeSquares.length == 3 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 3 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Hopeless situation
    else if (freeSquares.length == 3) {
        randomMove();
    }

    // Eignth turn
    // Player Two attack
    // Rows
    // First row
    else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(2) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    }
    // Second row
    else if (freeSquares.length == 2 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third row
    else if (freeSquares.length == 2 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(8) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    }

    // Columns
    // First column
    else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(4) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(7) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(7) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    }
    // Second column
    else if (freeSquares.length == 2 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third column
    else if (freeSquares.length == 2 && playerTwo.indexOf(3) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(6) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(3) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    }

    // Diagonals
    else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }

    // Player Two defence
    // Rows
    // First row
    else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(2) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    }
    // Second row
    else if (freeSquares.length == 2 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third row
    else if (freeSquares.length == 2 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(8) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    }

    // Columns
    // First column
    else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(4) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(4) !== -1 && playerTwo.indexOf(7) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(7) !== -1 && freeSquares.indexOf(4) > -1) {
        clicker(4);
    }
    // Second column
    else if (freeSquares.length == 2 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(8) > -1) {
        clicker(8);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(2) > -1) {
        clicker(2);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(2) !== -1 && playerTwo.indexOf(8) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Third column
    else if (freeSquares.length == 2 && playerTwo.indexOf(3) !== -1 && playerTwo.indexOf(6) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(6) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(3) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(6) > -1) {
        clicker(6);
    }

    // Diagonals
    else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(9) > -1) {
        clicker(9);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(1) > -1) {
        clicker(1);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(1) !== -1 && playerTwo.indexOf(9) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(5) !== -1 && freeSquares.indexOf(3) > -1) {
        clicker(3);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(5) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(7) > -1) {
        clicker(7);
    } else if (freeSquares.length == 2 && playerTwo.indexOf(7) !== -1 && playerTwo.indexOf(3) !== -1 && freeSquares.indexOf(5) > -1) {
        clicker(5);
    }
    // Hopeless situation
    else if (freeSquares.length == 2) {
        randomMove();
    }
    // Ninth
    else if (freeSquares.length == 1) {
        randomMove();
    }
    // Belongs to AI()
}

// Computer moves by game difficulty (= AI level)
function randomMove() {
    if (freeSquares.length > 0) {
        clicker(freeSquares[Math.floor(Math.random() * freeSquares.length)]);
    }
}

function noviceMove() {
    dice();
    if (rand1 < 5) {
        randomMove();
    } else {
        AI();
    }
}

function masterMove() {
    AI();
}

// Computer turn
function computerTurn() {
    if (AiLevel === 0) {
        randomMove();
    } else if (AiLevel === 0) {
        noviceMove();
    } else {
        masterMove();
    }
}

// Do stuff after click on a square
function clicker(square) {
    clicks++;
    controlsSwitch();
    getChar();
    board[square - 1] = char;
    remove(square);
    store(square);
    testVictory();
    findWinner();
    (document.getElementById(
        "s" + square
    ).innerHTML = char), (document.getElementById("s" + square).disabled = true);
    if (players === 0 && victory === false) {
        computerTurn();
    }
    if (players === 1 && victory === false) {
        if (rand < 5) {
            if (clicks % 2 === 0) {
                computerTurn();
            }
        } else {
            if (clicks % 2 !== 0) {
                computerTurn();
            }
        }
    }
    if (victory === true) {
        masterSwitch();
    }
}
// Checks victory
function testVictory() {
    // Check for rows
    if (
        playerOne.indexOf(1) !== -1 &&
        playerOne.indexOf(2) !== -1 &&
        playerOne.indexOf(3) !== -1
    ) {
        victory = true;
    }
    if (
        playerOne.indexOf(4) !== -1 &&
        playerOne.indexOf(5) !== -1 &&
        playerOne.indexOf(6) !== -1
    ) {
        victory = true;
    }
    if (
        playerOne.indexOf(7) !== -1 &&
        playerOne.indexOf(8) !== -1 &&
        playerOne.indexOf(9) !== -1
    ) {
        victory = true;
    }
    if (
        playerTwo.indexOf(1) !== -1 &&
        playerTwo.indexOf(2) !== -1 &&
        playerTwo.indexOf(3) !== -1
    ) {
        victory = true;
    }
    if (
        playerTwo.indexOf(4) !== -1 &&
        playerTwo.indexOf(5) !== -1 &&
        playerTwo.indexOf(6) !== -1
    ) {
        victory = true;
    }
    if (
        playerTwo.indexOf(7) !== -1 &&
        playerTwo.indexOf(8) !== -1 &&
        playerTwo.indexOf(9) !== -1
    ) {
        victory = true;
    }

    // Check for rows
    if (
        playerOne.indexOf(1) !== -1 &&
        playerOne.indexOf(4) !== -1 &&
        playerOne.indexOf(7) !== -1
    ) {
        victory = true;
    }
    if (
        playerOne.indexOf(2) !== -1 &&
        playerOne.indexOf(5) !== -1 &&
        playerOne.indexOf(8) !== -1
    ) {
        victory = true;
    }
    if (
        playerOne.indexOf(3) !== -1 &&
        playerOne.indexOf(6) !== -1 &&
        playerOne.indexOf(9) !== -1
    ) {
        victory = true;
    }
    if (
        playerTwo.indexOf(1) !== -1 &&
        playerTwo.indexOf(4) !== -1 &&
        playerTwo.indexOf(7) !== -1
    ) {
        victory = true;
    }
    if (
        playerTwo.indexOf(2) !== -1 &&
        playerTwo.indexOf(5) !== -1 &&
        playerTwo.indexOf(8) !== -1
    ) {
        victory = true;
    }
    if (
        playerTwo.indexOf(3) !== -1 &&
        playerTwo.indexOf(6) !== -1 &&
        playerTwo.indexOf(9) !== -1
    ) {
        victory = true;
    }

    // Check for diagonals
    if (
        playerOne.indexOf(1) !== -1 &&
        playerOne.indexOf(5) !== -1 &&
        playerOne.indexOf(9) !== -1
    ) {
        victory = true;
    }
    if (
        playerOne.indexOf(7) !== -1 &&
        playerOne.indexOf(5) !== -1 &&
        playerOne.indexOf(3) !== -1
    ) {
        victory = true;
    }
    if (
        playerTwo.indexOf(1) !== -1 &&
        playerTwo.indexOf(5) !== -1 &&
        playerTwo.indexOf(9) !== -1
    ) {
        victory = true;
    }
    if (
        playerTwo.indexOf(7) !== -1 &&
        playerTwo.indexOf(5) !== -1 &&
        playerTwo.indexOf(3) !== -1
    ) {
        victory = true;
    }
}
// Determines who made the winning move
function findWinner() {
    if (victory === true && clicks % 2 !== 0) {
        winner = playerOne;
        document.getElementById("gameState").innerHTML =
            "Player One " + "(" + char + ")" + " won.";
        document.getElementById("maskset").style.display = "none";
        document.getElementById("reset").style.display = "unset";
    } else if (victory === true && clicks % 2 === 0) {
        winner = playerTwo;
        document.getElementById("gameState").innerHTML =
            "Player Two " + "(" + char + ")" + " won.";
        document.getElementById("maskset").style.display = "none";
        document.getElementById("reset").style.display = "unset";
    } else {
        winner = "none";
        if (freeSquares.length == 0) {
            document.getElementById("maskset").style.display = "none";
            document.getElementById("reset").style.display = "unset";
            document.getElementById("gameState").style.display = "unset";
            document.getElementById("gameState").innerHTML = "It's a draw.";
        }
    }
}
// On click functions for every square
function one() {
    clicker(1);
}

function two() {
    clicker(2);
}

function three() {
    clicker(3);
}

function four() {
    clicker(4);
}

function five() {
    clicker(5);
}

function six() {
    clicker(6);
}

function seven() {
    clicker(7);
}

function eight() {
    clicker(8);
}

function nine() {
    clicker(9);
}

function reset() {
    players = -1;
    clicks = 0;
    freeSquares = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    char = "";
    rand = 0;
    rand1 = 0;
    firstChar = "X";
    secondChar = "◯";
    playerOne = [];
    playerTwo = [];
    vic = 0;
    victory = false;
    AiLevel = 0;
    score = 1000;
    board = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    masterSwitch();



    document.getElementById("s1").innerHTML = "&nbsp";
    document.getElementById("s2").innerHTML = "&nbsp";
    document.getElementById("s3").innerHTML = "&nbsp";
    document.getElementById("s4").innerHTML = "&nbsp";
    document.getElementById("s5").innerHTML = "&nbsp";
    document.getElementById("s6").innerHTML = "&nbsp";
    document.getElementById("s7").innerHTML = "&nbsp";
    document.getElementById("s8").innerHTML = "&nbsp";
    document.getElementById("s9").innerHTML = "&nbsp";

    document.getElementById("xbutton").disabled = false;
    document.getElementById("obutton").disabled = false;

    document.getElementById("aiset").style.display = "none";
    document.getElementById("playset").style.display = "none";
    document.getElementById("reset").style.display = "none";
    document.getElementById("gameState").style.display = "unset";
    document.getElementById("gameState").innerHTML = "Who will play first?";
    document.getElementById("charset").style.display = "unset";
}
            
          
!
999px
Loading ..................

Console