Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

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

HTML

              
                <div class="container">
  <h1 class="text-center">Tic Tac Toe Game</h1>
  <!-- Modal -->
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h2 class="modal-title text-center" id="myModalLabel">Choose your side!</h2>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <div class="radio">
              <label>
            <input name="optionsRadios" id="optionsRadios1" value="option1"  type="radio">
            <strong>Play as X</strong>
          </label>
            </div>
            <div class="radio">
              <label>
            <input name="optionsRadios" id="optionsRadios2" value="option2"  type="radio">
            <strong>Play as O</strong>
          </label>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" id="closemodal" class="btn btn-primary" data-dismiss="modal">Start playing!</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  <button id="restart" onClick="history.go(0)" class="btnreset center-block btn  btn-success">Play Again!</button>
  <div class="plansza center-block">
    <div class="row btn-group btn-group-justified">
      <div class="col-md-4 col-xs-4">
        <div id="box-1" class="box text-center">
        </div>
      </div>
      <div class="col-md-4 col-xs-4">
        <div id="box-2" class="box text-center">
          <p class="text-center"></p>
        </div>
      </div>
      <div class="col-md-4 col-xs-4">
        <div id="box-3" class="box text-center">
          <h1 class="text-center"></h1>
        </div>
      </div>
    </div>
    <div class="row btn-group btn-group-justified">
      <div class="col-md-4 col-xs-4">
        <div id="box-4" class="box text-center">
        </div>
      </div>
      <div class="col-md-4 col-xs-4">
        <div id="box-5" class="box text-center">
        </div>
      </div>
      <div class="col-md-4 col-xs-4">
        <div id="box-6" class="box text-center">
        </div>
      </div>
    </div>
    <div class="row btn-group btn-group-justified">
      <div class="col-md-4 col-xs-4">
        <div id="box-7" class="box text-center">
        </div>
      </div>
      <div class="col-md-4 col-xs-4">
        <div id="box-8" class="box text-center">
        </div>
      </div>
      <div class="col-md-4 col-xs-4">
        <div id="box-9" class="box text-center">
        </div>
      </div>

    </div>
  </div>

</div>
<br>
<br>
<p class="text-center">Made by <a href="https://www.freecodecamp.com/yerevin">Mateusz Skulski</a></p>
              
            
!

CSS

              
                body {
  background-color: #ffffb3;
}

h1 {
  font-size: 4em;
  color: black;
}

.plansza {
  width: 450px;
  height: 450px;
  background-color: #FFFFFF;
  border: 3px solid;
}

.X {
  background-color: #004d00 !important;
  color: #004d00;
}

.O {
  background-color: #990000 !important;
}

.box {
  width: 150px;
  height: 150px;
  background-color: #FFFFFF;
  border: 4px solid;
  font-size: 4em;
  color: black;
}

.btnreset {
  width: 450px;
  height: 50px;
  opacity: 0;
  margin-bottom: 10px;
}
              
            
!

