cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

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.

            
              <div class="container" style="margin: 50px auto">
  <div class="row">
    <div class="col-xs-4 top-left" id="top-left" onclick="tlSquareFunc()"><p class="marker" id="marker_1">&nbsp;</p></div>
    <div class="col-xs-4 top-center" id="top-center" onclick="tcSquareFunc()"><p class="marker" id="marker_2">&nbsp;</p></div>
    <div class="col-xs-4 top-right" id="top-right" onclick="trSquareFunc()"><p class="marker" id="marker_3">&nbsp;</p></div>
  </div>
  <div class="row">
    <div class="col-xs-4 middle-left" id="middle-left" onclick="mlSquareFunc()"><p class="marker" id="marker_4">&nbsp;</p></div>
    <div class="col-xs-4 middle-center" id="middle-center" onclick="mcSquareFunc()"><p class="marker" id="marker_5">&nbsp;</p></div>
    <div class="col-xs-4 middle-right" id="middle-right" onclick="mrSquareFunc()"><p class="marker" id="marker_6">&nbsp;</p></div>
  </div>
  <div class="row">
    <div class="col-xs-4 bottom-left" id="bottom-left" onclick="blSquareFunc()"><p class="marker" id="marker_7">&nbsp;</p></div>
    <div class="col-xs-4 bottom-middle" id="bottom-middle" onclick="bcSquareFunc()"><p class="marker" id="marker_8">&nbsp;</p></div>
    <div class="col-xs-4 bottom-right" id="bottom-right" onclick="brSquareFunc()"><p class="marker" id="marker_9">&nbsp;</p></div>
  </div>  
</div>
<div class="choose-marker" id="choose-marker">
  <div class="container">
    <div class="row">
      <div class="col-xs-2"></div>
      <div class="col-xs-8">
        <h1 class="ultra">TIC-TAC-TOE</h1>
        <h2 class="ultra">Choose a marker:</h2>
        <button class="btn btn-lg buttons" onclick="chooseX()">X</button><button class="btn btn-lg buttons" onclick="chooseO()">O</button>
      </div>
      <div class="col-xs-2"></div>
    </div>
  </div>
</div>
<div class="player-wins ultra" id="player-wins">PLAYER WINS!</div>
<div class="cpu-wins ultra" id="cpu-wins">CPU WINS!</div>
<div class="tie ultra" id="tie">YOU TIED!</div>

            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Martel+Sans:400,900);

body {
    background: #349522 url('http://www.mikemorkes.com/codepen/tictactoe/ttt_green_bkgd.jpg') no-repeat fixed bottom center / cover;
  font-family: 'Martel Sans', sans-serif;
}

h1 {
  font-size: 80px;
}

.buttons {
  font-weight: 900;
  padding-top: 15px;
  background: #000;
  margin: 0 20px;
}

.ultra {
  font-weight: 900;
  text-shadow: 0px 0px 10px rgba(0, 0, 0, .5);
}

.marker {
  position: relative;
  color: #fff;
  margin: auto;
  font-size: 140px;
  font-weight: bold;
  text-shadow: 0px 0px 10px rgba(0, 0, 0, .5);
  font-weight: 900;
}

.top-left {
  border-right: 8px solid rgba(0, 0, 0, .5);
  border-bottom: 8px solid rgba(0, 0, 0, .5);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */
  text-align: center;
  height: 200px;
  cursor:pointer;
}

.top-center {
  border-bottom: 8px solid rgba(0, 0, 0, .5);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */  
  text-align: center;
  height: 200px;
  cursor:pointer;  
}

.top-right {
  border-left: 8px solid rgba(0, 0, 0, .5);
  border-bottom: 8px solid rgba(0, 0, 0, .5);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */  
  text-align: center;
  height: 200px;  
  cursor:pointer;  
}

.middle-left {
  border-right: 8px solid rgba(0, 0, 0, .5);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */  
  text-align: center;
  height: 200px;   
  cursor:pointer;  
}

.middle-center {
  text-align: center;
  height: 200px;
  cursor:pointer;  
}

.middle-right {
  border-left: 8px solid rgba(0, 0, 0, .5);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */  
  text-align: center;
  height: 200px; 
  cursor:pointer;  
}

.bottom-left {
  border-top: 8px solid rgba(0, 0, 0, .5);
  border-right: 8px solid rgba(0, 0, 0, .5);  
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */  
  text-align: center;
  height: 200px;
  cursor:pointer;  
}

