Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                body
  div &nbsp;
#box  
.jumbotron
  .container
    .row
     h1(class=h1color) Tic Tac Toe     
      h3 
-var col = "pink";

 div
 h1#choice(class="input") Choose your symbol


  
  #choice.container
    .row
      div(class="col-xs-12 text-center")
       h3(class="pink")
           
           label(for="x")  X
           input(id="x" type="radio" name="selection" value="x" checked="checked")
           
           label(for="o")  O
           input(id="o" type="radio" name="selection" value="o")
           button(class="btn btn-primary aqua" onclick="start();") Start


  
 
  
 
 div#test22
  #box_area(class="container hidden")
      //if has_game_started
      .row
        .block
          button#r0c0(class=col box onclick="play_handler(r0c0['id'])" )

          button#r0c1(class=col box onclick="play_handler(r0c1['id'])") 
          button#r0c2(class=col box onclick="play_handler(r0c2['id'])") 



      .row
        .block
          button#r1c0(class=col box onclick="play_handler(r1c0['id'])") 
          button#r1c1(class=col box onclick="play_handler(r1c1['id'])") 
          button#r1c2(class=col box onclick="play_handler(r1c2['id'])") 

        //button(class="pill-button") -

      .row 
        .block
          button#r2c0(class=col onclick="play_handler(r2c0['id'])") 
          button#r2c1(class=col onclick="play_handler(r2c1['id'])") 
          button#r2c2(class=col onclick="play_handler(r2c2['id'])") 

br
.container
  .row
    #reset_area(class="col-sm-12 hidden")
     button(class="btn btn-primary aqua" onclick="reset();") Reset   
 
#quack

audio#beep(src="http://abir.x10.mx/jstest/ding.wav")
              
            
!

CSS

              
                @import "bourbon";
.h1color {color: red}
//$button
.numfonts {
color: #333333;
font-family: ‘Palatino Linotype’, ‘Book Antiqua’, Palatino, serif; 
}

 $background: #3F51B5; //rgba(51, 15, 10, 100) 

$button: #330f0a; //rgba(51, 15, 10, 100)
$color2: #394f49; //rgba(57, 79, 73, 100)
$color3: #65743a; //rgba(101, 116, 58, 100)
$color4: #efdd8d;
$color5: #efdd8d; //rgba(239, 221, 141, 100)
$color6: #f4fdaf; //rgba(244, 253, 175, 100)
$button-text: #f4fdaf;
//colors end
//buttons

