cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <h1> Welcome to Tic Tac Toe</h1>
<div id = "gameboard">
<div class="row top">
  <div id="one" class="box left"> </div>
   <div class="box middle"> </div>
   <div class="box right"> </div>
</div>
<div class="row middle">
  <div class="box left"> </div>
   <div class="box middle"> </div>
   <div class="box right"> </div>
</div>
<div class="row bottom">
  <div class="box left"> </div>
   <div class="box middle"> </div>
   <div class="box right"> </div>
</div>
</div>

<div id="dashboard">
  <div class="message"></div>
  
  <div class="control"><i class="fa fa-3x fa-refresh"></i></div>
</div>
            
          
!
            
              body {
  font-family: "Gochi Hand";
  text-align: center;
  background-image: url('http://subtlepatterns2015.subtlepatterns.netdna-cdn.com/patterns/tree_bark.png')
}

#one {
  background-color: ;
}

#gameboard {
  background-color: #ddf5dd;
  opacity: 0.7;
  margin: 0 auto;
  text-align: center;
  height: 396px;
  width: 396px;
  box-shadow: 5px 5px 10px;
  margin-bottom: 50px;
}

#dashboard {
  background-color: #ddddf5;
  opacity: 0.7;
  margin: 0 auto;
  text-align:center;
  height: 80px;
  width: 396px;
  box-shadow: 5px 5px 10px;
    text-align:center;
  border-radius: 5px;
}

.box {
  font-size: 7em;
  //display: inline-block;
  //vertical-align: middle;
  float: left;
  width: 130px;
  height: 100%;
  cursor: pointer;
  cursor: hand;
}

.message {
  font-size: 2.5em;
  width: 280px;
  display: inline-block;
 vertical-align: middle;   
 text-align: center;
 margin-top: 5px;    
 box-shadow: 2px 2px 5px;
 border-radius: 5px;
  height: 70px;
}

.control {
 width: 94px;  
 display: inline-block;
 vertical-align: middle;   
 text-align: center;
 margin-top: 5px;    
 box-shadow: 2px 2px 5px;
 border-radius: 5px;
  height: 70px;
}

.hover {
  background-color: steelblue;
}

.winner{
  background-color: green;
}

.row {
  clear: both;
  height: 130px;
}

.row.top,
.row.middle {
  border-bottom: solid 3px black;
}

.box.left,
.box.middle {
  border-right: solid 3px black;
}
            
          
!
            
              $('document').ready(function() {

  var player = 'human',
    mode = "easy",
    width = 3,
    height = 3,
    gameboardRows = height,
    gameboardCols = width,
    gameArray = [],
    legalMove = false,
    marker = "X",
    gameOver = false,
    computerCell,
    playerCell,
    possibleWins = [
      [0, 3, 6],
      [1, 4, 7],
      [2, 5, 8],
      [0, 1, 2],
      [3, 4, 5],
      [6, 7, 8],
      [0, 4, 8],
      [2, 4, 6]

    ],
    moves = 0,
    winner = 'none',
    preferredMoves = [4, 0, 2, 6, 8, 1, 3, 5, 7];

  initBoard();
  drawBoard(gameArray);

  function init() {
    console.log("Reinitializing...");
    moves = 0;
    winner = "none";
    marker = "X";
    player = "human";
    gameOver = false;
    legalMove = false;
    initBoard();
    //clearBoard();
  }

  function initBoard() {
    for (var i = 0; i < gameboardRows * gameboardCols; i++) {
      gameArray[i] = 0;
      $(".box").html("").removeClass("winner");
      console.log("game array looks like this:", gameArray);
      $(".message").html("New Game");
    }
  }

  $(".box").click(function() {
    if (player === "human") {
      playerCell = getBoxCell($(this));
      legalMove = checkLegalMove(playerCell);

      if (legalMove) {
        playCell(playerCell);
        if (!gameOver) {
          computerMoves(mode, gameArray);
        }
      }
    }
  })

  .hover(function() {
    $(this).addClass("hover");
  }, function() {
    $(this).removeClass("hover");
  });

  function playCell(cellIndex) {
    moves += 1;
    console.log("Moves: ", moves);
    gameArray[cellIndex] = marker;
    drawBoard(gameArray);
    checkStatus(gameArray);
    togglePlayer();
  }

  function getBoxCell(obj) {
    var row = obj.parent().index(),
      col = obj.index();
    return ([row * height + col]);
  }

  function checkStatus(boardArr) {
    /* jshint expr: true */
    //check for win
    //onsole.log("Checking for Win ...not implemented... ");
    checkWin();
    if (winner === player) {
      console.log("Player: ", player, "Wins!");
      $(".message").html(player + " Wins!")
      setTimeout(function() {
        init();
      }, 1000);

    } else
    //check for draw
    if (moves === 9) {
      gameOver = true;
      console.log("Draw, Game Over!");
      setTimeout(function() {
        init();
      }, 1000);
      //do the thing here for every element       
    } else {
      console.log("Game not over, next turn");
    }
  }

  function checkWin() {
    //get each win pattern
    possibleWins.forEach(function(sequence) {

      //check board array for those possitions, use falsy bouncer method
      if ((gameArray[sequence[0]] === marker) && (gameArray[sequence[1]] === marker) && (gameArray[sequence[2]] === marker)) {
        winner = player;
        gameOver = true;
        drawWinningCombo(sequence);

      }
    })
  }

  function computerMoves(mode, gameArray) {
    if (mode === "dumb") {
      console.log("Picking random move");
      computerCell = Math.floor(Math.random() * 9);
    } else if (mode === "easy") {
      var i = 0;
      while (i < 9) {
        if (gameArray[preferredMoves[i]] === 0) {
          break;
        }
        i += 1;
      }
      computerCell = preferredMoves[i];

    }
    legalMove = checkLegalMove(computerCell);
    if (legalMove) {
      playCell(computerCell);
    } else {
      computerMoves(mode, gameArray);
    }

  }

  function checkLegalMove(cell) {
    if (gameArray[cell] === 0) {
      return true;
    } else {
      return false;
    }
  }

  function togglePlayer() {
    ((player === "human") ? player = "computer" : player = "human");
    ((marker === "X") ? marker = "O" : marker = "X");
  }

  function drawBoard(boardArr) {
    boardArr.forEach(function(cell, index) {
      if (cell !== 0) {
        var row = Math.floor(index / 3) + 1;
        var col = (index + 3) % 3 + 1;
        $(".row:nth-child(" + (row) + ") .box:nth-child(" + (col) + ")").html(cell);

        console.log(row, col, cell);
      }
    });
  }

  function drawWinningCombo(sequence) {
    sequence.forEach(function(cell) {
      var row = Math.floor(cell / 3) + 1;
      var col = (cell + 3) % 3 + 1;
      $(".row:nth-child(" + (row) + ") .box:nth-child(" + (col) + ")").addClass("winner");
    })
  }

});
            
          
!
999px
Loading ..................

Console