Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container text-center">
  <div class="startgame">

    <h3>Pick your marker :
      </br>
  <a class="btn " id="turnx">X</a> 
  <a class="btn " id="turno">0</a>
    </h3>

  </div>
  <div class="game">
    <h3 class="title">TIC-TAC-TOE</h3>


    <div class="row">
     
      <table>
          <tr>
            <td><a class="tic" id="0"></a></td>
            <td class="v"><a class="tic" id="1"></a></td>
            <td><a class="tic" id="2"></a></td>
          </tr>
          <tr>
            <td class="h"><a class="tic" id="3"></a></td>
            <td class="h v"><a class="tic" id="4"></a></td>
            <td class="h"><a class="tic" id="5"></a></td>

          </tr>
          <tr>

            <td><a class="tic" id="6"></a></td>
            <td class="v"><a class="tic" id="7"></a></td>
            <td><a class="tic" id="8"></a></td>

          </tr>
        </table>
     
    </div>
  </div>
<div class="score">
  <span class="results">
    <i class="fa fa-user person" aria-hidden="true"></i>
    <h2 class="human">0</h2>
    <i class="fa fa-ban" aria-hidden="true"></i>
    <h2 class="draw">0</h2>
    <i class="fa fa-laptop" aria-hidden="true"></i>
    <h2 class="computer">0</h2>
    <a class="reset"><i class="fa fa-refresh" aria-hidden="true"></i></a>
    </span>
  </div>
  <div class="endgame">
    <h3 class="text"></h3>
    <a class="btn replay">Play Again</a>
  </div>
</div>
              
            
!

CSS

              
                body {
  background-color: #f0d41f;
  font-family: "Ubuntu", sans-serif;
  font-size: 20pt;
}

.startgame {
  border:5px solid black;
  border-radius:25px;
  position:absolute;
  background-color: rgba(240, 212, 31, 1);
  z-index: 99 !important;
  width:350px;
  top:330px;
  left:50%;
  transform:translate(-50%,-50%);
}


h3 {
  padding-top: 30px;
  font-weight: bold;
  margin: 10px;
}

h3 .btn {
  color: black;
  font-size: 25pt;
  margin: 10px;
  margin-bottom: 30px;
  font-weight:bold;
}

.title {
  font-size: 45pt;
  margin:40px 20px 60px 20px;
}

h3 .btn:hover {
  color: purple;
}

td {
  background-color: #f0d41f;
  width: 7.2vw;
  height: 7.2vw;
}
td a{
  font-size: 36pt !important;
  color: black;
  letter-spacing: 4px;
  cursor:pointer;
  padding:20px;
}
td a:hover {
  color:black;
  text-decoration: none;
  cursor:pointer;
}

table {
  margin: auto;
  align: center;
  border-color: black;
}

.v {
  border-left: 7px solid black;
  border-right: 7px solid black;
}
.h {
  border-top: 7px solid black;
  border-bottom: 7px solid black;
}
.tic {
  
}

.score{
  margin:60px 20px 20px 40px;
}

span{
  margin-top:20px;
}

i,h2{
  display:inline;
  margin:10px;
}
.human{
  padding-right:30px;
}
.fa-laptop{
  padding-left:30px;
}
.computer{
  padding-right:30px;
}
.reset{
  color:black;
}
.reset:hover{
  color:purple;
}
.endgame {
  border:5px solid black;
  border-radius:25px;
  width:350px;
  top:330px;
  background-color: rgba(240, 212, 31, 0.9);
  position:absolute;
  left:50%;
  transform:translate(-50%,-50%);
  display:none;
}

.replay{
  font-size:25pt;
  border: 3px solid black;
  border-radius:25px;
  color: black;
  font-size: 16pt;
  margin: 10px;
  margin-bottom: 30px;
}

.replay:hover{
  border: 3px solid purple;
  background-color: purple;
  color: #f0d41f;
}
              
            
!