.bottom-middle {
  border-top: 8px solid rgba(0, 0, 0, .5);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */  
  text-align: center;
  height: 200px; 
  cursor:pointer;  
}

.bottom-right {
  border-top: 8px solid rgba(0, 0, 0, .5);
  border-left: 8px solid rgba(0, 0, 0, .5);  
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */  
  text-align: center;
  height: 200px;  
  cursor:pointer;  
}

.player-wins {
  width: 100%;
  height: 100%;
  z-index: 10000;
  float: center;
  background: #602db0 url('http://www.mikemorkes.com/codepen/tictactoe/ttt_purple_bkgd.jpg') no-repeat fixed bottom center / cover;
  color: #fff;
  text-align: center;
  margin: auto auto;
  position: absolute;
  top: 0;
  left: 0;
  padding-top: 100px;
  font-size: 100px;
  display: none;
}

.cpu-wins {
  width: 100%;
  height: 100%;
  z-index: 10000;
  float: center;
  background: #d71b29 url('http://www.mikemorkes.com/codepen/tictactoe/ttt_red_bkgd.jpg') no-repeat fixed bottom center / cover;
  color: #fff;
  text-align: center;
  margin: auto auto;
  position: absolute;
  top: 0;
  left: 0;
  padding-top: 100px;
  font-size: 100px;
  display: none;
}

.tie {
  width: 100%;
  height: 100%;
  z-index: 5000;
  float: center;
  background: #333333 url('http://www.mikemorkes.com/codepen/tictactoe/ttt_gray_bkgd.jpg') no-repeat fixed bottom center / cover;
  color: #000;
  text-align: center;
  margin: auto auto;
  position: absolute;
  top: 0;
  left: 0;
  padding-top: 100px;
  font-size: 100px;
  display: none;
}

.choose-marker {
  width: 100%;
  height: 100%;
  z-index: 5000;
  float: center;
  background: #4179b4 url('http://www.mikemorkes.com/codepen/tictactoe/ttt_blue_bkgd.jpg') no-repeat fixed bottom center / cover;
  color: #fff;
  text-align: center;
  margin: auto auto;
  position: absolute;
  top: 0;
  left: 0;
  padding-top: 100px;
  font-size: 100px;
  display: block;
}

.winColors {
  color: yellow;
}

.winColorsReset {
  color: #fff;
}
            
          
!
            
              // got some ideas from http://www.dreamincode.net/forums/topic/247361-simple-tic-tac-toe-using-html5-css3-and-javascript/ 

// GLOBAL VARIABLES
// squares default values: 0 = off, 1 = X, 2 = O
var squareStatus = {
  "square_1": 0,
  "square_2": 0,
  "square_3": 0,
  "square_4": 0,
  "square_5": 0,
  "square_6": 0,
  "square_7": 0,
  "square_8": 0,
  "square_9": 0
};
var playerMarker = "X";
var squareCount = 0;
var win = false;
var elementID;
var playerSquares = [];
var cpuSquares = [];
var winningSquares = [];
var winCountCPU = 0;
var winCountP = 0;
var idNum;
var cpuSeq = [];
var winningCombinations = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [1, 4, 7],
  [2, 5, 8],
  [3, 6, 9],
  [1, 5, 9],
  [3, 5, 7]
];

// generate random sequence for cpu to pick squares from  - yeah it's dumb logic, but at least the human wins once in a while
function genSeq(array) {
  var i = array.length,
    j = 0,
    temp;

  while (i--) {

    j = Math.floor(Math.random() * (i + 1));

    // swap randomly chosen element with current element
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;

  }
  cpuSeq = [].concat(array);
}

// button functions for player to choose marker
function chooseX() {
  playerMarker = "X";
  document.getElementById("choose-marker").style.display = "none";
}

function chooseO() {
  playerMarker = "O";
  document.getElementById("choose-marker").style.display = "none";
}

// Player squares functions
function tlSquareFunc() {
  if (playerMarker === "X" && squareStatus["square_1"] === 0) {
    document.getElementById("marker_1").innerHTML = "X";
    $('#marker_1').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_1"] = 1;
    playerSquares.push(1);
    endHumanTurn();
  } else if (squareStatus["square_1"] === 0) {
    document.getElementById("marker_1").innerHTML = "O";
    $('#marker_1').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_1"] = 2;
    playerSquares.sort(function(a, b) {
      return a - b;
    });
    playerSquares.push(1);
    endHumanTurn();
  } else {

  }

}

