123

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 class="wrapper">
  <h1 class="gameTitle">Tic-tac-toe</h1>
  <div class="game-board"> 

    <!--top row of squares-->
    <div class="play-square" id="sqTopLeft" onclick="sqPlayed(1)"></div>
    <div class="play-square" id="sqTopMiddle" onclick="sqPlayed(2)"></div>
    <div class="play-square" id="sqTopRight" onclick="sqPlayed(3)"></div>

    <!--middle row of squares-->
    <div class="play-square" id="sqMiddleLeft" onclick="sqPlayed(4)"></div>
    <div class="play-square" id="sqMiddleMiddle" onclick="sqPlayed(5)"></div>
    <div class="play-square" id="sqMiddleRight" onclick="sqPlayed(6)"></div>

    <!--bottom row of squares-->
    <div class="play-square" id="sqBottomLeft" onclick="sqPlayed(7)"></div>
    <div class="play-square" id="sqBottomMiddle" onclick="sqPlayed(8)"></div>
    <div class="play-square" id="sqBottomRight" onclick="sqPlayed(9)"></div>


    <!--game grid lines-->
    <div class="game-grid-line vert-line" id="left-vert"></div>
    <div class="game-grid-line vert-line" id="right-vert"></div>
    <div class="game-grid-line horiz-line" id="top-horiz"></div>
    <div class="game-grid-line horiz-line" id="bottom-horiz"></div>
  
  </div>
  
  <h2 class="message">player 1, make the first move!</h2>
  
  <div class="resetButton hide">Play again?</div>
  
</div>
            
          
!
            
              body {
  font-family: Arial, Helvetica, sans-serif; 
  color: SlateGray;
}

.wrapper {
  border: 1px dashed transparent;
  position:relative;
  height: 700px;
  width: auto;
}

.gameTitle {
  position:relative;
  border: 1px dotted transparent;
  height: 60px;
  width:400px;
  font-size: 60px;
  text-align: center;
  margin: 30px auto 15px auto;
}

.message {
  position:relative;
  border: 1px dotted transparent;
  height: 60px;
  width:400px;
  font-size: 22px;
  text-align: center;
  margin: 30px auto 5px auto;
}

.game-board {
  position:relative;
  height: 400px;
  width:400px;
  background-color: LightGray;
  margin: 20px auto 0 auto;
  border-radius: 5%;
}

.play-square {
  position: absolute;
  border: 1px dotted transparent;
  height: 128px;
  width: 126px;
  font-size: 150px;
  text-align: center;
  line-height: .88em;
  cursor: pointer;
}

.game-grid-line {
  position: absolute;
  background-color: DimGray;
}

.vert-line {
  height: 390px;
  width: 10px;
}
.horiz-line {
  width: 390px;
  height: 10px;
}

#left-vert {
  top: 5px;
  left:32%;
}
#right-vert {
  top: 5px;
  left:66%;
}
#top-horiz {
  top: 32%;
  left: 5px;
}
#bottom-horiz {
  top: 66%;
  left:5px;
}

#sqTopLeft {
  left: 0;
  top: 0;
}
#sqTopMiddle {
  left: 34%;
  top: 0;
}
#sqTopRight {
  left: 68%;
  top: 0;
}
#sqMiddleLeft {
  left: 0;
  top: 34%;
}
#sqMiddleMiddle {
  left: 34%;
  top: 34%;
}
#sqMiddleRight {
  left: 68%;
  top: 34%;
}
#sqBottomLeft {
  left: 0;
  top: 68%;
}
#sqBottomMiddle {
  left: 34%;
  top: 68%;
}
#sqBottomRight {
  left: 68%;
  top: 68%;
}

.resetButton {
  position:relative;
  height: 40px;
  width:135px;
  background-color:DarkGreen;
  border: 1px solid DarkGray;
  border-radius: 15px;
  font-size: 20px;
  text-align: center;
  margin: 0 auto 15px auto;
  padding: 15px 0 0 0;
  color: white;
  cursor: pointer;
}

.hide {
  display: none;
}

.align-right {
  text-align:right;
}

.align-left {
  text-align:left;
}
            
          
!
            
              //records moves
var moveLog = [0, 0, 0, 0, 0, 0, 0, 0, 0];

//turn counter
var turnNo = 1;

//var for the 8 possible wins
var leftColWin = []; //1
var midColWin = []; //2
var rightColWin = []; //3
var topRowWin = []; //4
var midRowWin = []; //5
var bottomRowWin = []; //6
var leftDiagWin = []; //7
var rightDiagWin = []; //8

//array slice returns
function linesOfThree() {
  leftColWin = [ moveLog[0], moveLog[3], moveLog[6] ];
  midColWin = [ moveLog[1], moveLog[4], moveLog[7] ];
  rightColWin = [ moveLog[2], moveLog[5], moveLog[8] ];
  topRowWin = moveLog.slice(0, 3);
  midRowWin = moveLog.slice(3, 6);
  bottomRowWin = moveLog.slice(-3);
  leftDiagWin = [ moveLog[0], moveLog[4], moveLog[8] ];
  rightDiagWin = [ moveLog[2], moveLog[4], moveLog[6] ];
}

