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.

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 mr-auto ml-auto">
  <h1 class="text-center" id="headLine">Tic Tac Toe</h1>
  <div id="gameArea" class="addShadow">
<div class="row">
  <div id="one" class="box" >
    <div class="boxInside odd">
      <p class="xo"><span id="xoOne"></span></p>
    </div>
  </div>
  <div id="two" class="box" >
    <div class="boxInside even">
      <p class="xo"><span id="xoTwo"></span></p>
    </div>
  </div>
  <div id="three" class="box" >
    <div class="boxInside odd">
      <p class="xo"><span id="xoThree"></span></p>
    </div>
  </div>
  </div>
<div class="row">
  <div id="eight" class="box" >
    <div class="boxInside even">
      <p class="xo"><span id="xoEight"></span></p>
    </div>
  </div>
  <div id="zero" class="box" >
    <div class="boxInside odd">
      <p class="xo"><span id="xoZero"></span></p>
    </div>
  </div>
  <div id="four" class="box" >
    <div class="boxInside even">
      <p class="xo"><span id="xoFour"></span></p>
    </div>
  </div>
  </div>
  <div class="row">
  <div id="seven" class="box" >
    <div class="boxInside odd">
      <p class="xo"><span id="xoSeven"></span></p>
    </div>
    </div>
  <div id="six" class="box" >
    <div class="boxInside even">
      <p class="xo"><span id="xoSix"></span></p>
    </div>
    </div>
  <div id="five" class="box" >
    <div class="boxInside odd">
      <p class="xo"><span id="xoFive"></span></p>
    </div>
    </div>
  </div>
     <!-- Button trigger modal -->
    <div style="padding: 20px 10px;">
    <button id="aiMode" type="button" class="btn btn-secondary">
  1 Player
</button>
<button id="restart" type="button" style="float:right;" class="btn btn-secondary" data-toggle="modal" data-target="#chooseSide">
  Start/Restart Game
</button>
      </div>
    
  </div>





<!-- Modal -->
<div class="modal fade" id="chooseSide" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header" style="padding-left: 100px;">
        <h5 class="modal-title mr-auto ml-auto" id="exampleModalLabel">Choose Side ( X goes first! )</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
<!--       <div class="modal-body">
        ...
      </div> -->
      <div class="modal-footer">
        <button type="button" id="choseX" class="btn btn-danger ml-auto" data-dismiss="modal">X</button>
        <button type="button" id="choseO" class="btn btn-primary mr-auto" data-dismiss="modal">O</button>
      </div>
    </div>
  </div>
</div>


            
          
!

CSS

            
              
body{
  background-color: #DDD;
}
.container{
  padding-top: 50px;

}
#gameArea{
  background-color: rgb(90, 90, 90);
  margin-left: auto;
    margin-right: auto;
}
p{
  margin: 0px;
  color: black;
  
  
}
.box{
  cursor: pointer;
}
#headLine{
  min-width: 300px;
}

.addShadow {
  -webkit-box-shadow: 0px 2px 8px 0.3px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0px 2px 8px 0.3px rgba(0, 0, 0, 0.75);
  box-shadow: 0px 2px 8px 0.3px rgba(0, 0, 0, 0.75);
}

@media (max-width: 575.98px) {
  #gameArea{
    width: 300px;
    height: 300px;
    
  }
  .box{
    width: 100px;
    height: 100px;
    float: left;
  }
  .row{
  margin: 0px;
}
  .boxInside{
    height: 98px; 
    width: 98px;
    margin: 1px 0px 0px 1px;
  }
  .xo{
  font-size: 60px;
  text-align: center;
    
}
  
}

@media (min-width: 576px) and (max-width: 767.98px) {
  #gameArea{
    width: 450px;
    height: 450px;
  }
  .box{
    width: 150px;
    height: 150px;
    float: left;
  }
  .row{
  margin: 0px;
}
  .boxInside{
    height: 148px; 
    width: 148px;
    margin: 1px 0px 0px 1px;
  }
  .xo{
  font-size: 80px;
  text-align: center;
}
  h1{
    font-size: 60px;
    
  }
  
}

@media (min-width: 768px){
  #gameArea{
    width: 600px;
    height: 600px;
  }
  .box{
    width: 200px;
    height: 200px;
    float: left;
  }
  .row{
  margin: 0px;
}
  .boxInside{
    height: 198px; 
    width: 198px;
    margin: 1px 0px 0px 1px;
  }
  .xo{
  font-size: 110px;
  text-align: center;
}
  h1{
    font-size: 60px;
    
  }
  
}





.odd{
  background-color: 		#32CD32;
/*   background-color: #008080;  */
}
.even{
  background-color: #DDD;
/*   background-color: #808080;  */
}

            
          
!

JS

            
              var originalList = ["123", "345", "567", "781", "105", "307", "206", "804"];
var gamePlay = [
["#xoZero", 0, ""],
["#xoOne", 1, ""],
["#xoTwo", 2, ""],
["#xoThree", 3, ""],
["#xoFour", 4, ""],
["#xoFive", 5, ""],
["#xoSix", 6, ""],
["#xoSeven", 7, ""],
["#xoEight", 8, ""]
];
var choseFromOriginal = [0,1,2,3,4,5,6,7,8];
var list = _.cloneDeep(originalList);
var ourGame = _.cloneDeep(gamePlay);
var choseFrom = _.cloneDeep(choseFromOriginal);
var aiMode = true;
var playerIs = "✕";
var aiIs = "◯";
var count = 0;


