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='container-fluid'>
  <div class='row'>
    <div class='col-sm-5 text-center'>
      <table>
        <tr>
          <td>
            <a href='#'>
              <div id='1' class='tiles row1 col1 diag1'></div>
            </a>
          </td>
          <td>
            <a href='#'>
              <div id='2' class='tiles row1 col2'></div>
            </a>
          </td>
          <td>
            <a href='#'>
              <div id='3' class='tiles row1 col3 diag2'></div>
            </a>
          </td>
        </tr>
        <tr>
          <td>
            <a href='#'>
              <div id='4' class='tiles row2 col1'></div>
            </a>
          </td>
          <td>
            <a href='#'>
              <div id='5' class='tiles row2 col2 diag1 diag2'></div>
            </a>
          </td>
          <td>
            <a href='#'>
              <div id='6' class='tiles row2 col3'></div>
            </a>
          </td>
        </tr>
        <tr>
          <td>
            <a href='#'>
              <div id='7' class='tiles row3 col1 diag2'></div>
            </a>
          </td>
          <td>
            <a href='#'>
              <div id='8' class='tiles row3 col2'></div>
            </a>
          </td>
          <td>
            <a href='#'>
              <div id='9' class='tiles row3 col3 diag1'></div>
            </a>
          </td>
        </tr>
      </table>
      <h3 id='turn' class='text-center'></h3>
    </div>
    <div class='col-sm-1 text-center'></div>
    <div class='col-sm-6'>
      <div id='opponent'>
        <h1>Opponent</h1>
        <input type="checkbox" id='human' class='opp' checked>
        <p>Human</p>
        </input>
        <input type="checkbox" id='dumbAI' class='opp'>
        <p>Dumb AI</p>
        </input>
        <input type="checkbox" id='smartAI' class='opp'>
        <p>Smart AI</p>
        </input>
      </div>

      <div id='symbol'>
        <h1>Player 1</h1>
        <input type="checkbox" id='x' class='sym'>
        <p>X</p>
        </input>
        <input type="checkbox" id='o' class='sym'>
        <p>O</p>
        </input>
      </div>

      <div id='score'>
        <h1>Score</h1>
        <p>Player 1: <span id='p1Score'>0</span></p>
        <br>
        <p>Opponent: <span id='oppScore'>0</span></p>
        <br>
        <p>Ties: <span id='tie'>0</span></p>
      </div>

    </div>

  </div>
</div>
            
          
!
            
              body {
  background-color: #699864;
}

table {
  display: inline-block;
  margin-top: 20px;
}

.tiles {
  background-color: white;
  border: 5px solid #699864;
  text-align: center;
  width: 3cm;
  height: 3cm;
  font-size: 2cm;
}

p {
  font-size: 20px;
  display: inline-block;
}

input {
  display: inline-block;
  vertical-align: center;
  width: 10%;
  zoom: 2;
}

a, a:hover, a:active, a:visited {
  text-decoration: none !important;
  color: #699864;
  font-family: papyrus, sans-serif;
}
            
          
!
            
              var xSymbol, turn, player1, moves = 0, player2 = 'human', rows = ['.row1','.row2','.row3','.col1','.col2','.col3','.diag1','.diag2'];
$(document).ready(function() {
  
  // if opponent changes, reset score, turn, and symbol
  $('.opp').click(function() {
    $('input.opp, input.sym').filter(':checked').not(this).removeAttr('checked');
    player2 = $(this).attr('id');
    $('span').html('0');
    $('#turn, .tiles').empty();
    xSymbol = null;
  })

  // determine first player & symbol
  $('.sym').click(function() {
    $('input.sym').filter(':checked').not(this).removeAttr('checked');
    xSymbol = $('#x').is(':checked') ? true : false;
    player1 = xSymbol ? 'X' : 'O';
    turn = player1;
    
    $('#turn').html(player1+"'s turn");
  })
  
  // when you click a tile
  $('.tiles').click(function() {
    
    // make a move
    if (xSymbol != null) {  
      if ($(this).is(':empty')) {
        $(this).html(turn); // place symbol if it's a valid space
        moves++; // update how many turns have been taken
        checkWinOrTie(); // self explanatory
        opponent(player2); // opponent move
      } // end of if the tile is empty
    } // end of make sure symbol is chosen
    
    // if X or O not selected
    else {
      alert('Please choose X or O');
    }  
  }); // end of click tile
}) // end of doc ready