JS

              
                function checkWinXOrLoseX() {
  if ((($('#box-1').hasClass("X")) && ($('#box-2').hasClass("X")) && ($('#box-3').hasClass("X"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-4').hasClass("X")) && ($('#box-1').hasClass("X")) && ($('#box-7').hasClass("X"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-9').hasClass("X")) && ($('#box-5').hasClass("X")) && ($('#box-1').hasClass("X"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-2').hasClass("X")) && ($('#box-5').hasClass("X")) && ($('#box-8').hasClass("X"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-3').hasClass("X")) && ($('#box-6').hasClass("X")) && ($('#box-9').hasClass("X"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-4').hasClass("X")) && ($('#box-5').hasClass("X")) && ($('#box-6').hasClass("X"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-8').hasClass("X")) && ($('#box-9').hasClass("X")) && ($('#box-7').hasClass("X"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-5').hasClass("X")) && ($('#box-3').hasClass("X")) && ($('#box-7').hasClass("X"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-1').hasClass("O")) && ($('#box-2').hasClass("O")) && ($('#box-3').hasClass("O"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-1').hasClass("O")) && ($('#box-4').hasClass("O")) && ($('#box-7').hasClass("O"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-1').hasClass("O")) && ($('#box-5').hasClass("O")) && ($('#box-9').hasClass("O"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-2').hasClass("O")) && ($('#box-5').hasClass("O")) && ($('#box-8').hasClass("O"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-3').hasClass("O")) && ($('#box-6').hasClass("O")) && ($('#box-9').hasClass("O"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-4').hasClass("O")) && ($('#box-5').hasClass("O")) && ($('#box-6').hasClass("O"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-7').hasClass("O")) && ($('#box-8').hasClass("O")) && ($('#box-9').hasClass("O"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-3').hasClass("O")) && ($('#box-5').hasClass("O")) && ($('#box-7').hasClass("O"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  }
};

function checkWinXOrLoseO() {
  if ((($('#box-1').hasClass("O")) && ($('#box-2').hasClass("O")) && ($('#box-3').hasClass("O"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-4').hasClass("O")) && ($('#box-1').hasClass("O")) && ($('#box-7').hasClass("O"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-9').hasClass("O")) && ($('#box-5').hasClass("O")) && ($('#box-1').hasClass("O"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-2').hasClass("O")) && ($('#box-5').hasClass("O")) && ($('#box-8').hasClass("O"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-3').hasClass("O")) && ($('#box-6').hasClass("O")) && ($('#box-9').hasClass("O"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-4').hasClass("O")) && ($('#box-5').hasClass("O")) && ($('#box-6').hasClass("O"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-8').hasClass("O")) && ($('#box-9').hasClass("O")) && ($('#box-7').hasClass("O"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-5').hasClass("O")) && ($('#box-3').hasClass("O")) && ($('#box-7').hasClass("O"))) === true) {
    alert('YOU WON!');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-1').hasClass("X")) && ($('#box-2').hasClass("X")) && ($('#box-3').hasClass("X"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-1').hasClass("X")) && ($('#box-4').hasClass("X")) && ($('#box-7').hasClass("X"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-1').hasClass("X")) && ($('#box-5').hasClass("O")) && ($('#box-9').hasClass("X"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-2').hasClass("X")) && ($('#box-5').hasClass("X")) && ($('#box-8').hasClass("X"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-3').hasClass("X")) && ($('#box-6').hasClass("X")) && ($('#box-9').hasClass("X"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-4').hasClass("X")) && ($('#box-5').hasClass("X")) && ($('#box-6').hasClass("X"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-7').hasClass("X")) && ($('#box-8').hasClass("X")) && ($('#box-9').hasClass("X"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  } else if ((($('#box-3').hasClass("X")) && ($('#box-5').hasClass("X")) && ($('#box-7').hasClass("X"))) === true) {
    alert('YOU LOSE! TRY AGAIN :)');
    $('#restart').css('opacity', '1');
  }
};

function playAsX() {
  var array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  $("#box-1").click('#box-1', function() {
    var textField = $('#box-1');
    $('#box-1').addClass('X');
    textField.text('X');
    var index = array.indexOf(1);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-1").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('O');
    textField2.text('O');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseX();
  });

  $("#box-2").click('#box-2', function() {
    var textField = $('#box-2');
    $('#box-2').addClass('X');
    textField.text('X');
    var index = array.indexOf(2);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-2").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('O');
    textField2.text('O');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseX();
  });
  $("#box-3").click('#box-3', function() {
    var textField = $('#box-3');
    $('#box-3').addClass('X');
    textField.text('X');
    var index = array.indexOf(3);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-3").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('O');
    textField2.text('O');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseX();
  });
  $("#box-4").click('#box-4', function() {
    var textField = $('#box-4');
    $('#box-4').addClass('X');
    textField.text('X');
    var index = array.indexOf(4);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-4").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('O');
    textField2.text('O');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseX();
  });
  $("#box-5").click('#box-5', function() {
    var textField = $('#box-5');
    $('#box-5').addClass('X');
    textField.text('X');
    var index = array.indexOf(5);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-5").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('O');
    textField2.text('O');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseX();
  });
  $("#box-6").click('#box-6', function() {
    var textField = $('#box-6');
    $('#box-6').addClass('X');
    textField.text('X');
    var index = array.indexOf(6);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-6").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('O');
    textField2.text('O');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseX();
  });
  $("#box-7").click('#box-7', function() {
    var textField = $('#box-7');
    $('#box-7').addClass('X');
    textField.text('X');
    var index = array.indexOf(7);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-7").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('O');
    textField2.text('O');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseX();
  });
  $("#box-8").click('#box-8', function() {
    var textField = $('#box-8');
    $('#box-8').addClass('X');
    textField.text('X');
    var index = array.indexOf(8);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-8").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('O');
    textField2.text('O');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseX();
  });
  $("#box-9").click('#box-9', function() {
    var textField = $('#box-9');
    $('#box-9').addClass('X');
    textField.text('X');
    var index = array.indexOf(9);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-9").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('O');
    textField2.text('O');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseX();
  });
};

function playAsO() {
  var array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  $("#box-1").click('#box-1', function() {
    var textField = $('#box-1');
    $('#box-1').addClass('O');
    textField.text('O');
    var index = array.indexOf(1);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-1").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('X');
    textField2.text('X');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseO();
  });

  $("#box-2").click('#box-2', function() {
    var textField = $('#box-2');
    $('#box-2').addClass('O');
    textField.text('O');
    var index = array.indexOf(2);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-2").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('X');
    textField2.text('X');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseO();
  });
  $("#box-3").click('#box-3', function() {
    var textField = $('#box-3');
    $('#box-3').addClass('O');
    textField.text('O');
    var index = array.indexOf(3);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-3").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('X');
    textField2.text('X');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseO();
  });
  $("#box-4").click('#box-4', function() {
    var textField = $('#box-4');
    $('#box-4').addClass('O');
    textField.text('O');
    var index = array.indexOf(4);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-4").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('X');
    textField2.text('X');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseO();
  });
  $("#box-5").click('#box-5', function() {
    var textField = $('#box-5');
    $('#box-5').addClass('O');
    textField.text('O');
    var index = array.indexOf(5);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-5").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('X');
    textField2.text('X');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseO();
  });
  $("#box-6").click('#box-6', function() {
    var textField = $('#box-6');
    $('#box-6').addClass('O');
    textField.text('O');
    var index = array.indexOf(6);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-6").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('X');
    textField2.text('X');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseO();
  });
  $("#box-7").click('#box-7', function() {
    var textField = $('#box-7');
    $('#box-7').addClass('O');
    textField.text('O');
    var index = array.indexOf(7);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-7").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('X');
    textField2.text('X');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseO();
  });
  $("#box-8").click('#box-8', function() {
    var textField = $('#box-8');
    $('#box-8').addClass('O');
    textField.text('O');
    var index = array.indexOf(8);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-8").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('X');
    textField2.text('X');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseO();
  });
  $("#box-9").click('#box-9', function() {
    var textField = $('#box-9');
    $('#box-9').addClass('O');
    textField.text('O');
    var index = array.indexOf(9);
    if (index > -1) {
      array.splice(index, 1);
      $("#box-9").unbind("click");
    }
    var rand = array[Math.floor(Math.random() * array.length)];
    var textField2 = $('#box-' + rand);
    $('#box-' + rand).addClass('X');
    textField2.text('X');
    index = array.indexOf(rand);
    if (index > -1) {
      array.splice(index, 1);
      $('#box-' + rand).unbind("click");
    }
    checkWinXOrLoseO();
  });
};

$(document).ready(function() {
  $("#myModal").modal('show');
  $('#closemodal').click(function() {
    if ($('#optionsRadios1').is(':checked')) {
      playAsX();
    }

    if ($('#optionsRadios2').is(':checked')) {
      playAsO();
    }
    if (!$("input[name='optionsRadios']").is(':checked')) {
      alert('Nothing is checked! Refresh your website and pick a side!');
    }
  });
});
              
            
!
999px

Console