function tcSquareFunc() {
  if (playerMarker === "X" && squareStatus["square_2"] === 0) {
    document.getElementById("marker_2").innerHTML = "X";
    $('#marker_2').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_2"] = 1;
    playerSquares.push(2);
    endHumanTurn();
  } else if (squareStatus["square_2"] === 0) {
    document.getElementById("marker_2").innerHTML = "O";
    $('#marker_2').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_2"] = 2;
    playerSquares.push(2);
    endHumanTurn();
  } else {

  }
}

function trSquareFunc() {
  if (playerMarker === "X" && squareStatus["square_3"] === 0) {
    document.getElementById("marker_3").innerHTML = "X";
    $('#marker_3').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_3"] = 1;
    playerSquares.push(3);
    endHumanTurn();
  } else if (squareStatus["square_3"] === 0) {
    document.getElementById("marker_3").innerHTML = "O";
    $('#marker_3').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_3"] = 2;
    playerSquares.push(3);
    endHumanTurn();
  } else {

  }
}

function mlSquareFunc() {
  if (playerMarker === "X" && squareStatus["square_4"] === 0) {
    document.getElementById("marker_4").innerHTML = "X";
    $('#marker_4').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_4"] = 1;
    playerSquares.push(4);
    endHumanTurn();
  } else if (squareStatus["square_4"] === 0) {
    document.getElementById("marker_4").innerHTML = "O";
    $('#marker_4').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_4"] = 2;
    playerSquares.push(4);
    endHumanTurn();
  } else {

  }
}

function mcSquareFunc() {
  if (playerMarker === "X" && squareStatus["square_5"] === 0) {
    document.getElementById("marker_5").innerHTML = "X";
    $('#marker_5').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_5"] = 1;
    playerSquares.push(5);
    endHumanTurn();
  } else if (squareStatus["square_5"] === 0) {
    document.getElementById("marker_5").innerHTML = "O";
    $('#marker_5').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_5"] = 2;
    playerSquares.push(5);
    endHumanTurn();
  } else {

  }
}

function mrSquareFunc() {
  if (playerMarker === "X" && squareStatus["square_6"] === 0) {
    document.getElementById("marker_6").innerHTML = "X";
    $('#marker_6').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_6"] = 1;
    playerSquares.push(6);
    endHumanTurn();
  } else if (squareStatus["square_6"] === 0) {
    document.getElementById("marker_6").innerHTML = "O";
    $('#marker_6').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_6"] = 2;
    playerSquares.push(6);
    endHumanTurn();
  } else {

  }
}

function blSquareFunc() {
  if (playerMarker === "X" && squareStatus["square_7"] === 0) {
    document.getElementById("marker_7").innerHTML = "X";
    $('#marker_7').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_7"] = 1;
    playerSquares.push(7);
    endHumanTurn();
  } else if (squareStatus["square_7"] === 0) {
    document.getElementById("marker_7").innerHTML = "O";
    $('#marker_7').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_7"] = 2;
    playerSquares.push(7);
    endHumanTurn();
  } else {

  }
}

function bcSquareFunc() {
  if (playerMarker === "X" && squareStatus["square_8"] === 0) {
    document.getElementById("marker_8").innerHTML = "X";
    $('#marker_8').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_8"] = 1;
    playerSquares.push(8);
    endHumanTurn();
  } else if (squareStatus["square_8"] === 0) {
    document.getElementById("marker_8").innerHTML = "O";
    $('#marker_8').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_8"] = 2;
    playerSquares.push(8);
    endHumanTurn();
  } else {

  }
}

function brSquareFunc() {
  if (playerMarker === "X" && squareStatus["square_9"] === 0) {
    document.getElementById("marker_9").innerHTML = "X";
    $('#marker_9').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_9"] = 1;
    playerSquares.push(9);
    endHumanTurn();
  } else if (squareStatus["square_9"] === 0) {
    document.getElementById("marker_9").innerHTML = "O";
    $('#marker_9').addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
      function(e) {
        $(this).removeClass('animated bounceIn');
      }
    );
    squareStatus["square_9"] = 2;
    playerSquares.push(9);
    endHumanTurn();
  } else {

  }
}

function endHumanTurn() {
  squareCount = squareCount + 1;
  setTimeout(function() {
    computerAI();
  }, 500);
}

