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.

            
              <main>
    <section>
        <h2>Tic Tac Toe Game</h2>
        <table class="gameTable">
            <tr>
                <td id="a1"></td>
                <td id="a2"></td>
                <td id="a3"></td>
            </tr>
            <tr>
                <td id="b1"></td>
                <td id="b2"></td>
                <td id="b3"></td>
            </tr>
            <tr>
                <td id="c1"></td>
                <td id="c2"></td>
                <td id="c3"></td>
            </tr>
        </table>
        <footer class="hide"></footer>
    </section>
    <aside>
        <form>
            <label>Choose to play:</label><br>
            <input class="setSign" id="o" name="setSign" type="radio" value="o" autocomplete="off">
            <i class="fa fa-circle-o"></i>
            <input class="setSign" id="x" name="setSign" type="radio" value="x" autocomplete="off">
            <i class="fa fa-times"></i>
            <button type="button" class="hard-reset">
                <i id="refresh" class="fa fa-refresh fa-2x"></i>
            </button>
        </form>
    </aside>
</main>
            
          
!
            
              main {
    display: table;
    margin: 0 auto;
    font-family: 'Roboto', sans-serif;
    color: darkslateblue;
}

section {
    float: left;
}

section > h2 {
    text-align: center;
}

aside {
    float: left;
    margin: 186px 20px;
}

table {
    border-collapse: collapse;
    text-align: center;
    vertical-align: middle;
    border-style: hidden;
}

tr, td {
    border: 7px solid #444446;
    width: 100px;
    height: 100px;
    transition: background-color .2s;
    box-shadow: 10px 9px 166px 8px rgba(158, 158, 158, 1);
}

td:hover {
    background-color: #5b5b5b;
    cursor: pointer;
}

td.winnerLine {
    background-color: #5b5b5b;
    transition: opacity 3s linear;
    opacity: 0.5;
}

td > .fa-circle-o {
    color: #444446;
    font-size: 5em;
}

td > .fa-times {
    color: #B62434;
    font-size: 5em;
}

form > label {
    line-height: 150%;
    font-weight: bold;
}

form > .fa-circle-o {
    color: #5b5b5b;
    font-size: 1.8em;
}

form > .fa-times {
    color: #B62434;
    font-size: 2.1em;

}

button.hard-reset {
    border: 0;
    box-shadow: 0 0 0 1px #ccc;
    border-radius: 20%;
    margin-left: 10px;
}

#refresh {
    transition: all 0.5s ease;
}

footer {
    text-align: center;
    padding-top: 10px;
    font-weight: bold;
    font-size: 150%;
}

footer.show {

    opacity: 1;
    transition: opacity 1s linear;
}