function opponent(player2) {
  // change to comp symbol
  xSymbol = !xSymbol;
  turn = xSymbol ? 'X' : 'O';
  
  // just change whose turn it is
  if (player2 == 'human') {
    $('#turn').html(turn+"'s turn");
  }
  
  else if (player2 == 'dumbAI') {
    // check to see if 2 of comp symbol in a row
    comp = 0; i = -1; filled = 0;
    while (comp != 2 && i<rows.length) {
      comp = 0; filled = 0; i++;
      $(rows[i]).each(function() {
        if ($(this).html() == turn) {
          comp++; 
        }
        if (!$(this).is(':empty')) {
          filled++;
        }
        if (filled == 3) {
          comp = 0; filled = 0; i++;
        }
      })
    }
    // offensive - go for the win
    if (comp == 2 && filled != 3) {
      $(rows[i]).html(turn);
    }
    else {
      // find the empty spaces
      empty = [];
      $('.tiles').each(function() {
        if ($(this).is(':empty')) {
          empty.push(parseInt($(this).attr('id')));
        }
      });
      // make a random move
      $('#'+String(empty.randEl())).html(turn);
    }
    // update moves, check end game, change symbol back to human
    moves++;
    checkWinOrTie();
    xSymbol = !xSymbol;
    turn = xSymbol ? 'X' : 'O';
  }
  
  else if (player2 == 'smartAI') {
    // take the center if it's empty
    if ($('#5').is(':empty')) {
      $('#5').html(turn)
      centerTakenFirst = false;
    }
    else {
      if (moves == 1) {
        centerTakenFirst = true;
      }
      // check to see if 2 of comp symbol in a row
      comp = 0; i = -1; filled = 0;
      while (comp != 2 && i<rows.length) {
        comp = 0; filled = 0; i++;
        $(rows[i]).each(function() {
          if ($(this).html() == turn) {
            comp++; 
          }
          if (!$(this).is(':empty')) {
            filled++;
          }
          if (filled == 3) {
            comp = 0; filled = 0; i++;
          }
        })
      }
      // offensive - go for the win
      if (comp == 2 && filled != 3) {
        $(rows[i]).html(turn);
      } 
      
      // if comp can't win, see if it can block
      else {
        // change back to my symbol to check it
        xSymbol = !xSymbol;
        turn = xSymbol ? 'X' : 'O';
        
        // check to see if 2 in a row of mine
        mine = 0; i = -1; filled = 0;
        while (mine != 2 && i<rows.length) {
          mine = 0; filled = 0; i++;
          $(rows[i]).each(function() {
            if ($(this).html() == turn) {
              mine++; 
            }
            if (!$(this).is(':empty')) {
              filled++;
            }
            if (filled == 3) {
              mine = 0; filled = 0; i++;
            }
          })
        }
        // change back to comp symbol
        xSymbol = !xSymbol;
        turn = xSymbol ? 'X' : 'O';
        // block if it needs to
        if (mine == 2) {
          $(rows[i]).each(function() {
            if ($(this).is(':empty')) {
              $(this).html(turn);
            }
          })
        }
        
        // or play a random move
        else {
          // force a corner move if human took center
          if (centerTakenFirst) {
            empty = ['1','3','7','9'];
          }
          // otherwise, make a random move
          else {
            empty = [];
            $('.tiles').each(function() {
              if ($(this).is(':empty')) {
                empty.push(parseInt($(this).attr('id')));
              }
            });
          }
          $('#'+String(empty.randEl())).html(turn);
          centerTakenFirst = false;
        }
      }
    } // end of else
    moves++;
    checkWinOrTie();
    xSymbol = !xSymbol;
    turn = xSymbol ? 'X' : 'O';
  } // end of if smartAI 
} // end of opponent

function checkWinOrTie() {
  // check to see if you won
  rows.forEach(function(element) {
    win = 0;
    $(element).each(function() {
      if ($(this).html() == turn) {
        win ++;
      }
    })
    if (win == 3) {
      $(element).css('background-color','gray');
      alert(turn+' wins!');
      champ = (turn == player1) ? $('#p1Score') : $('#oppScore');
      champ.html(parseInt(champ.html())+1);
      $('.tiles').empty();
      $('.tiles').css('background-color','white');
      moves = 0;
    }
  })
        
    // or is it a tie
    if (moves == 9) {
      alert('Shucks, it\'s a tie.');
      $('.tiles').empty();
      $('#tie').html(parseInt($('#tie').html())+1);
      moves = 0;
    }
}

Array.prototype.randEl = function () {
    return this[Math.floor(Math.random() * this.length)]
}
            
          
!
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