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>
  <p class="player1">Player 1 GO!</p>
</div>
<div>
  <p class="you">Your turn!</p>
</div>
<div>
  <p class="player2">Player 2 GO!</p>
</div>
<div>
  <p class="computer">Computer's </p>
</div>


<div class="box">
  <div>
    <p class="score1"><span id="1score"> 0</span></p>
  </div>
  <div>
    <p class="score2"><span id="2score"> 0</span></p>
  </div>
  
  <button class="reset">reset</button>
  

  <div class="board">

    <div class="winner">
      <div class="whoWon">
        <p id="whoWon"></p>
      </div>
    </div>


    <div class="chooseMode">
      <p>How do you want to play?</p>
      <button class="player-num one-player" id="player1">One Player</button>
      <button class="player-num two-player" id="player2">Two Player</button>
    </div>

    <div class="XorO">
      <p>Would you like to be X or O?</p>
      <button class="choose-x chooseXorO" id="x">X</button>
      <button class="choose-o chooseXorO" id="o">O</button>
      <button class="back-button">Back</button>
    </div>

    <div class="game-board">
      <table id='board'>
        <tr>
          <td class="game" id="cell-0"></td>
          <td class="game" id="cell-1"></td>
          <td class="game" id="cell-2"></td>
        </tr>
        <tr>
          <td class="game" id="cell-3"></td>
          <td class="game" id="cell-4"></td>
          <td class="game" id="cell-5"></td>
        </tr>
        <tr>
          <td class="game" id="cell-6"></td>
          <td class="game" id="cell-7"></td>
          <td class="game" id="cell-8"></td>
        </tr>
      </table>
    </div>


  </div>
</div>
            
          
!
            
              .box {
  -webkit-box-shadow: 0 0 25px 3px rgba(0, 0, 0, 0.74);
  -moz-box-shadow: 0 0 25px 3px rgba(0, 0, 0, 0.74);
  box-shadow: 0 0 25px 3px rgba(0, 0, 0, 0.74);
  background-color: #F2CD32;
  position: relative;
  border-radius: 10px;
  width: 400px;
  height: 400px;
  margin: 10% auto;
  padding: 40px 0 0;
}

.board {
  background-color: #282829;
  width: 350px;
  height: 325px;
  overflow: hidden;
  position: relative;
  margin: 0 auto;
}

.winner {
  background-color: #282829;
  width: 350px;
  height: 325px;
  overflow: hidden;
  position: relative;
  margin: 0 auto;
}

.whoWon {
  width: 100%;
  height: 500px;
  top: 135px;
  text-align: center;
}

#whoWon {
  font-size: 55px;
  margin: 85px 5px 5px 5px;
  color: red;
  background-color: none;
}

button {
  cursor: pointer;
}

.chooseMode,
.XorO {
  font-size: 30px;
  color: white;
  display: block;
  width: 100%;
  height: 500px;
  position: absolute;
  top: 35px;
  text-align: center;
}

.chooseMode button,
.XorO button {
  border: 2px solid transparent;
  font-size: 26px;
  opacity: .5;
  background: none;
  color: white;
  margin: 15px;
}

.chooseMode button:hover,
.XorO button:hover {
  border-radius: 20px;
  border: 2px solid transparent;
  opacity: 1;
  border: 2px dashed rgba(230, 230, 230, .5);
}

.chooseMode button:focus,
.XorO button:focus {
  outline: 0;
}

.XorO button.back-button {
  border: none;
  display: block;
  position: absolute;
  left: 240px;
  top: 200px;
}

.reset {
  position: absolute;
  top: 5px;
  right: 20px;
  border: none;
  border: 2px transparent;
  background: none;
  opacity: .8;
  display: none;
}

.score1 {
  position: absolute;
  top: 5px;
  left: 30px;
  border: none;
  border: 2px transparent;
  background: none;
  opacity: .8;
  font-family: "Times New Roman", Times, serif;
  font-weight: bold;
}

.score2 {
  position: absolute;
  top: 5px;
  left: 130px;
  border: none;
  border: 2px transparent;
  background: none;
  opacity: .8;
  font-size: 15px;
  font-family: "Times New Roman", Times, serif;
  font-weight: bold;
}

table {
  background-color: none;
  width: 340px;
  margin-left: 5px;
  margin-top: 5px;
  height: 315px;
  text-align: center;
  color: white;
  font-size: 45px;
  cursor: pointer;
}

table td {
  width: 80px;
  height: 80px;
}

table td,
table th {
  border: 3px solid white;
}

table tr:first-child td {
  border-top: 0;
}

table tr:last-child td {
  border-bottom: 0;
}