//to end or continue game
var gameEnd = "no";

//game board position returns
function getPlayPosition(squareNo) {
  switch (squareNo) {
    case 1:
      return document.getElementById("sqTopLeft");
    case 2:
      return document.getElementById("sqTopMiddle");
    case 3:
      return document.getElementById("sqTopRight");
    case 4:
      return document.getElementById("sqMiddleLeft");
    case 5:
      return document.getElementById("sqMiddleMiddle");
    case 6:
      return document.getElementById("sqMiddleRight");
    case 7:
      return document.getElementById("sqBottomLeft");
    case 8:
      return document.getElementById("sqBottomMiddle");
    case 9:
      return document.getElementById("sqBottomRight");
  }
}

var playerTurn = 1;

function nextTurn(playerNo) {
  if (playerNo === 1) {
    playerTurn = 2;
    $('.message').html("player 2 turn").removeClass("align-left")
    .addClass("align-right");
  }
  else if (playerNo === 2) {
    playerTurn = 1;
    $('.message').html("player 1 turn").removeClass("align-right")
    .addClass("align-left");
  }
  console.log("it is now player "+ playerTurn +" turn\n");
}

//check for a win with three in a row
function checkWin() {
  linesOfThree();
  
  if (leftColWin.toString() == "O,O,O" ||
      midColWin.toString() == "O,O,O" ||
      rightColWin.toString() == "O,O,O" ||
      topRowWin.toString() == "O,O,O" ||
      midRowWin.toString() == "O,O,O" ||
      bottomRowWin.toString() == "O,O,O" ||
      leftDiagWin.toString() == "O,O,O" ||
      rightDiagWin.toString() == "O,O,O")
  {     
    console.log("player 1 wins!");
    $('.message').html("player 1 wins!").removeClass("align-right align-left");
    gameEnd = "yes";
    $('.resetButton').removeClass("hide");
  }
  
  else if (leftColWin.toString() == "X,X,X" ||
      midColWin.toString() == "X,X,X" ||
      rightColWin.toString() == "X,X,X" ||
      topRowWin.toString() == "X,X,X" ||
      midRowWin.toString() == "X,X,X" ||
      bottomRowWin.toString() == "X,X,X" ||
      leftDiagWin.toString() == "X,X,X" ||
      rightDiagWin.toString() == "X,X,X") 
  {  
    console.log("player 2 wins!");
    $('.message').html("player 2 wins!").removeClass("align-right align-left");
    gameEnd = "yes";
    $('.resetButton').removeClass("hide");
  }
  
  else {
    //do nothing
  }
}

//checks for cats game
function checkCatsGame() {
  if (turnNo == 10 && gameEnd == "no") {
    console.log("it's a cat's game!");
    $('.message').html("cat's game!").removeClass("align-right align-left");
    gameEnd = "yes";
    $('.resetButton').removeClass("hide");
  }
  else {
    //do nothing
  }
}

function makePlay(playerNo, position) {
  if (getPlayPosition(position).innerHTML === "X" || 
      getPlayPosition(position).innerHTML === "O") {
    console.log("this space has already been played!!!");
  }
  else if (playerNo === 1) {
    console.log("player1 made a turn");
    getPlayPosition(position).innerHTML = "O";
    moveLog[position - 1] = "O";
    nextTurn(1);
    turnNo++;
    console.log(turnNo);
  }
  else if (playerNo === 2) {
    console.log("player2 made a turn");
    getPlayPosition(position).innerHTML = "X";
    moveLog[position - 1] = "X";
    nextTurn(2);
    turnNo++;
    console.log(turnNo);
  }
  console.log(moveLog);
  checkWin();
  checkCatsGame();
}

//gameboard onclick function
function sqPlayed(squareNo) {
  if (gameEnd == "no") {
    console.log ("player made play on position "+ squareNo +"!");
    if (playerTurn === 1) {
      makePlay(1, squareNo);  
    }
    else if (playerTurn === 2) {
      makePlay(2, squareNo);
    }
  }
  else if (gameEnd == "yes") {
    //do nothing
  }
}

$('.resetButton').click(function() {
  $('.resetButton').addClass("hide");
  $('.play-square').html("");
  $('.message').html("player 1 turn").removeClass("align-right")
  .addClass("align-left");
  
  gameEnd = "no";
  playerTurn = 1;
  turnNo = 1;
  
  moveLog = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  
  leftColWin = []; //1
  midColWin = []; //2
  rightColWin = []; //3
  topRowWin = []; //4
  midRowWin = []; //5
  bottomRowWin = []; //6
  leftDiagWin = []; //7
  rightDiagWin = []; //8
});
            
          
!
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.

Console