footer.hide {
    opacity: 0;
    transition: opacity 1s linear;
}
            
          
!
            
              document.addEventListener("DOMContentLoaded", function() {
  var signPlayer, signAI;
  var signValue = document.getElementsByClassName("setSign");
  var signO = '<i class="fa fa-circle-o"></i>',
    signX = '<i class="fa fa-times"></i>';
  var footer = document.getElementsByTagName('footer')[0];
  for (i = 0; i < signValue.length; i++) {
    signValue[i].addEventListener("click", function() {
      signPlayer = this.value;
    });
  }
  var tableElem = document.getElementsByTagName("td");
  var board = ['e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'];

  var reset = document.getElementsByClassName('hard-reset')[0];
  reset.addEventListener('click', resetAll);

  for (var i = 0; i < tableElem.length; i++) {
    tableElem[i].addEventListener("click", function() {
      if ((signPlayer) && !(this.innerHTML)) {
        this.innerHTML = (signPlayer === "o") ? signO : signX;
        signAI = (signPlayer === 'x') ? 'o' : 'x';
        switch (this.id) {
          case "a1":
            board[0] = signPlayer;
            break;
          case "a2":
            board[1] = signPlayer;
            break;
          case "a3":
            board[2] = signPlayer;
            break;
          case "b1":
            board[3] = signPlayer;
            break;
          case "b2":
            board[4] = signPlayer;
            break;
          case "b3":
            board[5] = signPlayer;
            break;
          case "c1":
            board[6] = signPlayer;
            break;
          case "c2":
            board[7] = signPlayer;
            break;
          case "c3":
            board[8] = signPlayer;
            break;
        }
        makeMove();
      }
    })
  }

  function makeMove() {
    if (game.over(board)[0] === true) {
      footer.innerHTML = "DRAW!";
      footer.className = "show";
      restartGame();
      return;
    }
    var positionAiCoord = getMove();

    tableElem[positionAiCoord].innerHTML = (signAI === 'o') ? signO : signX;
    board[positionAiCoord] = signAI;
    if (game.over(board)[0] === "x") {
      var arr = game.over(board)[1];
      for (var i = 0; i < arr.length; i++) {
        tableElem[arr[i]].className = "winnerLine";
      }
      footer.innerHTML = "The Winner is X";
      footer.className = "show";
      restartGame();
    } else if (game.over(board)[0] === "o") {
      footer.innerHTML = "The Winner is O";
      footer.className = "show";
      restartGame();
    }
  }

  function hideFooter() {
    for (i = 0; i < tableElem.length; i++) {
      tableElem[i].innerHTML = "";
      tableElem[i].className = "";
    }
    footer.className = "hide";
  }

  function restartGame() {
    board = ['e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'];
    setTimeout(hideFooter, 750);

  }

  function resetAll() {

    board = ['e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e'];
    hideFooter();
    var refresh = document.getElementById("refresh");
    refresh.classList.add("fa-spin");

    setTimeout(function () {
        stopSpinner();
    }, 1000);
    function stopSpinner() {
        refresh.classList.remove("fa-spin");
    }

  }

  var game = {
    over: function(board) {

      for (var i = 0; i < board.length; i += 3) {
        if ((board[i] === board[i + 1] && board[i + 1] === board[i + 2]) &&
          (board[i] !== 'e')) {
          return [board[i],
            [i, i + 1, i + 2]
          ];
        }
      }
      for (var j = 0; j < 3; j++) {
        if ((board[j] === board[j + 3] && board[j + 3] === board[j + 6]) &&
          (board[j] !== 'e')) {
          return [board[j],
            [j, j + 3, j + 6]
          ];
        }
      }

      if ((board[4] === board[0] && board[4] === board[8]) &&
        (board[4] !== 'e')) {
        return [board[4],
          [0, 4, 8]
        ];
      }
      if ((board[4] === board[2] && board[4] === board[6]) &&
        (board[4] !== 'e')) {
        return [board[4],
          [2, 4, 6]
        ];
      }
      return [board.every(function(element) {
        return element !== 'e';
      })];
    },
    possible_moves: function(board, sign) {
      var testBoard = [],
        nextBoard;
      for (var i = 0; i < board.length; i++) {
        nextBoard = board.slice();
        if (nextBoard[i] === 'e') {
          nextBoard[i] = sign;
          testBoard.push(nextBoard);
        }
      }
      return testBoard;
    }
  };

  function moveScore(board) {
    var result = game.over(board)[0];
    if (result === signPlayer) {
      return -100;
    }
    if (result === signAI) {
      return +100;
    }
    return 0; //Game is a draw
  }

  function max(board) {

    if (game.over(board)[0]) {
      return [moveScore(board), []];
    }
    var newGame = [];
    var bestMove = [];
    var score;
    var best_score = -Infinity;
    var movesArray = game.possible_moves(board, signAI);

    for (var i = 0; i < movesArray.length; i++) {
      newGame = movesArray[i].slice();
      score = min(newGame)[0];
      if (score > best_score) {
        best_score = score;
        bestMove = newGame;
      }
    }
    return [best_score, bestMove];
  }

  function min(board) {

    if (game.over(board)[0]) {
      return [moveScore(board), []];
    }
    var newGame = [];
    var worstMove = [];
    var score;
    var worst_score = +Infinity;
    var movesArray = game.possible_moves(board, signPlayer);

    for (var i = 0; i < movesArray.length; i++) {
      newGame = movesArray[i].slice();
      score = max(newGame)[0];
      if (score < worst_score) {
        worst_score = score;
        worstMove = newGame;
      }
    }
    return [worst_score, worstMove];
  }

  function getMove() {
    var getBestMove = max(board)[1];
    for (var i = 0; i < getBestMove.length; i++) {
      if (getBestMove[i] !== board[i]) {
        return i;
      }
    }
  }
});
            
          
!
999px
Loading ..................

Console