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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <h1>Tic-Tac-Toe</h1>
<div id="board">
  <div id="score">
    <div id="p1">Player 1:
      <div class="p1-sc" id="first">0</div>
    </div>
    <div id="p2">Player 2:
      <div class="p2-sc" id="second">0</div>
    </div>
    <div id="reset">
      reset
    </div>
  </div>
  <div id="game">
    <div id="welcome">
      Welcome!
      <span>Please select mode:</span>
      <div id="one">One Player</div>
      <div id="two">Two Player</div>
    </div>
    <div id="selection">
      <div id="message"></div>
      <div id="zero">O</div>
      <div id="x">X</div>
      <div id="back">Back</div>
    </div>
    <div id="playarea">
      <div class="sect" id="top">
        <canvas id="1"></canvas>
        <canvas id="2"></canvas>
        <canvas id="3"></canvas>
      </div>
      <div class="sect" id="middle">
        <canvas id="4"></canvas>
        <canvas id="5"></canvas>
        <canvas id="6"></canvas>
      </div>
      <div class="sect" id="bottom">
        <canvas id="7"></canvas>
        <canvas id="8"></canvas>
        <canvas id="9"></canvas>
      </div>
    </div>
  </div>
</div>
<footer> By: Braulio Salcedo </footer>
              
            
!

CSS

              
                body {
  background-color: black;
  color: white;
  font-family: Calibri;
  text-align: center;
}

h1 {
  font-size: 95px;
  margin-top: 5px;
  margin-bottom: 5px;
}

#one:hover {
  font-size: 25px;
  color: #350b63;
}

#two:hover {
  font-size: 25px;
  color: #350b63;
}

#one:active {
  font-size: 25px;
  color: #350b63;
}

#one,
#two,
#zero,
#x {
  font-size: 20px;
  color: #545251;
  cursor: pointer;
}

#one,
#two {
  margin-top: 15px;
}

#x:hover,
#zero:hover {
  font-size: 65px;
  color: #350b63;
  -webkit-transform: translate(-4px, -10px);
  -moz-transform: translate(-4px, -10px);
  -o-transform: translate(-4px, -10px);
  -ms-transform: translate(-4px, -10px);
  transform: translate(-4px, -10px);
}

#zero,
#x {
  font-size: 50px;
  display: inline-block;
  margin-top: 15px;
  position: absolute;
}

#back {
  display: block;
  color: #350b63;
  width: 75px;
  border-radius: 15%;
  font-size: 25px;
  border: solid #350b63 5px;
  font-weight: bold;
  cursor: pointer;
  margin-left: auto;
  margin-right: auto;
  margin-top: 100px;
}

#back:active,
#reset:active {
  -webkit-transform: translateX(3px);
  -moz-transform: translateX(3px);
  -o-transform: translateX(3px);
  -ms-transform: translateX(3px);
  transform: translateX(3px);
}

#zero {
  left: 95px;
}

#x {
  left: 175px;
}

#one {
  margin-top: -25px;
}

#board {
  display: block;
  margin: auto;
  background-color: #350b63;
  width: 400px;
  height: 400px;
  border-radius: 5%;
  margin-bottom: 25px;
}

#selection {
  opacity: 0;
  visibility: hidden;
  color: black;
  max-width: 250px;
  display: block;
  margin: auto;
  margin-top: -195px;
}

#message {
  font-size: 30px;
  margin-top: -50px;
}

#game {
  width: 300px;
  height: 225px;
  background-color: #cec8d4;
  display: block;
  margin: auto;
  padding-top: 100px;
  position: relative;
  top: 40px;
}

canvas {
  background-color: #cec8d4;
  width: 97px;
  height: 105px;
  color: #632da1;
}

#playarea {
  margin-top: -305px;
  visibility: hidden;
}

#welcome {
  color: black;
  font-size: 65px;
  margin-top: -50px;
}

span {
  font-size: 30px;
  position: relative;
  bottom: 35px;
}

.purple {
  color: #350b63;
  font-size: 25px;
}

footer {
  color: #ffffff;
  position: relative;
  bottom: 56px;
  font-size: 20px;
}

#p1,
.p1-sc,
#p2,
.p2-sc {
  display: inline-block;
  font-weight: bold;
}

#p1 {
  position: relative;
  right: 26px;
  color: #b473ff;
}

#p2 {
  margin-right: 75px;
  color: #644d7d;
}

