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

              
                h1.text-center Tic-Tac-Toe

.text-center.i-controls
  .row
    .btn-group
      button.btn.btn-default.active(id="playAsX" type="button") Play as X
      button.btn.btn-default(id="playAsO" type="button") Play as O
  .row
    .btn-group
      button.btn.btn-default.active(id="multiPlayer" type="button") Multi-player
      button.btn.btn-default(id="singlePlayer" type="button") Single-player
  .row
    button.btn.btn-default(id="resetButton" type="button") Reset
  
.i-message-box
  span &nbsp
  
.board
  table(align="center")
    tr.firstRow
      td.square(id="0")
      td.square(id="1")
      td.square(id="2")
    tr.secondRow
      td.square(id="3")
      td.square(id="4")
      td.square(id="5")
    tr.thirdRow
      td.square(id="6")
      td.square(id="7")
      td.square(id="8")
              
            
!

CSS

              
                .i-controls
  margin-bottom: 25px
  
.i-message-box
  width: 300px
  margin: auto
  margin-bottom: 25px
  padding: 4px
  text-align: center
  background: #eeeeee
  border-radius: 4px

.square
  width: 100px
  height: 100px
  border: 1px solid black
  cursor: pointer
  font-size: 50px
  text-align: center
  
.winning
  background: gold
  transition: 1s

.X
  color: red
  
.O
  color: blue
  
button
  margin-top: 5px

// table
//   margin-top: 200px
              
            
!

JS

              
                // Shout out to "thinsoldier" on Gitter for helping me refactor and improve this code!

