css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="container">
  <h1>Tic-Tac-Toe</h1>  
  <canvas id="canvas"></canvas>
  <hr>
  <div id="user-marker-field">Your Marker
    <br /><input id="user-marker" type="text" maxlength="1" value="X"></div>
  <div id="reset" class="button" >Reset</div>
  <div class="log"></div>
</div>
            
          
!
            
              body {
  background: #475577;
  color: #FAC51C;
}

#container {
  width: 100%;
  text-align: center;
  margin-top: 20px;
}

h1 {
  font-size: 60px;
}

hr {
  width: 500px;
  margin-bottom: 30px;
  margin-top: 0px;
  border: 0px;
  height: 1px;
  border-top: 4px solid #FAC51C;
}

canvas {
  position: relative;
  margin: 30px;
  display: inline;
}

div.button {
  color: #475577;
  background: #FAC51C;
  cursor: pointer;
  max-width: 100px;
  padding: 10px 15px;
  font-size: 25px;
  font-weight: 800;
  margin: 0 auto;
}

div.button:active {
  color: darken(#475577, 20%);
  box-shadow: 0 0 5px -1px rgba(0, 0, 0, 0.6);
}

div.log {
  padding: 15px;
  font-size: 30px;
}

#user-marker-field {
  font-size: 40px;
 
}

#user-marker {
  width: 1em;
  padding: 10px;
  margin: 15px;
  font-size: 50px;
  text-align: center;
  border: 5px solid lighten(#475577,40%);
}

#user-marker:read-only {
  background-color: #EBEBE4;
  border: 5px solid #ABADB3;
  cursor: not-allowed;
}

.log {
    margin: 0 auto;
  width: 350px;
}
.log-item {
  display: inline-block;
  padding: 15px 10px 5px 10px;
  clear:both;
  border-bottom: 1px solid #FAC51C;
}
            
          
!
            
              // Logging function for debugging
function log() {

  var logItem = "<div class=\"log-item\">";

  for (var i = 0; i < arguments.length; i++) {
    if (typeof arguments[i] === 'object') {
      logItem += JSON.stringify(arguments[i]);
    } else {
      logItem += arguments[i];
    }

    if (i !== arguments.length - 1) {
      logItem += ", ";
    }

  }

  $(".log").append(logItem + "</div>");

}

function compare() {
  if (arguments.length === 0) {
    return false;
  }
  var equal = true;
  for (var i = 0; i < arguments.length - 1; i++) {
    if (arguments[i] !== arguments[i + 1]) {
      equal = false;
    }
  }
  return equal;
}

// Grid square
function Square(x, y, width, height) {
  this.x = x;
  this.y = y;
  this.width = width;
  this.height = height;
  this.mark = "";
}

Square.prototype.contains = function(x, y) {
  return this.x <= x && this.y <= y && (this.x + this.width) >= x && (this.y + this.height) >= y;
};

Square.prototype.marked = function() {
  return this.mark !== "";
};

Square.prototype.centerX = function() {
  return this.x + this.width / 2;
}

Square.prototype.centerY = function() {
  return this.y + this.height / 2;
}

// store + render a tic tac toe grid
function TicTacToe(width, height, padding) {
  this.width = width;
  this.height = height;
  this.padding = padding;
  this.tiles = [];
};

TicTacToe.prototype.makeGrid = function() {
  for (var y = 0; y < this.height; y += this.height / 3) {
    for (var x = 0; x < this.width; x += this.width / 3) {

      this.tiles.push(
        new Square(x, y, (this.width / 3) - this.padding, (this.height / 3) - this.padding)
      );
    }
  }
};

TicTacToe.prototype.drawGrid = function(ctx) {
  for (var i = 0; i < this.tiles.length; i++) {
    var tile = this.tiles[i];
    ctx.fillStyle = "#FAC51C";
    ctx.fillRect(tile.x, tile.y, tile.width, tile.height);
  }
};

TicTacToe.prototype.drawMarks = function(ctx) {
  for (var i = 0; i < this.tiles.length; i++) {
    var tile = this.tiles[i];
    ctx.fillStyle = "#475577";
    ctx.fillText(tile.mark, tile.centerX(), tile.centerY() + 30);
  }
}

TicTacToe.prototype.isFull = function() {
  var count = 0;
  for (var i = 0; i < this.tiles.length; i++) {
    if (this.tiles[i].mark !== "") {
      count = count + 1;
    }
  }
  return (count === this.tiles.length);
};

TicTacToe.prototype.tryOrFail = function(x, y, mark, ctx) {

  if (grid.hasWinner(computerMarker) || grid.hasWinner(userMarker) || grid.isFull()) {
    grid.restart();
    return;
  }

  for (var i = 0; i < this.tiles.length; i++) {
    var tile = this.tiles[i];
    if (tile.contains(x, y) && !tile.marked()) {
      tile.mark = mark;
      return true;
    }
  }

  return false;

};