table tr td:first-child,
table tr th:first-child {
  border-left: 0;
}

table tr td:last-child,
table tr th:last-child {
  border-right: 0;
}

.player1 , .you {
  padding: 20px;
  margin: 85px 600px -150px 470px;
  text-align: center;
  background-color: green;
  border-radius: 5px;
  font-size: 17px;
  font-weight: bold;
}

.player2 , .computer{
  padding: 20px;
  margin: 85px 470px -137px 600px;
  text-align: center;
  background-color: yellow;
  border-radius: 5px;
  font-size: 17px;
  position: relative;
  font-weight: bold;
}


/*.players{display: none;} */

.XorO {
  display: none;
}

.game-board {
  display: none;
}

.message {
  /*   display: none; */
  visibility: hidden;
}

.player1,
.player2,
.you,
.computer{
  /*   display: none; */
  visibility: hidden;
  font-family: Lobster, "Times New Roman", Times, serif;
}

.draw {
  display: none;
}

.winner {
  display: none;
}

.score1 {
  display: none;
}

.score2 {
  display: none;
}
            
          
!
            
              var gameType;//choose between 1 player game or 2 player game.
var AItimer;
var player1Timer;
var player2Timer;
var playerTimer;
var computerTimer;

//PLAYER BUTTON - Choose beetween one player or two player game.
$(".player-num").on("click", function(evt) {
  gameType = $(this).attr("id");
  console.log("gameType: " + gameType);
  
  if (gameType === "player2") {
    $("td").addClass("twoPlayerGame");
  }
  else if (gameType === "player1") {
    $("td").addClass("onePlayerGame");
  }
  
  $(".chooseMode").hide();
  $(".XorO").show();
});

//CHOICE BUTTON - Choose X or O.
$(".chooseXorO").on("click", function(evt) {
  var choiceA = $(this).attr("id").toUpperCase();
  //console.log("choiceA: " + choiceA);
  
  $(".XorO").hide();
  $(".reset").show();
  $(".game-board").show();
  
  i = 1;
  index = Math.floor(Math.random() * values.length);
  playsFirst = values[index];

  $(".score1").show();
    $(".score2").show();
  
  if (gameType === "player2") {
    if (player1 === playsFirst) {
        $('.player1').css('visibility', 'visible');
        $('.player2').css('visibility', 'hidden');
      }
      else {
        $('.player2').css('visibility', 'visible');
        $('.player1').css('visibility', 'hidden');
      }
    $(".score1").html(`player 1: ${0}`);
    $(".score2").html(`player 2: ${0}`);
    setValues(choiceA);
  }
  else {
    $(".score1").html(`you: ${0}`);
    $(".score2").html(`computer: ${0}`);
    computer(choiceA);
  }
});

//goes back to how many players menu.
$(".back-button").on("click", function(evt) {
  $(".XorO").hide();
  $(".chooseMode").show();
  $("td").removeClass("twoPlayerGame");
  $("td").removeClass("onePlayerGame");
});

// resets all options.
$(".reset").on("click", function(evt) {
  $(".XorO").hide();
  $(".game-board").hide();
  $(".reset").hide();
  $(".chooseMode").show();
  diakoptis = false;
  //$(".message").html("");
  //$('.message').css('visibility', 'hidden');
  $('.player1').css('visibility', 'hidden');
  $('.player2').css('visibility', 'hidden');
  $('.computer').css('visibility', 'hidden');
  $('.you').css('visibility', 'hidden');
  $(".score1").hide();
  $(".score2").hide();
  $(".player1").stop(true, true); //stops the fadeIn if the user presses "reset"right after he wins.
  $(".player2").stop(true, true); // >> >> >>
  $(".winner").hide();
  playsSecond = "";
  playsFirst = "";
  i = 0;
  $(".game").text(""); //or this $(“.game").each(function(){ $(this).empty(); }) .
  cellValues = [];
  Xvalues = [];
  Ovalues = [];
  winner = false;
  gamesXwon = 0; //O score.
  gamesOwon = 0; //0 score.
  $("#1score").text(" " + 0);
  $("#2score").text(" " + 0);
  $("td").removeClass("twoPlayerGame");
  $("td").removeClass("onePlayerGame");
  clearTimeout(playerTimer);
  clearTimeout(computerTimer);
  clearTimeout(player1Timer);
  clearTimeout(player2Timer);
  clearTimeout(AItimer);
  compTurn = false;
  console.clear();
});



var player1; //keeps track of who's player1.
//assigns value to playsSecond according to playsFirst.
function setValues(choice) {
  player1 = choice;

  if (playsFirst === "X") {
    playsSecond = "O";
  }
  else {
    playsSecond = "X";
  }
}