$(document).ready(function() {
  // JavaScript variables
  var moveX = true;
  var playerMove = true;
  var gameStart = false;
  var playerIsX = true;
  var isSinglePlayer = false;
  var gameOver = false;

  // jQuery Variables
  var $cells = $('.board td');

  // Functions
  function resetBoard() {
    moveX = true;
    playerMove = true;
    gameStart = false;
    playerIsX = true;
    isSinglePlayer = false;
    gameOver = false;

    $("button").removeClass('active');
    $("#playAsX").addClass('active');
    $("#multiPlayer").addClass('active');

    $("td").empty();

    $(".square").removeClass("X O winning");
  }

  function messageAlert(text) {
    $('.i-message-box span').stop().css('opacity', 1);
    $('.i-message-box span').html(text).animate({
      opacity: 0
    }, 3000);
  }

  function highlightWinningMove(items) {
    for (i in items) {
      $cells.eq(items[i]).addClass("winning");
    }
  }

  function findEmptyCells() {
    var cellList = [];
    $cells.each(function(i, e) {
      cellList.push(i);
    });
    return cellList;
  }

  function randomEmptyCell() {
    var emptyOptions = findEmptyCells();
    var randomIndex = emptyOptions[Math.floor(Math.random() * emptyOptions.length)];
    return $cells.eq(randomIndex);
  }

  $("#resetButton").click(resetBoard);

  $("#playAsO").click(function() {
    if (gameStart === false) {
      playerMove = false;
      playerIsX = false;
      $(this).addClass('active');
      $("#playAsX").removeClass('active');
    } else {
      messageAlert("Game already started!");
    }
  });

  $("#playAsX").click(function() {
    if (gameStart === false) {
      playerMove = true;
      playerIsX = true;
      $(this).addClass('active');
      $("#playAsO").removeClass('active');
    } else {
      messageAlert("Game already started!");
    }
  });

  $("#singlePlayer").click(function() {
    if (gameStart === false) {
      isSinglePlayer = true;
      $(this).addClass('active');
      $("#multiPlayer").removeClass('active');
      if (playerIsX === false) {
        $("#0").trigger("click");
      }
    } else {
      messageAlert("Game already started!");
    }
  });

  $("#multiPlayer").click(function() {
    if (gameStart === false) {
      isSinglePlayer = false;
      $(this).addClass('active');
      $("#singlePlayer").removeClass('active');
    } else {
      messageAlert("Game already started!");
    }
  });

  $(".square").click(function() {
    if (gameOver === false) {
      if (moveX === true) {
        if ($(this).html() === "") {
          $(this).html("X").addClass("X");
          moveX = false;
          gameStart = true;
          if (playerIsX === true) {
            playerMove = false;
          } else {
            playerMove = true;
          }
        }
      } else {
        if ($(this).html() === "") {
          $(this).html("O").addClass("O");
          moveX = true;
          gameStart = true;
          if (playerIsX === true) {
            playerMove = true;
          } else {
            playerMove = false;
          }
        }
      }
    }

    // Winning conditions
    // Horizontal X Wins
    if ($("#0").text() == "X" && $("#1").text() == "X" && $("#2").text() == "X" && gameOver === false) {
      messageAlert("X wins!");
      gameOver = true;
      highlightWinningMove([0, 1, 2]);
    } else if ($("#3").text() == "X" && $("#4").text() == "X" && $("#5").text() == "X" && gameOver === false) {
      messageAlert("X wins!");
      gameOver = true;
      highlightWinningMove([3, 4, 5]);
    } else if ($("#6").text() == "X" && $("#7").text() == "X" && $("#8").text() == "X" && gameOver === false) {
      messageAlert("X wins!");
      gameOver = true;
      highlightWinningMove([6, 7, 8]);
    }
    // Vertical X Wins
    if ($("#0").text() == "X" && $("#3").text() == "X" && $("#6").text() == "X" && gameOver === false) {
      messageAlert("X wins!");
      gameOver = true;
      highlightWinningMove([0, 3, 6]);
    } else if ($("#1").text() == "X" && $("#4").text() == "X" && $("#7").text() == "X" && gameOver === false) {
      messageAlert("X wins!");
      gameOver = true;
      highlightWinningMove([1, 4, 7]);
    } else if ($("#2").text() == "X" && $("#5").text() == "X" && $("#8").text() == "X" && gameOver === false) {
      messageAlert("X wins!");
      gameOver = true;
      highlightWinningMove([2, 5, 8]);
    }
    // Diagonal X Wins
    if ($("#0").text() == "X" && $("#4").text() == "X" && $("#8").text() == "X" && gameOver === false) {
      messageAlert("X wins!");
      gameOver = true;
      highlightWinningMove([0, 4, 8]);
    } else if ($("#2").text() == "X" && $("#4").text() == "X" && $("#6").text() == "X" && gameOver === false) {
      messageAlert("X wins!");
      gameOver = true;
      highlightWinningMove([2, 4, 6]);
    }
    // Horizontal O Wins
    if ($("#0").text() == "O" && $("#1").text() == "O" && $("#2").text() == "O" && gameOver === false) {
      messageAlert("O wins!");
      gameOver = true;
      highlightWinningMove([0, 1, 2]);
    } else if ($("#3").text() == "O" && $("#4").text() == "O" && $("#5").text() == "O" && gameOver === false) {
      messageAlert("O wins!");
      gameOver = true;
      highlightWinningMove([3, 4, 5]);
    } else if ($("#6").text() == "O" && $("#7").text() == "O" && $("#8").text() == "O" && gameOver === false) {
      messageAlert("O wins!");
      gameOver = true;
      highlightWinningMove([6, 7, 8]);
    }
    // Vertical O Wins
    if ($("#0").text() == "O" && $("#3").text() == "O" && $("#6").text() == "O" && gameOver === false) {
      messageAlert("O wins!");
      gameOver = true;
      highlightWinningMove([0, 3, 6]);
    } else if ($("#1").text() == "O" && $("#4").text() == "O" && $("#7").text() == "O" && gameOver === false) {
      messageAlert("O wins!");
      gameOver = true;
      highlightWinningMove([1, 4, 7]);
    } else if ($("#2").text() == "O" && $("#5").text() == "O" && $("#8").text() == "O" && gameOver === false) {
      messageAlert("O wins!");
      gameOver = true;
      highlightWinningMove([2, 5, 8]);
    }
    // Diagonal O Wins
    if ($("#0").text() == "O" && $("#4").text() == "O" && $("#8").text() == "O" && gameOver === false) {
      messageAlert("O wins!");
      gameOver = true;
      highlightWinningMove([0, 4, 8]);
    } else if ($("#2").text() == "O" && $("#4").text() == "O" && $("#6").text() == "O" && gameOver === false) {
      messageAlert("O wins!");
      gameOver = true;
      highlightWinningMove([2, 4, 6]);
    } else if ($("#0").text() != "" && $("#1").text() != "" && $("#2").text() != "" && $("#3").text() != "" && $("#4").text() != "" && $("#5").text() != "" &&
      $("#6").text() != "" && $("#7").text() != "" && $("#8").text() != "") {
      if (gameOver !== true) {
        messageAlert("Tie!");
      }
      gameOver = true;
    }

    if (isSinglePlayer === true && gameOver === false && playerMove === false) {
      randomEmptyCell().trigger("click");
    }

  });
});
              
            
!
999px

Console