#score {
  height: 0px;
  position: relative;
  top: 10px;
  visibility: hidden;
  opacity: 0;
}

#reset:hover {
  font-size: 30px;
  -webkit-transform: translate(-4px, -3px);
  -moz-transform: translate(-4px, -3px);
  -o-transform: translate(-4px, -3px);
  -ms-transform: translate(-4px, -3px);
  transform: translate(-4px, -3px);
}

#reset {
  display: inline-block;
  position: absolute;
  left: 272px;
  top: -9px;
  color: #ffffff;
  font-weight: bold;
  font-size: 25px;
  cursor: pointer;
}

#top {
  margin-top: 30px;
}

.jconfirm.jconfirm-my-theme .jconfirm-bg {
  background-color: rgba(71, 37, 99, 0.95);
}

.jconfirm.jconfirm-my-theme .jconfirm-box {
  background-color: transparent;
}

.jconfirm.jconfirm-my-theme .jconfirm-box.jconfirm-type-blue {
  border: none;
}

.jconfirm.jconfirm-my-theme .jconfirm-box.jconfirm-type-green {
  border: none;
}

.jconfirm.jconfirm-my-theme .jconfirm-box.jconfirm-type-red {
  border: none;
}

.jconfirm.jconfirm-my-theme .jconfirm-box.jconfirm-type-orange {
  border: none;
}

.jconfirm.jconfirm-my-theme .jconfirm-box.jconfirm-type-purple {
  border: none;
}

.jconfirm.jconfirm-my-theme .jconfirm-box.jconfirm-type-dark {
  border: none;
}

.jconfirm.jconfirm-my-theme .jconfirm-box div.jconfirm-closeIcon {
  color: white;
}

.jconfirm.jconfirm-my-theme .jconfirm-box div.jconfirm-title-c {
  text-align: center;
  color: white;
  font-size: 50px;
  font-weight: normal;
}

.jconfirm.jconfirm-my-theme .jconfirm-box div.jconfirm-title-c > * {
  margin-top: 50px;
  padding-bottom: 0px;
}

.jconfirm.jconfirm-my-theme .jconfirm-box div.jconfirm-title-c .jconfirm-icon-c {
  margin-right: 8px;
  margin-left: 0px;
}

.jconfirm.jconfirm-my-theme .jconfirm-box div.jconfirm-content-pane {
  margin-bottom: 25px;
}

.jconfirm.jconfirm-my-theme .jconfirm-box div.jconfirm-content {
  text-align: center;
  color: white;
  font-size: 20px;
}

.jconfirm.jconfirm-my-theme .jconfirm-box .jconfirm-buttons button {
  font-size: 16px;
  border-radius: 2px;
  background: #290942;
  text-shadow: none;
  border: none;
  color: white;
  padding: 10px;
  min-width: 100px;
}

.jconfirm.jconfirm-my-theme.jconfirm-rtl .jconfirm-box div.jconfirm-title-c .jconfirm-icon-c {
  margin-left: 8px;
  margin-right: 0px;
}
              
            
!

JS

              
                var $cells = $("canvas"),
  $this,
  $playarea = $("#playarea"),
  $selection = $("#selection"),
  $welcome = $("#welcome"),
  $game = $("#game"),
  $score = $("#score"),
  $cell,
  $p2 = $("#p2"),
  $p1 = $("#p1"),
  $p2Sc = $("#second"),
  $p1Sc = $("#first"),
  $cell = $("#1"),
  $sections = $(".sect"),
  $message = $("#message"),
  $section,
  cell,
  turn,
  player1,
  player2,
  correspondingCells = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [1, 4, 7],
    [2, 5, 8],
    [3, 6, 9],
    [1, 5, 9],
    [3, 5, 7]
  ],
  combinations,
  touchedArr,
  won,
  multi,
  touched = 0,
  firstX,
  score,
  j,
  comp,
  compMoves,
  availableSlots;

$cells.click(makeMove);

function makeMove() {
  $this = $(this);
  if (!touchedArr[this.id - 1] && !won && (multi || (!multi && !turn))) {
    cell = this.getContext("2d");
    triggerCell($this, cell, this.id, 0);
  }
}

function drawX(cell) {
  cell.moveTo(50, 25);
  cell.lineTo(250, 125);
  cell.stroke();
  cell.moveTo(50, 125);
  cell.lineTo(250, 25);
  cell.stroke();
}

