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 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.

Quick-add: + add another resource

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.

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.

            
              <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">


<div id="background" class="b1">
  
  <div class="loginForm">
    <h2> X or O?</h2>
    <div class="labelHolder">
      <label class="switch">
      <input id="input"  type="checkbox">
      <div class="slider round"> </div>
    </label>
      <div id = "label1" class="toggleLabel label1 ">x</div>
      <div id = "label2" class="toggleLabel label2 ">o</div>
    <div class="clear"></div>
    </div>
    
    
    
  </div>  
  
  





<div class="board">  
  <div class="square top left" id="1"></div>
  <div class="square top" id="2"></div>
  <div class="square top right" id="3"></div>
  <div class="square left" id="4"></div>
  <div class="square" id="5"></div>
  <div class="square right" id="6"></div>
  <div class="square bottom left" id="7"></div>
  <div class="square bottom" id="8"></div>
  <div class="square bottom right" id="9"></div>
</div>  
  
  
</div>  
            
          
!
            
              

body {
  font-family: 'Roboto', sans-serif;
/*  background-color: #00bbd3;*/
  color: rgba(0,0,0,0.87);
  padding: 0;
  margin: 0;
}
h2 {
  font-family: 'Roboto', sans-serif;
}
.board {
  margin: 0 auto;
  width: -webkit-fit-content;
  width: -moz-fit-content;
  width: 286px;
  height: 286px;
  background-color: rgb(250,250,250);
  padding: 40px;
  border-radius: 5px;
  box-shadow: 5px 5px 5px rgba(0,0,0,0.2);
}
.square {
  width: 90px;
  height: 90px;
  border: 4px solid #00bbd3;
  float: left;
  cursor: pointer;
  color: #5c6bc0;
}
.square:nth-child(3n+1){
  clear:both;
}
.top {
  border-top: none;
}
.bottom {
  border-bottom: none;
}
.left {
  border-left: none;
}
.right {
  border-right: none;
}

.player1,
.player2,
.computer{
  font-size: 55px;
  text-align: center;
  vertical-align: text-top;
  cursor: default;
}


            
          
!
            
              //chane symbol by having switches that toggle currentPlayer.symbol and game.mode 

Set.prototype.isSuperset = function(subset) {
    for (var elem of subset) {
        if (!this.has(elem)) {
            return false;
        }
    }
    return true;
}

let waysToWin = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]];


function Game() {
  function toggleXO(){
      var input = document.getElementById("input");
      if (input.checked){
        player1.symbol = "o";
        player2.symbol = "x";
        }
      else{
        player1.symbol = "x";
        player2.symbol = "o";
      }
  }
  this.initialize = function () {
      mode = "single";
      player1 = new User("player1","x");
      //make computer subclass of player
      if (mode=="single"){
          player2 = new User("computer","o")}
      if (mode=="double"){
          player2 = new User("player2","o");
        };
      toggleXO();
      state = {player1:[], player2:[], remaining:[1,2,3,4,5,6,7,8,9]}
      turnCount = 1;
      currentPlayer = player1;
//      state = {};
      addEventListenersToBoard();
  }
   document.getElementById("input").addEventListener("click", toggleXO);
    
  allSquares = document.getElementsByClassName('square');
  addEventListenersToBoard = function (){for (var i=0; i<allSquares.length; i++) {
    allSquares[i].addEventListener('click', playTurn, false);
    }}
  
  this.switchPlayer = function () {
    if (currentPlayer == player1) {
      currentPlayer = player2;
    }
    else {
      currentPlayer = player1;
    }
  }
  let dplay = function(id){
    thisSquare = document.getElementById(id);
    currentPlayer.addSquare(id);
    index=state.remaining.indexOf(parseInt(id));
    state.remaining.splice(index,1);
    thisSquare.removeEventListener("click", playTurn, false);
    turnCount++;
    thisSquare.className += ' '+ currentPlayer.name;
    thisSquare.innerText = currentPlayer.symbol;
    if (turnCount > 5 ){
      let w = currentPlayer.checkWins();
      if (w==true) {
        alert(currentPlayer.symbol+" wins!!!")
        game.initialize();
        game.clearXAndO();
      }
      else if (turnCount == 10) {
        game.initialize();
        game.clearXAndO();
      } 
      else {  
        game.switchPlayer();
        if (mode == "single"&& currentPlayer.name != "player1"){
          game.computerPlay();
      } 
      }
    }
    else {
      game.switchPlayer();
      if (mode == "single"&& currentPlayer.name != "player1"){
        game.computerPlay();
      }  
    }
  }
  
  this.computerPlay = function () {
    max = state.remaining.length;
    randomIndex=Math.floor(Math.random()*(max));
    randomSquare = state.remaining[randomIndex];
    randomSquareId = randomSquare;
    dplay(randomSquareId);
  }
  this.clearXAndO = function () {for (var i=0; i<allSquares.length; i++){
    allSquares[i].classList.remove(player1.name);
    allSquares[i].classList.remove(player2.name);
    allSquares[i].innerText = "";
  }}
  let playTurn = function () {
    id = this.id;
    dplay(id);
  }
  this.initialize();                              
}

function User(name,symbol){
  this.symbol = symbol;
  this.name = name;
//  this.color = color;
  this.squares = [];
  this.addSquare = function(squareId){
    this.squares.push(parseInt(squareId));
  }
  this.checkWins = function(){
    let setA = new Set (this.squares);
    let winFound = waysToWin.filter(function(win){
      let setB = new Set(win);
      return setA.isSuperset(setB);
    })
    if (winFound.length>0){
      return true;
    }
  } 
}

let game = new Game;


            
          
!
999px
Loading ..................

Console