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 id="wrap" class="container-fluid pull-center">
<div class="hero text-center">
<h1>Tropical Tic-Tac-Toe</h1>
<div class="choice"><h2><strong>Choose Your Playing Piece</strong></h2></div>
</div><!-- hero -->
<div id="columnContent" class="container-fluid pull-center text-center">
<div id="marker" class="pull-center">
<div class="pl">
<img class="img-responsive" src="https://www.konikodes.com/fcc/img/plumeria.png" alt="plumeria flower">
Plumeria
</div><!-- pl -->
<div class="bp"><img class="img-responsive" src="https://www.konikodes.com/fcc/img/bird.png" alt="bird of paradise">
Bird of Paradise</div><!-- bip -->

</div><!-- marker --><br/>

<div id="messages"><h3>
<div id="msg"></div>
<div id="msg1a"></div>
<div id="msg1"></div>
</h3></div>
   <div id="board">

   <div class="row">
   <div id = "0" class = "space bottom right col-xs-4"></div>
   <div id = "1" class = "space bottom right col-xs-4"></div>
   <div id = "2" class = "space bottom col-xs-4"></div>
   </div>

 
   <div class="row">
   <div id = "3" class = "space bottom right col-xs-4"></div>
   <div id = "4" class = "space bottom right col-xs-4"></div>
   <div id = "5" class = "space bottom col-xs-4"></div>
   </div>
   
   <div class="row">
   <div id = "6" class = "space right col-xs-4"></div>
   <div id = "7" class = "space right col-xs-4"></div>
   <div id = "8" class = "space col-xs-4"></div>
   </div>
 
   </div><!-- board -->
<div id="msg2"></div>
</div><!-- columnContent -->

</div><!-- Wrap -->
            
          
!

CSS

            
              body{
    width:100%;
	height:100%;
	margin:0;
	padding:0;
	border:0;
	font-family:Georgia, serif;	
	font-size: 16px;
	color:#fbf2ea !important;	
	background:url(https://www.konikodes.com/fcc/img/waterback.png) no-repeat center center;
    position: fixed;
	background-attachment: fixed;
    background-size:cover;
	-webkit-background-size: cover;
	-o-background-size: cover; 
}
#hero{
    margin:0 auto;
	width:100%;
	height:12.88em;
}
h2{
   font-size:1.8em;
}
h3{
   font-size: 1.2em;
   line-height: 1.3em;
   margin:0;
   padding:0;
}
#marker{
    width:100%;
	height: 1.3em;
	margin:0 auto;
	font-size:1.3em;
}
.pl{
   float:left;
   cursor: pointer;
}

.bp {
   float:right;
   cursor: pointer;

}
#columnContent{
   max-width: 38.75em;
   width:32.5em;
   float:none;
   margin: 0 auto;
   padding: 0;
}
#wrap {
   height:31.25em;
   width:31.25em;
}
#messages{
   clear:both;
   width:31.25em;
   margin:0 auto;
}
#msg, #msg1a, #msg1, #msg2{
    text-align:center;
}
 
#board{ 
   display: table ! important;
   margin:0 auto;
   padding: 0;
   max-width:31.25em;
   height: 100%;
   max-height: 20em;
   width: 100%;
}
#board > .row {
   padding:0;
   margin: 0 auto;
   width: 100%;
   height: 10.84em;
}
.space {
    display: table-cell;
	padding: 0;
    margin: 0;
    vertical-align: middle;
    text-align: center;
    font-size:1.2em;
	height:100%;
	max-height: 10.4em;
    cursor: pointer;
}
.bottom {
   border-bottom: 3px solid #775726;
}
.right {
   border-right: 3px solid #775726;  
}
 