function drawO(cell) {
  cell.beginPath();
  cell.ellipse(150, 75, 100, 57, 0, 0, 2 * Math.PI);
  cell.stroke();
}

function updateArrays(player, num) {
  switch (num) {
    case "1":
      combinations = [0, 3, 6];
      break;
    case "2":
      combinations = [0, 4];
      break;
    case "3":
      combinations = [0, 5, 7];
      break;
    case "4":
      combinations = [1, 3];
      break;
    case "5":
      combinations = [1, 4, 6, 7];
      break;
    case "6":
      combinations = [1, 5];
      break;
    case "7":
      combinations = [2, 3, 7];
      break;
    case "8":
      combinations = [2, 4];
      break;
    default:
      combinations = [2, 5, 6];
      break;
  }
  if (player) {
    combinations.forEach(function(curr) {
      player2[curr]++;
      won = player2.indexOf(3) + 1;
    });
  } else {
    combinations.forEach(function(curr) {
      player1[curr]++;
      won = player1.indexOf(3) + 1;
    });
  }

  if (won) {
    correspondingCells[won - 1].forEach(function(curr) {
      cell = $("#" + curr).get(0).getContext("2d");
      cell.strokeStyle = "green";
      if (player) {
        if (firstX) drawO(cell);
        else drawX(cell);
      } else {
        if (firstX) drawX(cell);
        else drawO(cell);
      }
    });
    score[player]++;
    if (player) {
      $p2Sc.text(score[player]);
      if (multi) alertResults("Player two won. Congratulations! :)", false);
      else alertResults("Sorry! You lost! :(", false);
    } else {
      $p1Sc.text(score[player]);
      if (multi) alertResults("Player one won. Congratulations! :)", false);
      else alertResults("Congratulations! You won! :)", false);
    }
  }
}

$(document).ready(function() {
  $cells.each(function() {
    cell = this.getContext("2d");
    cell.lineWidth = 15;
    cell.strokeStyle = "#632da1";
  });
});

$("#one, #two").click(function() {
  makeInvisible($welcome);
  if (this.id === "one") {
    multi = false;
    $message.text("Would you like to be O or X?");
  } else {
    multi = true;
    $message.text("Player 1: Would you like to be O or X?");
  }
  makeVisible($selection);
});

$("#back").click(function() {
  makeInvisible($selection);
  makeVisible($welcome);
});

$("#zero, #x").click(function() {
  makeInvisible($selection);
  makeVisible($playarea);
  makeVisible($score);
  if (this.id === "x") {
    firstX = true;
  } else {
    firstX = false;
  }
  window.setTimeout(function() {
    $game.animate({ backgroundColor: "#000000" }, 1500);
    newGame(true);
  }, 1500);
});

function makeVisible(obj) {
  obj.css({ visibility: "visible" });
  window.setTimeout(function() {
    obj.fadeTo(1500, 1);
  }, 1500);
}

function makeInvisible(obj) {
  obj.fadeTo(1500, 0);
  window.setTimeout(function() {
    obj.css({ visibility: "hidden" });
  }, 1500);
}

$("#reset").click(function() {
  makeInvisible($playarea);
  makeInvisible($score);
  makeVisible($welcome);
  $game.animate({ backgroundColor: "#cec8d4" }, 1500);
  window.setTimeout(function() {
    clearBoard();
  }, 750);
  window.setTimeout(function() {
    $p1Sc.text("0");
    $p2Sc.text("0");
  }, 1500);
});

function newGame(first) {
  player1 = [0, 0, 0, 0, 0, 0, 0, 0];
  player2 = [0, 0, 0, 0, 0, 0, 0, 0];
  touchedArr = [false, false, false, false, false, false, false, false, false];
  turn = getRandomNumber(1, 0);
  touched = 0;
  if (turn) {
    $p2.css({ color: "#b473ff" });
    $p1.css({ color: "#644d7d" });
  } else {
    $p1.css({ color: "#b473ff" });
    $p2.css({ color: "#644d7d" });
  }
  if (first) {
    score = [0, 0];
    $cells.each(function() {
      cell = this.getContext("2d");
      cell.lineWidth = 15;
      cell.strokeStyle = "#632da1";
    });
  } else {
    clearBoard();
  }
  won = false;
  if (!multi && turn) {
    computerMove();
  }
}