var HUMAN;
var COMP;
var compTurn = false; //checks if its the computers turn.
//Puts values in computer and player(X or O) in playerVScomputer mode.
function computer(value) {
  console.log("plays first: " + playsFirst);

  HUMAN = value;
  COMP = HUMAN == "X" ? "O" : "X";
  console.log("COMP is: " + COMP);
  if (playsFirst === COMP) {
    $('.computer').css('visibility', 'visible');
    compTurn = true;
    AIturn();
  } 
  else{
    compTurn = false
    $('.you').css('visibility', 'visible');
  }
}

//Handles computer moves.
function AIturn() {
  if (compTurn === true) {
    var emptyTable = [];
    var j = 0;
    cellNames.forEach(function(cell) {
      if ($(cell).text() === "") {
        emptyTable.push(j);
      }
      j++;
    });
    console.log("empty table: " + emptyTable);
    var rand = Math.floor(Math.random() * emptyTable.length);
    console.log("IM IN " + cellNames[emptyTable[rand]]);

    //makes the computer to play with 2 seconds delay.
    AItimer = setTimeout(function() {
      $(cellNames[emptyTable[rand]]).html(COMP);
      $('.you').css('visibility', 'visible');
      $('.computer').css('visibility', 'hidden');
      compTurn = false;
      i++;
      //a win can be done only after 5 moves in total of both players.
      if (emptyTable.length < 5) {
        checkForWin();
        console.log("TRUE BITCH");
      }
      console.log("i is: " + i);
    }, 2000);
  }
}



//Handles player moves for playerVScomputer mode.
$(document).on("click", ".onePlayerGame", function(evt) {
  //checks if the cell is emtpy.
  if ($(evt.target).html() === "") {
    if (compTurn === false) {
      $('.computer').css('visibility', 'visible');
      $('.you').css('visibility', 'hidden');
      console.log(HUMAN);
      $(this).html(HUMAN);
      $('.you').css('visibility', 'hidden');
      $('.computer').css('visibility', 'visible');
      i++;
      compTurn = true;
      if (i > 5) {
        //a win can be done only after 5 moves in total of both players.
        checkForWin();
      }
      console.log("i is player : " + i);
      AIturn();
    }
  }
});






var values = ["X", "O"];
var index = Math.floor(Math.random() * values.length);
var playsFirst = values[index]; //randomly chooses who plays first.
var playsSecond; //plays second.
var i = 0; //moves played.
//Puts values on click to empty cells .
$(document).on("click", ".twoPlayerGame", function(evt) {
  //$('.message').css('visibility', 'hidden');

  if ($(evt.target).html() === "") {
    //checks if the cell is emtpy.
    if (i % 2 !== 0) {
      //first player plays in all odd moves.
      $(this).html(playsFirst);
      if (player1 === playsFirst) {
        $('.player1').css('visibility', 'hidden');
        $('.player2').css('visibility', 'visible');
      }
      else {
        $('.player2').css('visibility', 'hidden');
        $('.player1').css('visibility', 'visible');
      }
    }
    else {
      $(this).html(playsSecond);
      if (player1 === playsSecond) {
        $('.player1').css('visibility', 'hidden');
        $('.player2').css('visibility', 'visible');
      }
      else {
        $('.player2').css('visibility', 'hidden');
        $('.player1').css('visibility', 'visible');
      }
    }
    i++;
    if (i > 5) {
      //a win can be done only after 5 moves in total of both players.
      checkForWin();
    }
  }
});







  var cellNames = [
    "#cell-0",
    "#cell-1",
    "#cell-2",
    "#cell-3",
    "#cell-4",
    "#cell-5",
    "#cell-6",
    "#cell-7",
    "#cell-8"
  ];
  var winCombos = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
  ];
  var cellValues = [];
  var Xvalues = [];
  var Ovalues = [];
  var winner = false;