function computerAI() {
  checkForWinP();
  aiLogic: if (win === true) {
      break aiLogic;
    } else {
      checkSquareCount();
      for (var a = 1; a < 9; a++) {

        idNum = cpuSeq.shift();

        if (squareStatus["square_" + idNum] === 0) {
          elementID = "marker_" + idNum;
          if (playerMarker === "X") {
            document.getElementById(elementID).innerHTML = "O";
            $('#' + elementID).addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
              function(e) {
                $(this).removeClass('animated bounceIn');
              }
            );
            squareStatus["square_" + idNum] = 2;
            cpuSquares.push(idNum);
            checkSquareCount();
            checkForWinCPU();
            break;
          } else {
            document.getElementById(elementID).innerHTML = "X";
            $('#' + elementID).addClass('animated bounceIn').one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
              function(e) {
                $(this).removeClass('animated bounceIn');
              }
            );
            squareStatus["square_" + idNum] = 1;
            cpuSquares.push(idNum);
            checkSquareCount();
            checkForWinCPU();
            break;
          }
        }
      }
    }

}

function checkSquareCount() {
  if (squareCount === 5 && win === false) {

    setTimeout(function() {
      console.log("NO WINNER");
      document.getElementById("tie").style.display = "block";
    }, 500);
    setTimeout(function() {
      resetBoard();
    }, 2000);

  }
}

// functions to check for winners
function checkForWinP() {
  winningSquares = [];
  for (var c = 0; c < 8; c++) {
    var tempArr = winningCombinations[c];
    for (var d = 0; d < 3; d++) {
      var tempObject = tempArr[d];
      for (var e = 0; e <= playerSquares.length; e++) {
        if (tempObject === playerSquares[e]) {
          winCountP = winCountP + 1;
          if (winCountP === 3) {
            win = true;
            winningSquares = [].concat(tempArr);
            break;
          } else {}
        }
      }

    }
    winCountP = 0;
  }
  if (win === true) {
    winCountP = 0;
    winSeqP();
  }
}

function checkForWinCPU() {
  winningSquares = [];
  for (var e = 0; e < 8; e++) {
    var tempArrCPU = winningCombinations[e];
    for (var f = 0; f < 3; f++) {
      var tempObjectCPU = tempArrCPU[f];
      for (var g = 0; g < cpuSquares.length; g++) {
        if (tempObjectCPU === cpuSquares[g]) {
          winCountCPU = winCountCPU + 1;
          
          if (winCountCPU === 3) {
            win = true;
            winningSquares = [].concat(winningCombinations[e]);
            break;
          } else {}
        }
      }
    }
    winCountCPU = 0;
  }
  if (win === true) {
    winCountCPU = 0;
    winSeqCPU();
  }
}

// functions to highlight winning markers and then reset them
function highlightNum() {
    for (var winNum = 1; winNum < 4; winNum++) {
    idNum = winningSquares.shift();
    elementID = "marker_" + idNum;
    $('#' + elementID).addClass('winColors');
  }
}

function resetHighlights() {
    for (var winNumReset = 1; winNumReset < 10; winNumReset++) {
    elementID = "marker_" + winNumReset;
    $('#' + elementID).removeClass('winColors');
  }  
}


// functions for win sequences
function winSeqP() {
  win === true;

  highlightNum();

  setTimeout(function() {
    console.log("WINNER, PLAYER");
    document.getElementById("player-wins").style.display = "block";
  }, 500);

  setTimeout(function() {   
    resetBoard();
  }, 2000);
}

function winSeqCPU() {
  win === true;

  highlightNum();
  
  setTimeout(function() {
    console.log("WINNER, COMPUTER");
    document.getElementById("cpu-wins").style.display = "block";
  }, 500);
  setTimeout(function() {
    resetBoard();
  }, 2000);
}


// function to reset the board
function resetBoard() {
  for (var b = 1; b < 10; b++) {
    var resetElementID = "marker_" + b;
    document.getElementById(resetElementID).innerHTML = " ";
    squareStatus["square_" + b] = 0;
  }
  squareCount = 0;
  playerSquares = [];
  cpuSquares = [];
  winCountCPU = 0;
  winCountP = 0;
  cpuSeq = [];
  genSeq([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  console.log("board reset");
  win = false;
  resetHighlights();
  document.getElementById("player-wins").style.display = "none";
  document.getElementById("cpu-wins").style.display = "none";
  document.getElementById("tie").style.display = "none";
  document.getElementById("choose-marker").style.display = "block";

}

// generate inital sequence
window.onload = function() {
  genSeq([1, 2, 3, 4, 5, 6, 7, 8, 9]);
};
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console