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.

            
              <h1>Tic Tac Toe</h1>
<p id='state'><br></p>
<button class='btn' onClick='onStart()'>Start</button>
<br><br>
<div class='row'>
  <div class='col-md-4'>
  </div>
  <div class='col-md-2'>
    <p><b>Playing against:</b></p>
    <div id='toggleAIDiv' class='pntr'>
      Human
    </div>
  </div>
  <div class='col-md-2'>
    <p><b>Level:</b></p>
    <div id='levelAIDiv' class='pntr'>Blind</div>
  </div>
  <div class='col-md-4'>
  </div>
</div>
<hr>

<div class='wrapper'>
  <div class='xoBlock' id='bl0' onClick='onMakeMove(0)'></div>
  <div class='xoBlock' id='bl1' onClick='onMakeMove(1)'></div>
  <div class='xoBlock' id='bl2' onClick='onMakeMove(2)'></div>

  <div class='xoBlock' id='bl3' onClick='onMakeMove(3)'></div>
  <div class='xoBlock' id='bl4' onClick='onMakeMove(4)'></div>
  <div class='xoBlock' id='bl5' onClick='onMakeMove(5)'></div>

  <div class='xoBlock' id='bl6' onClick='onMakeMove(6)'></div>
  <div class='xoBlock' id='bl7' onClick='onMakeMove(7)'></div>
  <div class='xoBlock' id='bl8' onClick='onMakeMove(8)'></div>
</div>
            
          
!
            
              body {
  text-align: center;
  font-family: monospace;
  background-color: #CCC;
}
.pntr {
  cursor: pointer;
}
hr {
  width: 90%;
  margin: 50px;
}
.wrapper {
  position: absolute;
  left: 50%;
  margin-left: -150px;
  height: 300px;
  width: 300px;
  clip: rect(2px, 298px, 298px, 2px);
}
.xoBlock {
  border: solid 2px black;
  height: 100px;
  width: 100px;
  font-size: 5em;
  cursor: pointer;
}
#bl0 {
  
  position: absolute;
  left: 0px;
  top: 0px;
}
#bl1 {
  position: absolute;
  left: 100px;
  top: 0px;
}
#bl2 {
  position: absolute;
  left: 200px;
  top: 0px;
}
#bl3 {
  position: absolute;
  left: 0px;
  top: 100px;
}
#bl4 {
  position: absolute;
  left: 100px;
  top: 100px;
}
#bl5 {
  position: absolute;
  left: 200px;
  top: 100px;
}
#bl6 {
  position: absolute;
  left: 0px;
  top: 200px;
}
#bl7 {
  position: absolute;
  left: 100px;
  top: 200px;
}
#bl8 {
  position: absolute;
  left: 200px;
  top: 200px;
}

            
          
!
            
              
var toggleAI = false;
var playing = false;
$('#toggleAIDiv').on('click', function() {
  if (!playing) {
    toggleAI = toggleAI === false ? true: false;
    console.log(toggleAI);
  }
  
  if (toggleAI) {
    $('#toggleAIDiv').html('AI');
  } else {
    $('#toggleAIDiv').html('Human');
  }
});
var levelAI = 'Blind';
$('#levelAIDiv').on('click', function() {
 if (!playing) {
  switch (levelAI) {
   case 'Blind': 
     levelAI = 'Novice';
     break;
   case 'Novice':
     levelAI = 'Master';
     break;
   case 'Master':
     levelAI = 'Blind';
     break;
        } 
 }
  $('#levelAIDiv').html(levelAI);
});