//Checks if there is a winner to stop the game.
function checkForWin() {

  console.log("fuck off");
  var j = 0;
  var three = 0; //needs to be 3 in order to have a winner.
  cellNames.forEach(function(cell) {
    cellValues[j] = $(cell).text();
    if (cellValues[j] === "X" && Xvalues.indexOf(j) === -1) {
      Xvalues.push(j);
    }
    else if (cellValues[j] === "O" && Ovalues.indexOf(j) === -1) {
      Ovalues.push(j);
    }
    j++;
  });
  console.log("Xvalues: " + Xvalues);
  console.log("Ovalues : " + Ovalues);

  //checks if X won.
  for (var x = 0; x < winCombos.length; x++) {
    for (var t = 0; t < Xvalues.length; t++) {
      if (winCombos[x].indexOf(Xvalues[t]) != -1) {
        three++;
        if (three === 3) {
          console.log("X won!! yayy");
          winner = true;
          if (gameType === "player2") {
            if (player1 === "X") {
              $("#whoWon").text("Player 1 won!");
              newRound("X");
            } else
            {
              $("#whoWon").text("Player 2 won!");
              newRound("X");
            }
          }
          else {
            if (HUMAN === "X") {
              $("#whoWon").text("You won!");
              newRound("X");
            }
            else {
              $("#whoWon").text("Computer won!");
              newRound("X");
            }
          }
        }
      }
    }
    three = 0;
  }
  //checks if O won.
  for (var a = 0; a < winCombos.length; a++) {
    for (var b = 0; b < Ovalues.length; b++) {
      if (winCombos[a].indexOf(Ovalues[b]) != -1) {
        three++;
        if (three === 3) {
          console.log("O won!! yayy");
          winner = true;
          if (gameType === "player2") {
            if (player1 === "O") {
              $("#whoWon").text("Player 1 won!");
              newRound("O");
            }
            else {
              $("#whoWon").text("Player 2 won!");
              newRound("O");
            }
          }
          else {
            if (HUMAN === "O") {
              $("#whoWon").text("You won!");
              newRound("O");
            }
            else {
              $("#whoWon").text("Computer won!");
              newRound("O");
            }
          }
        }
      }
    }
    three = 0;
  }
  if (Ovalues.length + Xvalues.length === 9 && winner === false) {
    $("#whoWon").text("its a draw..");
    newRound();
  }
  
    if(winner === true){
    return true;
  }
}




var gamesXwon = 0; //O score.
var gamesOwon = 0; //O score.
//CALLS A NEW ROUND.
function newRound(champion) {
  
  //hide all menus/tags.
  $('.player1').css('visibility', 'hidden');
  $('.player2').css('visibility', 'hidden');
  $('.you').css('visibility', 'hidden');
  $('.computer').css('visibility', 'hidden');

  $(".winner").show();
  $(".winner").delay(4000).fadeOut(500);

  
  //keeps track of score.
  if (champion === "X") {
    //if X won, X plays first in the next round.
    gamesXwon++;
    //playsFirst = player1;
    playsFirst = "X";
    playsSecond = "O";
  } else if (champion === "O") {
    //if O won, O plays first in the next round.
    gamesOwon++;
    //player1 = playsSecond;
    playsFirst = "O";
    playsSecond = "X";
  }
  else {
      //if its a draw, the player who played first now plays second.
    var z = playsFirst;
    playsFirst = playsSecond;
    playsSecond = z;
  }
  

    //clear values for new game.
  i = 1;
  $(".game").text("");
  cellValues = [];
  Xvalues = [];
  Ovalues = [];
  winner = false;
  
  
    //fade in the player who plays first(winner).
  if ($("td").hasClass("twoPlayerGame")) {
    
    //updating score.
    if(player1 === "X"){
      $(".score1").html(`player 1: ${gamesXwon}`);
      $(".score2").html(`player 2: ${gamesOwon}`);
    }
    else{
      $(".score1").html(`player 1: ${gamesOwon}`);
      $(".score2").html(`player 2: ${gamesXwon}`);
    }
    
    if (playsFirst === player1) {
      //$(".player1").delay(4000).fadeIn(500);
      //$('.player1').css('visibility', 'visible');
      player1Timer = setTimeout(() => { $('.player1').css('visibility', 'visible'); }, 4000)
      
    }
    else {
      //$(".player2").delay(4000).fadeIn(500);
      //$('.player2').css('visibility', 'visible');
      player2Timer = setTimeout(() => { $('.player2').css('visibility', 'visible'); }, 4000)
    }
  }
  else if($("td").hasClass("onePlayerGame")){
    
    //updating score.
    if(HUMAN === "X"){
      $(".score1").html(`you: ${gamesXwon}`);
      $(".score2").html(`computer: ${gamesOwon}`);
    }
    else{
      $(".score1").html(`you: ${gamesOwon}`);
      $(".score2").html(`computer: ${gamesXwon}`);
    }
    
    if (playsFirst === HUMAN) {
      //$(".you").delay(4000).fadeIn(500);
      
      playerTimer = setTimeout(() => { $('.you').css('visibility', 'visible'); }, 4000)
      
      clearTimeout(AItimer);
      compTurn = false;
      console.log("hear here");
    }
    else {
      //$(".computer").delay(4000).fadeIn(500);
      
      computerTimer = setTimeout(() => { $('.computer').css('visibility', 'visible'); }, 4000)
      
      compTurn = true;
      setTimeout(AIturn, 4000);
    }
  }
}

            
          
!
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