JS

              
                $(document).ready(function() {
  //default player's turn to X
  var turn = "X";
  var turnWon = 0;
  //array stores values will checklater for a winner
  var turns = [0, 1, 2, 3, 4, 5, 6, 7, 8];
  //default computer's turn
  var computer = "O";
  var computerWon = 0;
  var draw=0;
  //keeps track of comuters turn so no loop
  var fc = 0;
  var checkDraw = [];
  var gameOn = false;
  //Change player turn to x and computer to o
  $("#turnx").click(function() {
    $(".startgame").fadeOut();
    turn = "X";
    computer = "O";
    reset();
    gameOn = true;
  });

  //Change player turn to x and computer to o
  $("#turno").click(function() {
    $(".startgame").fadeOut();
    turn = "O";
    computer = "X";
    reset();
    gameOn = true;
  });

  $(".tic").click(function() {
    var slot = $(this).attr("id");
    if (gameOn === true) {
      playerTurn(turn, slot);
    }
  });

  $(".reset").click(function(){
    computerWon=0;
    turnWon=0;
    draw=0;
    $(".human").text(turnWon);
    $(".computer").text(computerWon);
    $(".draw").text(draw);
    reset();
    $(".startgame").fadeIn();
                    });
  
  function playerTurn(turn, id) {
    var spotTaken = turns[id];
    console.log(spotTaken);
    if (!isNaN(spotTaken)) {
      turns[id] = turn;
      console.log(turns);
      //$("#" + id).text(turn);
     // $("#"+id).css("display","none");
      $("#"+id).text(turn);
     // drawMove(id, turn);
      checkDraw = emptyIndexies(turns);
      if (checkDraw.length === 0) {
        show(0);
      }
      if (winCondition(turns, turn)) {
        show(turn);
      } else {
        var bestSpot=computersMove(turns, computer);
        setTimeout(function() {
            $("#" + bestSpot.index).text(computer);
        }, 750);
        //$("#" + bestSpot.index).fadeIn(1000).text(computer);
        //drawMove(bestSpot.index, computer);
        turns[bestSpot.index] = computer;
        console.log("index: " + bestSpot.index);
        console.log("function calls: " + fc);
        if (winCondition(turns, computer)) {
          show(computer);
        }
      }
    }
  }
  //computer's move function
  function computersMove(newBoard, player) {
    
    //keep track of function calls;
    fc++;

    //available spots
    var availSpots = emptyIndexies(newBoard);

    // checks for the terminal states such as win, lose, and tie and returning a value accordingly
    if (winCondition(newBoard, turn)) {
      return { score: -10 };
    } else if (winCondition(newBoard, computer)) {
      return { score: 10 };
    } else if (availSpots.length === 0) {
      return { score: 0 };
    }

    // an array to collect all the objects
    var moves = [];

    // loop through available spots
    for (var i = 0; i < availSpots.length; i++) {
      //create an object for each and store the index of that spot that was stored as a number in the object's index key
      var move = {};
      move.index = newBoard[availSpots[i]];

      // set the empty spot to the current player
      newBoard[availSpots[i]] = player;

      //if collect the score resulted from calling minimax on the opponent of the current player
      if (player == computer) {
        var result = computersMove(newBoard, turn);
        move.score = result.score;
      } else {
        var result = computersMove(newBoard, computer);
        move.score = result.score;
      }

      //reset the spot to empty
      newBoard[availSpots[i]] = move.index;

      // push the object to the array
      moves.push(move);
    }

    // if it is the computer's turn loop over the moves and choose the move with the highest score
    var bestMove;
    if (player === computer) {
      var bestScore = -10000;
      for (var i = 0; i < moves.length; i++) {
        if (moves[i].score > bestScore) {
          bestScore = moves[i].score;
          bestMove = i;
        }
      }
    } else {
      // else loop over the moves and choose the move with the lowest score
      var bestScore = 10000;
      for (var i = 0; i < moves.length; i++) {
        if (moves[i].score < bestScore) {
          bestScore = moves[i].score;
          bestMove = i;
        }
      }
    }
   
     
    // return the chosen move (object) from the array to the higher depth
    return moves[bestMove];
      
  }

  function emptyIndexies(board) {
    return board.filter(s => s != "O" && s != "X");
  }

  function reset() {
    turns = [0, 1, 2, 3, 4, 5, 6, 7, 8];
    count = 0;
    $(".tic").text("");
    $(".human").text(turnWon);
    $(".computer").text(computerWon);
    $(".draw").text(draw);
    gameOn=true;
  }

  //function to check if there is a winner
  function winCondition(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;
    }
  }

  //show the winner
  function show(player) {
    gameOn = false;
     setTimeout(function() {
    $(".endgame").fadeIn(500);
    if (player === 0) {
      $(".text").text("Oh well, It's a draw!");
      draw++;
    } 
       else {
      
      if (player === computer) {
        $(".text").text("Aw snap, the computer won the game!");
        computerWon++;
      } 
         else if (player === turn) {
        $(".text").text("Excelent, you won!");
        turnWon++;
      }
    }
       }, 1500);
  }

  $(".replay").click(function() {
    reset();
   // $(".endgame").css("display", "none");
    $(".endgame").fadeOut();
   // $(".startgame").css("display", "block");
   // $(".startgame").fadeIn();
  });
});

              
            
!
999px

Console