TicTacToe.prototype.checkRow = function(row, mark) {
  return compare(this.tiles[row * 3].mark, this.tiles[row * 3 + 1].mark, this.tiles[row * 3 + 2].mark, mark);
};

TicTacToe.prototype.checkColumn = function(col, mark) {
  return compare(this.tiles[col].mark, this.tiles[col + 3].mark, this.tiles[col + 6].mark, mark);
};

TicTacToe.prototype.checkAscendingDiagonal = function(mark) {
  return compare(this.tiles[2].mark, this.tiles[4].mark, this.tiles[6].mark, mark);
};

TicTacToe.prototype.checkDescendingDiagonal = function(mark) {
  return compare(this.tiles[0].mark, this.tiles[4].mark, this.tiles[8].mark, mark);
};

TicTacToe.prototype.hasWinner = function(mark) {
  // check for diagonal wins
  if (this.checkAscendingDiagonal(mark) || this.checkDescendingDiagonal(mark)) {
    return true;
  }

  // Check vertical & horizontal lines
  for (var x = 0; x < 3; x++) {
    if (this.checkColumn(x, mark)) {
      return true;
    }
    if (this.checkRow(x, mark)) {
      return true;
    }
  }
  return false;
};

TicTacToe.prototype.aiRespond = function() {

  // win
  for (var i = 0; i < this.tiles.length; i++) {
    if (this.tiles[i].mark === "") {
      this.tiles[i].mark = computerMarker;
      if (this.hasWinner(computerMarker)) {
        return;
      } else {
        this.tiles[i].mark = "";
      }
    }
  }

  // block wins
  for (var i = 0; i < this.tiles.length; i++) {
    if (this.tiles[i].mark === "") {
      this.tiles[i].mark = userMarker;
      if (this.hasWinner(userMarker)) {
        this.tiles[i].mark = computerMarker;
        return;
      } else {
        this.tiles[i].mark = "";
      }
    }
  }

  // go middle
  if (this.tiles[4].mark === "") {
    this.tiles[4].mark = computerMarker;
    return;
  }

  // check for straddle strat
  if ((this.tiles[0].mark === userMarker && this.tiles[8].mark === userMarker) 
      || (this.tiles[2].mark === userMarker && this.tiles[6].mark === userMarker)) {
    // edge
    for (i = 1; i < this.tiles.length; i += 2) {
      if (this.tiles[i].mark == "") {
        this.tiles[i].mark = computerMarker;
        return;
      }
    }

  }

  // corners 
  for (i = 0; i < this.tiles.length; i += 2) {
    if (this.tiles[i].mark == "") {
      this.tiles[i].mark = computerMarker;
      return;
    }
  }

  // next available
  for (var i = 0; i < this.tiles.length; i++) {
    if (this.tiles[i].mark == "") {
      this.tiles[i].mark = computerMarker;
      return;
    }
  }
};

TicTacToe.prototype.restart = function() {

  this.active = true;
  for (var i = 0; i < this.tiles.length; i++) {
    this.tiles[i].mark = "";
  }
  this.drawGrid(ctx);

};

// Returns the mouse position.
function getMousePos(canvas, evt) {
  var rect = canvas.getBoundingClientRect();
  return {
    x: evt.clientX - rect.left,
    y: evt.clientY - rect.top
  };
}

// Submit a move when things are clicked
$("#canvas").click(function(evt) {

  $("#user-marker").attr("readonly", true);
  userMarker = $("#user-marker").val();
  if (userMarker === "O") {
    computerMarker = String.fromCharCode(userMarker.charCodeAt(0) + 1);
  } else {
    computerMarker = "O";
  }

  var pos = getMousePos(canvas, evt);

  if (!grid.tryOrFail(pos.x, pos.y, userMarker, ctx)) {
    return;
  }

  if (grid.hasWinner(userMarker)) {
    log("Round #" + round, "you won!");
    round++;
  } else {
    grid.aiRespond();

    if (grid.hasWinner(computerMarker)) {
      log("Round #" + round, "you lost!");
      round++;
    } else if (grid.isFull()) {
      log("Round #" + round, "tie!");
      round++;
    }
  }

  grid.drawMarks(ctx);

});

$("#reset").click(function() {
  grid.restart();
  $(".log").text("");
  round = 1;
  $("#user-marker").attr("readonly", false);

});

// Instantiate canvas and set its size attributes
var width = 400;
var height = 400;
var round = 1;
var userMarker = "X";
var computerMarker = "O";

$('canvas').attr({
  'width': width,
  'height': height
});
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

// Create a grid and fill it 
var grid = new TicTacToe(width, height, 10);
ctx.font = "80px Arial";
ctx.textAlign = "center";

grid.makeGrid();
grid.drawGrid(ctx);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console