.brown { @include button(pill, $button);}
.pill-button {    @include button(pill, #ff9900);}

.red{    @include button(pill, red);}

.pink { @include button(pill,#FF4081
)}
.aqua {@include button(pill,#18FFFF
)}
.purple {@include button(pill,#3F51B5
)}
body,
html {
  width: 100%;
  background-color: $background;
  color: $background;
}
hr {
  color: $background;
}
h1 {
  color: red;
}
#quack{
  color: white;
}
.calculator {
  width: 1050px;
  height: 635px;
  background-color: $color4;
  margin: 0 auto;
}
.row {
    display: block;
    text-align: center;
  .block
    button {
      width: 100px;
      height: 100px;
      background-color: $button;
      color: $button-text;
      margin: 6px;
      border: 1px solid $color2;
      border-top: 2px solid lighten($color2, 20);
      font-size: 4em;
   /*    offset-x | offset-y | blur-radius | spread-radius | color */
      box-shadow: 12px -7px $color2;
  }
}




.modal.modal-wide .modal-dialog {
  width: 90%;
}
.modal-wide .modal-body {
  overflow-y: auto;
}

/* irrelevant styling */
body { text-align: center; }
body p { 
  max-width: 400px; 
  margin: 20px auto; 
}
#tallModal .modal-body p { margin-bottom: 900px }
  
              
            
!

JS

              
                var game_board = clear_board();
var last_symbol_played="";
var latest_position;

//GUI FUNCTIONS START HERE
function disable_buttons(){


  $('#r0c0').attr('disabled','disabled');
  $('#r0c1').attr('disabled','disabled');
  $('#r0c2').attr('disabled','disabled');
  $('#r1c0').attr('disabled','disabled');
  $('#r1c1').attr('disabled','disabled');
  $('#r1c2').attr('disabled','disabled');
  $('#r2c0').attr('disabled','disabled');
  $('#r2c1').attr('disabled','disabled');
  $('#r2c2').attr('disabled','disabled');
  
 
  
}
function draw_board(board){
  //var new_board=remove_underscores(board);
  var positions=get_position(board);
 
  for (var i in positions){
    if (positions[i]==="_"){
      $('#'+i).html("&nbsp;");
    }
    else{
    $('#'+i).html(positions[i]);
    }
  }
  
}
function enable_buttons(){
   $('#r0c0').removeAttr('disabled');
  $('#r0c1').removeAttr('disabled');
  $('#r0c2').removeAttr('disabled');
  $('#r1c0').removeAttr('disabled');
  $('#r1c1').removeAttr('disabled');
  $('#r1c2').removeAttr('disabled');
  $('#r2c0').removeAttr('disabled');
  $('#r2c1').removeAttr('disabled');
  $('#r2c2').removeAttr('disabled'); 
}
function start(){
  $("#choice").addClass('hidden');
  $("#box_area").removeClass('hidden');
   $("#reset_area").removeClass('hidden');
  enable_buttons();

  var comp_symbol = flip_symbol($('input[name=selection]:checked').val());
 // $('#quack').html(comp_symbol);
  var done=false;
  
  computer_play_handler(comp_symbol);
  

/*have button to start
option to choose x and o
ALL code should happen here
don't change any other functions
*/
  
}
function computer_play_handler(comp_symbol){
  var comp_symbol;
   if (last_symbol_played.length === 0){
 //   comp_symbol="x";
   }
  else{
    comp_symbol=flip_symbol(last_symbol_played);
  }
  
    //5 seconds delay and display a "spinner" that shows the computer is thinking
    var move=next_move(comp_symbol,game_board);
   
    if (move!==-1){
      var cp=play(comp_symbol,move,game_board);
      
      game_board=cp;
      last_symbol_played=comp_symbol;
      
      draw_board(game_board);
      //5 seconds delay and display a "spinner" that shows the computer is thinking
    } 
   if (winner(game_board)==="x" || winner(game_board)==="o"){
    // $("#box_area").addClass('hidden');
     disable_buttons();

    beep.play();  
  }
  
}
function play_handler(id){
  var position={};
  position['r0c0']=0;
  position['r0c1']=1;
  position['r0c2']=2;
  position['r1c0']=3;
  position['r1c1']=4;
  position['r1c2']=5;
  position['r2c0']=6;
  position['r2c1']=7;
  position['r2c2']=8;
  
  latest_position=position[id];
  
  var cp;
  var new_symbol;
 
  if (last_symbol_played.length === 0){
    new_symbol="x";
    cp=play(new_symbol,latest_position,game_board)
  }
  else {
    new_symbol=flip_symbol(last_symbol_played);
    cp=play(new_symbol,latest_position,game_board)
  }
  if (cp!==null){
    last_symbol_played=new_symbol;
    game_board=cp;//this may need to be taken out
  }
  
  if (winner(game_board)==="x" || winner(game_board)==="o"){
    
    draw_board(game_board); //this may be a new function that will draw a line through teh winning board
    
    beep.play();  
    
  }
  else{
    draw_board(cp);
    computer_play_handler();
    
    
  }

  //
  
  
}
function get_position(board){
  
  var position={};
  position['r0c0']=board[0];
  position['r0c1']=board[1];
  position['r0c2']=board[2];
  position['r1c0']=board[3];
  position['r1c1']=board[4];
  position['r1c2']=board[5];
  position['r2c0']=board[6];
  position['r2c1']=board[7];
  position['r2c2']=board[8];
  
  return position;
}
function tester(){
  //var board=["x","x","x","x","_","o","x","o","x"]
  play_handler();
  location=3;
  //draw_board(board);
  //clear_board(board);
  
}
//GUI FUNCTIONS END HERE
//ENGINE FUNCTIONS START HERE
function reset(){
  game_board=clear_board();
  draw_board(game_board);
  last_symbol_played="";
  $("#choice").removeClass('hidden');
  $("#box_area").addClass('hidden');
  $("#reset_area").addClass('hidden');
  
}
function flip_symbol(symbol){
  if (symbol==="_"){return "_";}
  if (symbol==="x"){  
    symbol="o";
  }
  else{
    symbol="x"
  }
  return symbol;
}
function show_initial_board(){
  //game_board=clear_board();
  //draw_board(game_board);
  disable_buttons();

    
   //     $("#box_area").hide();


  
  
}
function play(symbol,location,board){
  //play (symbol, location, board) updates the board with the symbol returns the updated board. return null if board cannot be updated 

  var new_board=[];
  for (var i in board){
    new_board.push(board[i]);
  }
  if (board[location] === "_"){
    new_board[location]=symbol;
    
  }
  else{
    new_board=null;
  }
  //console.log(new_board);
  return new_board;
  
}
function winner(board) {
  // returns x or o if one of them  is a winner - if not returns null
  var result=null;
  var i=0;
  var done=false;
  var all_wins=['012','345','678','036','147','258','048','246'];
  while (!done && i < all_wins.length){
    var a=Number(all_wins[i][0]);
    var b=Number(all_wins[i][1]);
    var c=Number(all_wins[i][2]);
   
   if (board[a]===board[b] && board[b]===board[c] && board[a]!=="_" ){
     done=true;
     result=board[a];    
     
   }
      i++
   
  }

  return result;
}
function next_move(symbol,board){

  var move=-1;
  /*The following may appear strange but I am doing so because javascript objects are mutable
   if board is an object which it is (it is an array and arrays are objects) and I use:
   var new_board=board; 
   new_board is not a copy of board. It is board. Both point to the same object. Any changes to new_board will also change board, because they are the same object. For the CS kinds this is pass by reference not pass by value
  */
  var new_board=[];
  for (var i in board){
    new_board.push(board[i]);
  }

  var os=open_slots(new_board);
  
  if (os.length===0){
    return null;
  }
  
  //console.log(os.length)
  var done=false;
  var i=0;
  
  while (i<os.length && !done){
     
      if(symbol===winner(play(symbol,os[i],new_board))){
        
        done=true;
        move=os[i];
       
      }
    
           
      i++ ;
  }
    
  if (move ===-1) {
    symbol=flip_symbol(symbol);
    i=0;
     while (i<os.length && !done){
     
      if(symbol===winner(play(symbol,os[i],new_board))){
        done=true;
        move=os[i];
       
      }
    
           
      i++ ;
     }
     	
  }
  //code is not elegant
  if (move===-1){
    for (var i in os){
      if (os[i]===4){
        move=os[4]
      }
    }
    if (move===-1){
      move=os[0];
    }
  }

  return move;
}
function clear_board(){
  
  game_board=["_","_","_","_","_","_","_","_","_"];
  
  return game_board;
}
function open_slots(board){
  //board=["x","_","x","_","o","_","_","_","_"];
  var open_slots=[];
  for (var i=0; i<board.length; i++){
    if(board[i] === "_"){
      open_slots.push(i);
    }
    
  }

  return open_slots;
}




              
            
!
999px

Console