.plm {
    background: url(https://www.konikodes.com/fcc/img/plumeria.png) 50% 50% no-repeat;
    background-color: transparent;
}
.brd {
    background: url(https://www.konikodes.com/fcc/img/bird.png) 50% 50% no-repeat;
    background-color: transparent;
}
.taken {
    background-color: transparent;
	cursor: none;
}
.hilite {
    background-color: #94b5ed;
}

@media only screen and (max-height:620px){
body {
	width:100%;
    height:100%;
	margin:0 auto;
}
#hero{
    margin:0 auto;
	width:100%;
	height:5em;
}
h1{
   font-size:1.7em;
}
h2{
   font-size:1.5em;
}
h3{
   font-size: 1.2em;
   line-height: 1.3em;
   margin:0;
   padding:0;
}
#marker{
	height: 1.2em;
	margin:-3em 0;
	font-size:1.1em;
}
.pl{
   float:left;
   cursor: pointer;
}

.bp {
   float:right;
   cursor: pointer;

}
#columnContent{
   width:100%;
   max-height:30em;
   float:none;
   margin: 0 auto;
   padding: 0;
}
#wrap {
   height:100%;
   width:100%;
}
#board{ 
   display: table ! important;
   margin:0 auto;
   padding: 0;
   height: 100%;
   max-height: 20em;
   width: 100%;
}
#board > .row {
   height: 5.9em;
}
.space {
    display: table-cell;
	padding: 0;
    margin: 0;
    vertical-align: middle;
    text-align: center;
    font-size:1.2em;
	height:100%;
	max-height:5.8em;
    cursor: pointer;
}
.bottom {
   border-bottom: 2px solid #775726;
}
.right {
   border-right: 2px solid #775726;  
}
 
