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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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.

            
              <table>
    <tr>
        <td class = "cell" id = "0"></td>
        <td class = "cell" id = "1"></td>
        <td class = "cell" id = "2"></td>
    </tr>
    <tr>
        <td class = "cell" id = "3"></td>
        <td class = "cell" id = "4"></td>
        <td class = "cell" id = "5"></td>
    </tr>
    <tr>
        <td class = "cell" id = "6"></td>
        <td class = "cell" id = "7"></td>
        <td class = "cell" id = "8"></td>
    </tr>
</table>
<p>choose</p>
<div class = "guys">
  <div class= "dots">
  </div> 

  <div class= "dots2">
  </div>
</div>

            
          
!
            
              p {
  margin-left:-210px;
  position:absolute;
  font-size: 900%;
  width:410px;
  left:50%; 
}
.dots{
display:inline-block;
  background:white;
  height:100px;
  width:100px;
  float: left;
  border: 10px solid #333;
}
.dots2{
  display:inline-block;
  background:#333;
  height:100px;
  width:100px;
  float: right;
}

.guys{
  position:absolute;
  height:100px;
  width:350px;
  top:200px;
  left:50%;
  margin-left:-175px;
}

td {
border: 1px solid #333;
height: 100px;
width: 100px;
border-style: solid;
border-width: 15px;
visibility: hidden;
}

table{
  position:absolute;
  left:50%;
  margin-left:-150px;
  top:10%;
}
html{
  width:100%;
  height:100%;
  background:linear-gradient(#f7aeb7, #ffcccc, #ffeee6);
 
}
            
          
!
            
              $(document).ready(function() {
  $(".dots").click(function() {
    $(".guys, p").css("visibility", "hidden");
    $("td").css("visibility", "visible");
    aiCo = "#333";
    huCo = "white";
    console.log("white");
  });
  $(".dots2").click(function() {
    $(".guys, p").css("visibility", "hidden");
    $("td").css("visibility", "visible");
    console.log("black");
  });

  $("td").click(function() {
    move(this, huPlayer, huCo);
    console.log("clicked");
  });
});
var board = [0, 1, 2, 3, 4, 5, 6, 7, 8];
var huPlayer = "P";
var aiPlayer = "C";
var iter = 0;
var round = 0;
var aiCo = "white";
var huCo = "#333";

function move(element, player, color) {
  console.log("element"+ element.id);
  if (board[element.id] != "P" && board[element.id] != "C") {
    round++;
    $(element).css("background-color", color);
    board[element.id] = player;
    console.log(board);

    if (winning(board, player)) {
      setTimeout(function() {
        alert("YOU WIN");
        reset();
      }, 500);
      return;
    } else if (round > 8) {
      setTimeout(function() {
        alert("TIE");
        reset();
      }, 500);
      return;
    } else {
      round++;
      var index = minimax(board, aiPlayer).index;
      var selector = "#" + index;
      $(selector).css("background-color", aiCo);
      board[index] = aiPlayer;
      console.log(board);
      console.log(index);
      if (winning(board, aiPlayer)) {
        setTimeout(function() {
          alert("YOU LOSE");
          reset();
        }, 500);
        return;
      } else if (round === 0) {
        setTimeout(function() {
          alert("tie");
          reset();
        }, 500);
        return;
      }
    }
  }
}

function reset() {
  round = 0;
  board = [0, 1, 2, 3, 4, 5, 6, 7, 8];
  $("td").css("background-color", "transparent");
}

function minimax(reboard, player) {
  iter++;
  let array = avail(reboard);
  if (winning(reboard, huPlayer)) {
    return {
      score: -10
    };
  } else if (winning(reboard, aiPlayer)) {
    return {
      score: 10
    };
  } else if (array.length === 0) {
    return {
      score: 0
    };
  }

  var moves = [];
  for (var i = 0; i < array.length; i++) {
    var move = {};
    move.index = reboard[array[i]];
    reboard[array[i]] = player;

    if (player == aiPlayer) {
      var g = minimax(reboard, huPlayer);
      move.score = g.score;
    } else {
      var g = minimax(reboard, aiPlayer);
      move.score = g.score;
    }
    reboard[array[i]] = move.index;
    moves.push(move);
  }

  var bestMove;
  if (player === aiPlayer) {
    var bestScore = -10000;
    for (var i = 0; i < moves.length; i++) {
      if (moves[i].score > bestScore) {
        bestScore = moves[i].score;
        bestMove = i;
      }
    }
  } else {
    var bestScore = 10000;
    for (var i = 0; i < moves.length; i++) {
      if (moves[i].score < bestScore) {
        bestScore = moves[i].score;
        bestMove = i;
      }
    }
  }
  return moves[bestMove];
}

//available spots
function avail(reboard) {
  return reboard.filter(s => s != "P" && s != "C");
}

// winning combinations
function winning(board, player) {
  if (
    (board[0] == player && board[1] == player && board[2] == player) ||
    (board[3] == player && board[4] == player && board[5] == player) ||
    (board[6] == player && board[7] == player && board[8] == player) ||
    (board[0] == player && board[3] == player && board[6] == player) ||
    (board[1] == player && board[4] == player && board[7] == player) ||
    (board[2] == player && board[5] == player && board[8] == player) ||
    (board[0] == player && board[4] == player && board[8] == player) ||
    (board[2] == player && board[4] == player && board[6] == player)
  ) {
    return true;
  } else {
    return false;
  }
}
            
          
!
999px
Loading ..................

Console