function alertResults(message, first) {
  $.confirm({
    title: message,
    content: "Click button to start next game.",
    theme: "my-theme",
    buttons: {
      nextMatch: {
        text: "Next Match",
        action: function() {
          window.setTimeout(function() {
            newGame(first);
          }, 500);
        }
      }
    }
  });
}

function clearBoard() {
  $cells.unbind("click");
  j = 1;
  $sections.each(function() {
    $section = $(this);
    $section.html("");
    $section.html(
      "<canvas id='" +
        j++ +
        "'></canvas><canvas id='" +
        j++ +
        "'></canvas><canvas id='" +
        j++ +
        "'></canvas>"
    );
  });
  $cells = $("canvas");
  $cells.each(function() {
    $this = $(this);
    if (this.id === "2" || this.id === "5" || this.id === "8") {
      $this.css({ "margin-left": "3.7px" });
      $this.css({ "margin-right": "3.7px" });
    } else if (this.id === "1" || this.id === "4" || this.id === "7") {
      $this.css({ "margin-left": "-1.5px" });
    } else {
      $this.css({ "margin-right": "-1.5px" });
    }
  });
  $cells.click(makeMove);
  $cells.each(function() {
    cell = this.getContext("2d");
    cell.lineWidth = 15;
    cell.strokeStyle = "#632da1";
    if ($(this).hasClass("touched")) $(this).toggleClass("touched");
  });
}

function computerMove() {
  comp = null;
  if (player2.indexOf(2) !== -1) {
    compMoves = getCorrespondingIndexes(player2.indexOf(2));
    compMoves.forEach(function(ndx) {
      if (touchedArr[ndx] === false) {
        comp = ndx;
      }
    });
    if (comp === null) {
      player2[player2.indexOf(2)] = 0;
      computerMove();
      comp = null;
    }
  } else if (player1.indexOf(2) !== -1) {
    compMoves = getCorrespondingIndexes(player1.indexOf(2));
    compMoves.forEach(function(ndx) {
      if (touchedArr[ndx] === false) {
        comp = ndx;
        player1[player1.indexOf(2)] = 0;
      }
    });
    if (comp === null) {
      player1[player1.indexOf(2)] = 0;
      computerMove();
      comp = null;
    }
  } else if (!touchedArr[4]) {
    comp = 4;
  } else {
    compMoves = [];

    touchedArr.forEach(function(val, ndx) {
      if (val === false) compMoves.push(ndx);
    });
    comp = compMoves[getRandomNumber(compMoves.length - 1, 0)];
  }

  if (comp !== null) {
    $cell = $("#" + (comp + 1));
    cell = $cell.get(0).getContext("2d");
    if (!won) {
      triggerCell($cell, cell, "" + (comp + 1), 1500);
    }
  }
}

function triggerCell(canvas, cell, id, timer) {
  window.setTimeout(function() {
    updateArrays(Number.parseInt(turn), id);
    if (turn) {
      if (firstX) drawO(cell);
      else drawX(cell);
      turn = 0;
      $p1.css({ color: "#b473ff" });
      $p2.css({ color: "#644d7d" });
    } else {
      if (firstX) drawX(cell);
      else drawO(cell);
      turn = 1;
      $p2.css({ color: "#b473ff" });
      $p1.css({ color: "#644d7d" });
    }
    if (!canvas.hasClass("touched")) {
      canvas.toggleClass("touched");
      touchedArr[Number.parseInt(id) - 1] = true;
    }
    touched++;
    if (!won) {
      if (touched === 9) {
        alertResults("The game was a draw!", false);
      } else if (!multi && turn) {
        computerMove();
      }
    }
  }, timer);
}

function getRandomNumber(max, min) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getCorrespondingIndexes(num) {
  switch (num) {
    case 0:
      availableSlots = [0, 1, 2];
      break;
    case 1:
      availableSlots = [3, 4, 5];
      break;
    case 2:
      availableSlots = [6, 7, 8];
      break;
    case 3:
      availableSlots = [0, 3, 6];
      break;
    case 4:
      availableSlots = [1, 4, 7];
      break;
    case 5:
      availableSlots = [2, 5, 8];
      break;
    case 6:
      availableSlots = [0, 4, 8];
      break;
    default:
      availableSlots = [2, 4, 6];
      break;
  }
  return availableSlots;
}

              
            
!
999px

Console