.plm {
    background: url(https://www.konikodes.com/fcc/img/plumeria.png) 40% 40% no-repeat;
    background-color: transparent;
}
.brd {
    background: url(https://www.konikodes.com/fcc/img/bird.png) 40% 40% no-repeat;
    background-color: transparent;
}
.taken {
    background-color: transparent;
	cursor: none;
}
.hilite {
    background-color: #94b5ed;
}
}
            
          
!

JS

            
              $(document).ready(function () {
     'use strict';

var space = document.getElementsByClassName("space"),
  symbX = "X",
	symbO = "O",
	playrVal = -1,
  cmpVal = 1,
	statVal = 0,	
	boardMatrix=[
	// Horizontal Wins
	  [0,1,2],[3,4,5],[6,7,8],
	// Vertical Wins
	  [0,3,6],[1,4,7],[2,5,8],
	// Diagonal Wins
	  [0,4,8],[2,4,6]
	  ],
	status = [0,0,0,0,0,0,0,0,0],
	scores = [0,1,2,3,4,5,6,7,8],
	turnCount = 0,
	gameOn = true,
	place,
	playr = symbO,
	cmp = symbX,
	tie,
	curplayr = playr,
	winner = false,
	winVal = 0;

	
// Initial Game SetUp
function init() {
// click Flower but not spaces
	  $("#marker").prop("disabled", false);
	  $("#board").prop("disabled", true);
	  $("#msg2").text(" ");
	  turnCount = 0;
	  gameOn = true;

	choices();
}	// end init
									
// Initial Choices
function choices(){									
// if bird of paradise clicked
	  $(".bp").on("click", function(){
	  $("#msg").text("You will play with the Bird Of Paradise as your Marker");
	  $("#msg1a").text("The computer will play with the Plumeria Flower as its Marker");
	  $("#msg1").text("Please click on the space to place your marker");  
	  playr = symbX;
	  cmp = symbO;
	  curplayr = playr;
	  console.log("Player is " + playr);
     }); 

// if plumeria clicked
	$(".pl").on("click", function(){
	  $("#msg").text("You will play with the Plumeria Flower as your Marker");
	  $("#msg1a").text("Computer will play with the Bird of Paradise as its Marker");
	  $("#msg1").text("Please click on the space to place your marker");
	  playr = symbO;
	  cmp = symbX;
	  curplayr = playr;
	  console.log("Player is " + playr);
	  });
}  // end choices 

// Attach Marker to GameBoard for Player 
	$(".space").on("click", function(){
	place = $(this).attr("id"); 
	curplayr = playr;
    playGame(curplayr, place);
	}); // end Attach Marker for Player

// Announce Winning Status
function winnr(curplayr, a, d, e, f, x){
           if (turnCount === 9 && winner === false && curplayr === "tie"){
		 	tiesound.play();
			 $("#msg").text("We Have a Tie Game!");	
			 $("#msg1").text("Preparing for next game");
			 console.log("Tie Game!");	
		  }  
	
	  $('*[id="' + d + '"]').addClass("hilite");
		$('*[id="' + e + '"]').addClass("hilite");
		$('*[id="' + f + '"]').addClass("hilite");	
			  
	
 	   if (curplayr === cmp){
	   console.log("Winner is " + curplayr);
		  cmpsound.play();
		  $("#msg").text("Computer is the Winner of This Game!");		  
		 $("#msg1a").text("Preparing for next game");
		 
	     }else if (curplayr === playr){
 	  
		  playrsound.play();
		  $("#msg").text("Congratulations!");
		  $("#msg1a").text("You are the Winner of This Game!");	
		  $("#msg1a").text("Preparing for next game");
		  	  
		  }

			setTimeout(function() {
			reset();
	     }, 8000); 
		gameOn = false;
		return;
		reset();   
} // end winnr()	
	
// Player Placement if GameOn, Board is not full and place is not taken
function playGame(curplayr, place){
	if (gameOn === false){
	reset();
	}
	 if (turnCount === 9){
	 checkFull();
	 } else {
	$("#msg").text("");
	$("#msg1a").text("");
	$("#msg1").text("");
	$("#msg2").text("");
	$(".choice").hide();

// Is space taken?
	if ($('*[id="' + place + '"]').hasClass("taken")){
	$("#msg1a").text("Please choose a space that has not been taken");
	return;
	} else {	
		  if (curplayr === playr){
	       statVal = playrVal;
	     }else{
	        statVal = cmpVal;
	     }
	 status[place] = statVal;
	 
// Then place marker
	     if (curplayr == playr && playr != symbO) {
	         $('*[id="' + place + '"]').addClass("brd taken");
			 scores[place] = ("X");
	         console.log("Space " + place + " has added the Bird of Paradise Class");
			 
	     } else {
	        $('*[id="' + place + '"]').addClass("plm taken"); 
			scores[place] = ("O");
	        console.log("Space " + place + " has added the Plumeria Class");
			
	     } turnCount = turnCount + 1;

		 
	     if (turnCount > 3){
	   winCheck(curplayr);
	 }

	 $("#msg1a").text("");
	 curplayr = cmp;
	 statVal = cmpVal;
	 place = "";
	 comp(curplayr, place);
	 } 
}

}// end playGame()


// Computer Turn to choose placement 
	 function comp(curplayr, place){
	  if (scores[4] === 4){
	  place = 4;
	  } else if (scores[0] === 0){
      place = 0;
	  } else if ((scores[0] !== 0 && scores[2] !== 2 && scores[1] === 1) && status[0] === status[2]  || (scores[4] !== 4 && scores[7] !== 7 && scores[1] === 1) &&
	             status[4] === status[7]){
     console.log("space 1 is " + scores[1]);
	 place = 1;
	 }
 	 else if ((scores[0] !== 0 && scores[1] !== 1 && status[0] === status[1] && scores[2] === 2) || (scores[4] !== 4 && scores[6] !== 6 && status[4] === status[6]  && scores[2] === 2) || 
	          (scores[5] !== 5 && scores[8] !== 8 && status[5] === status[8] && scores[2] === 2) ){
     console.log("space 2 is " + scores[2]);
	 place = 2;
	 } 	
	 else if ((scores[0] !== 0 && scores[3] !== 3 && status[0] === status[3] && scores[6] === 6) || (scores[2] !== 2 && scores[4] !== 4 && status[2] === status[4] && scores[6] === 6) ||
	          (scores[7] !== 7 && scores[8] !== 8 && status[7] === status[8] && scores[6] === 6)) {
     console.log("space 6 is " + scores[6]);
	 place = 6;
	 }
	 else if ((scores[4] !== 4 && scores[0] !== 0 && status[4] === status[0] && scores[8] === 8) || (scores[2] !== 2 && scores[5] !== 5 && status[2] === status[5] && scores[8] === 8) ||
	          (scores[6] !== 6 && scores[7] !== 7 && status[6] === status[7] && scores[8] === 8)) {
     console.log("space 8 scores is " + scores[8]);
	 place = 8;
	 }
	 else if ((scores[0] !== 0 && scores[6] !== 6 && status[0] === status[6] && scores[3] === 3) || (scores[4] !== 4 && scores[5] !== 5 && status[4] === status[5] && scores[3] === 3)){
     console.log("space 3 is " + scores[3]);
	 place = 3;
	 } 
	 else if ((scores[2] !== 2 && scores[8] !== 8 && status[2] === status[8] && scores[5] === 5)|| (scores[4] !== 4 && scores[3] !== 3 && status[4] === status[3] &&  scores[5] === 5)){
     console.log("space 5 is " + scores[5]);
	 place = 5;
	 } 
 	 else if ((scores[1] !== 1 && scores[4] !== 4 && status[1] === status[4] && scores[7] === 7)|| (scores[6] !== 6 && scores[8] !== 8 && status[6] === status[8] && scores[7] === 7)){
     console.log("space 7 is " + scores[7]);
	 place = 7;
	 } 
	 else {
	  for (var i = 0; i < scores.length; i ++){
	   var newList = [];
	 if (scores[i] !== "X" && scores[i] !== "O"){
	 
	  newList.push(i);
	  place = newList[Math.floor(Math.random() * newList.length)];
}
}
}
	     if (cmp != symbO) {
	         setTimeout(function() {
	           $('*[id="' + place + '"]').addClass("brd taken");
			   scores[place] = ("X");
			   winCheck(curplayr, place);
	         }, 2000);
	     } else {
	        setTimeout(function() {
	           $('*[id="' + place + '"]').addClass("plm taken");
			   scores[place] = ("O"); 
			   winCheck(curplayr, place);
	     }, 2000);
	     }
	 status[place] = 1;
	 	turnCount = turnCount + 1;
        winCheck(curplayr, place);
} // end cmpPlace()

// Check if Board is Full
function checkFull(turnCount){
      for (var i = 0; i < 9; i++){
         if (status[i] === 0){
         playGame(place, curplayr);
		  gameOn = true;
		 
		 }else if (status[i] === 9){
		 var winVal = 2;
		 winnr(winVal); 
      } 
   }
} // end checkFull

// Check for Winner
function winCheck(curplayr){
      for (var i = 0; i < boardMatrix.length; i++) {

	   var a, b, c, d, e, f, x;
	   a = scores[boardMatrix[i][0]];
	   b = scores[boardMatrix[i][1]];
	   c = scores[boardMatrix[i][2]];

	   
      if (a === b && a === c && a !== 0){
	 console.log("Current a = " + a);
	   d = [boardMatrix[i][0]];
	   e = [boardMatrix[i][1]];
	   f = [boardMatrix[i][2]];
	   x = status[boardMatrix[i][0]];

	 if (x === -1){
	 curplayr = playr;
	 } 
	 if (x === 1){
	 curplayr = cmp;
	 }  
	  winner = true;
      winnr(curplayr,a, d, e, f,x);
			  
   }else if (turnCount === 9 && winner === false){

	curplayr = "tie";
	a = "t";
	d = "t";
	e = "t";
	f = "t";
	x = "t"
	winnr(curplayr,a, d, e, f, x);
}
	 } 
} // end winCheck()



// Game Over reset Board
function reset(){
	  $("#msg").text("");
	  $("#msg1a").text("");
	  $("#msg1").text("");  
	  $(".hero").show();
	  $(".choice").show();
	  status = [0,0,0,0,0,0,0,0,0];
	  scores = [0,1,2,3,4,5,6,7,8];
	  place = "";
	  curplayr = "";
	  statVal = 0;
	  $(".space").removeClass("brd plm taken hilite");
	  gameOn = false;
     
      init();

} // end reset

init();

}); // end document
            
          
!
999px

Console