function drawBoard() {
  for (var k = 0; k < ourGame.length; k++) {
  	$(ourGame[k][0]).text(ourGame[k][2]);
  }
  winAndBlock();
}

function winAndBlock() {
	if (aiIs === "✕") {
		for (var e = 0; e < list.length; e++) {
			if (list[e].match(/✕{1}([^✕◯a-z]){1}✕{1}|✕{2}([^✕◯a-z]){1}|([^✕◯a-z]){1}✕{2}/g) !== null) {
				return list[e].match(/\d{1}/g);
		}
	}
		for (var j = 0; j < list.length; j++) {
			if (list[j].match(/◯{1}([^✕◯a-z]){1}◯{1}|◯{2}([^✕◯a-z]){1}|([^✕◯a-z]){1}◯{2}/g) !== null) {
				return list[j].match(/\d{1}/g);
		}
	}

} else if (aiIs === "◯") {
	for (var p = 0; p < list.length; p++) {
		if (list[p].match(/◯{1}([^✕◯a-z]){1}◯{1}|◯{2}([^✕◯a-z]){1}|([^✕◯a-z]){1}◯{2}/g) !== null) {
			return list[p].match(/\d{1}/g);
	}
}
	for (var d = 0; d < list.length; d++) {
		if (list[d].match(/✕{1}([^✕◯a-z]){1}✕{1}|✕{2}([^✕◯a-z]){1}|([^✕◯a-z]){1}✕{2}/g) !== null) {
			return list[d].match(/\d{1}/g);
	}
}
}

return;

}
function place(number){
	ourGame[number][2] = aiIs;
	pushToList(ourGame[number][1], aiIs);
	count++;
	drawBoard();
	checkWon();
  
}

function aiMove() {
  var unit;
  
	if(count === 0 && aiIs === "✕"){
		place(0);
	}
	else if (typeof winAndBlock() === "object") {
		unit = parseInt(winAndBlock());
    place(unit);
	}
  else{place(choseFrom[Math.floor(Math.random() * choseFrom.length)]);}
    
  }
  


function pushToList(number, symbol) {
  if(choseFrom.indexOf(number) > -1){choseFrom.splice(choseFrom.indexOf(number),1);}
  console.log(count);
	for (var t = 0; t < list.length; t++) {
		for (var u = 0; u < list[t].length; u++) {
			list[t] = list[t].replace(number, symbol);
		}
	}
}
function checkWon() {
	for (var e = 0; e < list.length; e++) {
		if (list[e].match(/[◯]{3}|[✕]{3}/g) !== null) {
			drawBoard();
			bootbox.alert(list[e][0] + " has WON!", clearAll);
      return;
		}
	}
  if(count === 9){
    bootbox.alert("Cat's Game", clearAll);
    return;
  }
  return false;
}

function clearAll() {
  list = _.cloneDeep(originalList);
  ourGame = _.cloneDeep(gamePlay); // This is for a deep copy
  choseFrom = _.cloneDeep(choseFromOriginal);
  // console.log(choseFrom + " || " + choseFromOriginal);
  playerIs = "✕";
  aiIs = "◯";
  count = 0;
  drawBoard();
}

function playerMove(place, numberPressed) {
  ourGame[place][2] = playerIs;
  pushToList(gamePlay[place][1], playerIs);
  count++;
  drawBoard();
  checkWon();
  if (checkWon() === false && aiMode === true) {
  	aiMove();
  } else if (aiMode === false) {
  	if (playerIs === "✕") {
  		playerIs = "◯";
      aiIs = "✕";
  	} else if (playerIs === "◯") {
  		playerIs = "✕";
      aiIs = "◯";
  	}
  }
}

$("#restart").click(function() {
	clearAll();
});

$("#choseX").click(function() {
	playerIs = "✕";
	if (aiMode === true) {
		aiIs = "◯";
	}
});
$("#choseO").click(function() {
	playerIs = "◯";
	if (aiMode === true) {
		aiIs = "✕";
		aiMove();
	}
});

$("#one").click(function() {
	if ($("#xoOne").text() === "") {
		playerMove(1, "#one");
	}
});
$("#two").click(function() {
	if ($("#xoTwo").text() === "") {
		playerMove(2, "#two");
	}
});
$("#three").click(function() {
	if ($("#xoThree").text() === "") {
		playerMove(3, "#three");
	}
});
$("#eight").click(function() {
	if ($("#xoEight").text() === "") {
		playerMove(8, "#eight");
	}
});
$("#zero").click(function() {
	if ($("#xoZero").text() === "") {
		playerMove(0, "#zero");
	}
});
$("#four").click(function() {
	if ($("#xoFour").text() === "") {
		playerMove(4, "#four");
	}
});
$("#seven").click(function() {
	if ($("#xoSeven").text() === "") {
		playerMove(7, "#seven");
	}
});
$("#six").click(function() {
	if ($("#xoSix").text() === "") {
		playerMove(6, "#six");
	}
});
$("#five").click(function() {
	if ($("#xoFive").text() === "") {
		playerMove(5, "#five");
	}
});

$("#aiMode").click(function() {
	if (aiMode === false) {
    aiMode = true;
		$("#aiMode").text("1 Player");
	}
  else if (aiMode === true) {
    aiMode = false;
		$("#aiMode").text("2 Players");
	}
});
            
          
!
999px

Console