var State = function(old) {
 
  // State setup
  this.board = [];
  this.movesTotal = 0;
  this.oMovesMade = 0;
  this.turn = '';
  this.advanceTurn = function() {
    this.turn = old.turn === 'X' ? 'O' : 'X';
  }
  
  // building new state from old.
  if (typeof old !== 'undefined') {
    
    this.board = old.board;
    this.oMovesMade = old.oMovesMade;
    this.movesTotal = old.movesTotal;
    this.movesTotal++;
    this.turn = old.turn;
    
    
    
  }
  
  
  
  //functions
  
  this.result = function () {
    // check rows
    for (var i = 0; i < 9; i += 3) {
      
    if (this.board[i] !== 'E' && this.board[i] === this.board[i + 1] && this.board[i + 1] === this.board[i + 2]) {
      playing = false;
      return this.board[i] + ' wins!';
      
    }
      
    }
    // check collumns
    for (var j = 0; j < 3; j++) {
      if (this.board[j] !== 'E' && this.board[j] === this.board[j + 3] && this.board[j + 3] === this.board[j + 6]) {
      playing = false; 
      return this.board[j] + ' wins!';
        
      }
    }
    // check diagonals
    if (this.board[0] !== 'E' && this.board[0] === this.board[4] && this.board[4] === this.board[8] || this.board[2] !== 'E' && this.board[2] === this.board[4] && this.board[4] === this.board[6]) {
      playing = false;
      return this.board[4] + ' wins!';
      
    } 
    var check = false;
    for (var k = 0; k < this.board.length; k++) {
      if (state.board[k] == 'E') {
        check = true;
      }
    }
    if (check) {
      return 'Playing';
    } else {
      playing = false;
      return 'Draw';
    }
  }
  this.emptyCells = function () {
    
    var ec = [];
    
    for (var i = 0; i < this.board.length; i++) {
      if (this.board[i] === 'E') {
        
        ec.push(i);
        
      }
    }
    
    return ec;
  }
  this.updateBoard = function() {
    $('#state').html(this.result());
    for (var i = 0; i < this.board.length; i++) {
  if (this.board[i] === 'X') {
  
    
    $('#bl' + i).html('X');
  } else if (this.board[i] === 'O') {
    $('#bl' + i).html('O');
  } else if (this.board[i] === 'E') {
    $('#bl' + i).html('');
  }
}
   
  }
  this.isTerminal = function () {
    if (this.board.length == 9) {
   return this.result() !== 'Playing'
  }
  }
  if (this.isTerminal()) {
    
    alert (this.result() + score(state));
  }
  console.log(state);
};

function onStart () {
  playing = true;
  state = new State();
  state.turn = 'X';
  state.board = ['E','E','E','E','E','E','E','E','E']
  console.log(miniMax(state));
  state.updateBoard();
}
function onMakeMove (pos) {
  if (state.board[pos] === 'E' && !state.isTerminal()) {
  state.board[pos] = state.turn;
  state = new State(state)
  state.advanceTurn();
  state.updateBoard();
  console.log(miniMax(state));
  if (state.turn === 'O') {
       if (toggleAI) {
    aiMove();
      }
      state.oMovesMade++;
  }
  }
  
}



var AI = function() {
  var selector;
  this.blindMove = function() {
  selector = state.emptyCells()[Math.floor(Math.random() * state.emptyCells().length)]; 
  onMakeMove(selector);
  
  }
  this.noviceMove = function() {
    alert('test');
  }
  this.masterMove = function() {
    alert('test2');
  }
  
}
var ai = new AI();
function aiMove() {
var lvl = levelAI;
  switch(lvl) {
    case 'Blind':
      ai.blindMove();
      break;
                }
  
}
function miniMax(_st) {
  var st1 = _st;
  if (st1.isTerminal()) {
    return score(st1);
  } else {
    var stateScore;
    var availablePos = st1.emptyCells();
    
    var nextStates = availablePos.map(function(val) {
      var tempState = st1;
      tempState.board[val] = tempState.turn;
      console.log(tempState);
      //tempState = new State(tempState);
      //tempState.advanceTurn();
      //return tempState;
    });
    console.log(availablePos);
  }
  
}
function score(_state) {
  console.log(_state.result());
  if (_state.isTerminal()) {
    if (_state.result() === 'X wins!') {
      return 10 - _state.oMovesMade;
    } else if (_state.result() === 'O wins!') {
      return -10 + _state.oMovesMade;
    } else {
      return 0;
    }
  }
}

            
          
!
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