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 id="counter-container">
	<table>
		<tr id="counter-labels">
			<td>You</td>
			<td>CP</td>
		</tr>
		<tr id="scores-row">
			<td id="player-score">0</td>
			<td id="cp-score">0</td>
		</tr>
	</table>
</div>
<div id="wrapper">	
<h2>tic tac toe</h1>
<table>
	<tr>
		<td id="0"><span class="fa"></span></td>
		<td id="1"><span class="fa"></span></td>
		<td id="2"><span class="fa"></span></td>
	</tr>
	<tr>
		<td id="3"><span class="fa"></span></td>
		<td id="4"><span class="fa"></span></td>
		<td id="5"><span class="fa"></span></td>
	</tr>
	<tr>
		<td id="6"><span class="fa"></span></td>
		<td id="7"><span class="fa"></span></td>
		<td id="8"><span class="fa"></span></td>
	</tr>
</table>
</div>
            
          
!
            
              $background: #EEE;
$default: #78BEC5;
$yellow: #ECAF4F;
$red: #DC685A;
$hover: #3D4250;

@import url('https://fonts.googleapis.com/css?family=Josefin+Sans');

body {
	background-color: $background;
	font-family: "Josefin Sans", sans-serif;
	font-weight: 700;
}

#counter-container {
	position: absolute;
	left: 25px;
	top: 25px;
	width: 150px;
	height: 60px;
	z-index: 100;
	background-color: #E0E0E0;
	border-radius: 10px;
	color: black;
	
	table {
		width: 100%;
		height: 100%;
		vertical-align: middle;
		text-align: center;
		table-layout: fixed;
		td {
			padding: 3px;
		}
			
		td:nth-child(1) {
			border-right: 3px solid #AAA;
		}
		
		#counter-labels {
			line-height: 1em;
		}
		
		#scores-row {
			line-height: 0;
			font-size: 2.3em;
			height: 100%;
		}
	}
}

#wrapper {
	width: 468px;
	margin: 0 auto;
	position: relative;
	top: 50vh;
	transform: translateY(-50%);
	text-align: center;
	
	h2 {
		font-size: 5rem;
		margin-bottom: 0;
		opacity: 0.7;
	}
	
	table {
		height: 468px;
		width: 100%;
		table-layout: fixed;
		border-collapse: separate;
		border-spacing: 12px;
		text-align: center;
		font-size: 6em;

		td {
			height: 140px;
			border-radius: 14px;
			background-color: $default;
			
			span {
				vertical-align: middle;
				color: white;
				opacity: 0.4;
				text-shadow: 2px 2px 4px rgba(0, 0, 0, .2);
			}
		}
		td:hover {
			cursor: pointer;
		}
	}
}

.clicked {
	color: white;
	cursor: default !important;
}

.clicked > span {
	opacity: 1.0;
	display: inline;
}

.red-bg {
	background-color: $red !important;
}

.yellow-bg {
	background-color: $yellow !important;
}

.in {
	animation: in 0.3s ease-out 0s;
	animation-fill-mode: forwards;
}

.out {
	animation: out 0.3s ease-out 0s;
}

@media handheld {
	#wrapper {
		width: 80%;
	}
}

@keyframes in {
	100% {background-color: $hover;}
} 

@keyframes out {
	100% {background-color: $default;}
}

@keyframes red-click {
	100% {
		background-color: $red;
		color: white;
		opacity: 1.0;
	}
}

@keyframes yellow-click {
	100% {
		background-color: $yellow;
		color: white;
		opacity: 1.0;
	}
}
            
          
!
            
              var turn;
var remainingTiles;

// Knuth Shuffle
function shuffle(array) {
	var currentIndex = array.length,
		temporaryValue, randomIndex;

	// While there remain elements to shuffle...
	while (0 !== currentIndex) {
		// Pick a remaining element...
		randomIndex = Math.floor(Math.random() * currentIndex);
		currentIndex -= 1;

		// And swap it with the current element.
		temporaryValue = array[currentIndex];
		array[currentIndex] = array[randomIndex];
		array[randomIndex] = temporaryValue;
	}
}

function checkBoard() {
	function victory(result) {
		switch (result) {
			case "fa-times":
				setTimeout(function() {
					$("#player-score").text(parseInt($("#player-score").text())+1);
					$.notify("You won!", "success");
					initialize();
				}, 500);
				break;
			case "fa-circle-o":
				setTimeout(function() {
					$("#cp-score").text(parseInt($("#cp-score").text())+1);
					$.notify("You lost!", "error");
					initialize();
				}, 500);
				break;
			case "draw":
				setTimeout(function() {
					$.notify("Draw!", "warn");
					initialize();
				}, 500);
				break;
		}
	}

	function checkRow(firstInRow, symbol) {
		if ($("#" + firstInRow).children("span").hasClass(symbol) && $("#" + (firstInRow + 1)).children("span").hasClass(symbol) && $("#" + (firstInRow + 2)).children("span").hasClass(symbol)) {
			return symbol;
		}
	}

	function checkCol(firstInCol, symbol) {
		if ($("#" + firstInCol).children("span").hasClass(symbol) && $("#" + (firstInCol + 3)).children("span").hasClass(symbol) && $("#" + (firstInCol + 6)).children("span").hasClass(symbol)) {
			return symbol;
		}
	}

	if (checkRow(0, "fa-times") == "fa-times") {
		victory("fa-times");
		return;
	}
	if (checkRow(0, "fa-circle-o") == "fa-circle-o") {
		victory("fa-circle-o");
		return;
	}
	if (checkRow(3, "fa-times") == "fa-times") {
		victory("fa-times");
		return;
	}
	if (checkRow(3, "fa-circle-o") == "fa-circle-o") {
		victory("fa-circle-o");
		return;
	}
	if (checkRow(6, "fa-times") == "fa-times") {
		victory("fa-times");
		return;
	}
	if (checkRow(6, "fa-circle-o") == "fa-circle-o") {
		victory("fa-circle-o");
		return;
	}

	if (checkCol(0, "fa-times") == "fa-times") {
		victory("fa-times");
		return;
	}
	if (checkCol(0, "fa-circle-o") == "fa-circle-o") {
		victory("fa-circle-o");
		return;
	}
	if (checkCol(1, "fa-times") == "fa-times") {
		victory("fa-times");
		return;
	}
	if (checkCol(1, "fa-circle-o") == "fa-circle-o") {
		victory("fa-circle-o");
		return;
	}
	if (checkCol(2, "fa-times") == "fa-times") {
		victory("fa-times");
		return;
	}
	if (checkCol(2, "fa-circle-o") == "fa-circle-o") {
		victory("fa-circle-o");
		return;
	}

	// back diag X
	if ($("#0").children("span").hasClass("fa-times") && $("#4").children("span").hasClass("fa-times") && $("#8").children("span").hasClass("fa-times")) {
		victory("fa-times");
		return;
	}
	// back diag O
	if ($("#0").children("span").hasClass("fa-circle-o") && $("#4").children("span").hasClass("fa-circle-o") && $("#8").children("span").hasClass("fa-circle-o")) {
		victory("fa-circle-o");
		return "move";
	}

	// forward diag X
	if ($("#2").children("span").hasClass("fa-times") && $("#4").children("span").hasClass("fa-times") && $("#6").children("span").hasClass("fa-times")) {
		victory("fa-times");
		return;
	}
	// forward diag O
	if ($("#2").children("span").hasClass("fa-circle-o") && $("#4").children("span").hasClass("fa-circle-o") && $("#6").children("span").hasClass("fa-circle-o")) {
		victory("fa-circle-o");
		return;
	}

	var allClicked = true;
	for (var i = 0; i < 9; i++) {
		if (!($("#" + i).hasClass("clicked"))) {
			allClicked = false;
		}
	}
	if (allClicked) {
		victory("draw");
	};

	return "move";
}

function initialize() {
	$("#wrapper td").removeClass("clicked out in").removeAttr("style");
	$("#wrapper td").children("span").removeClass("fa-times fa-circle-o");
	turn = 'X';
	$(this).unbind('mouseenter mouseleave click');
	remainingTiles = [0, 1, 2, 3, 4, 5, 6, 7, 8];
	shuffle(remainingTiles); // shuffle  the order of computer tile plays

	// When hovering over a tile...
	$("#wrapper td").hover(function() {
			// Animate cell change
			if (!($(this).hasClass("in"))) {
				$(this).removeClass("out").addClass("in");
			}

			if (turn === 'X') {
				$(this).children("span").removeClass("fa-circle-o").addClass("fa-times");
			}

			// Fade in the symbol
			$(this).children("span").fadeIn(50);
		},
		// When the cursor leaves the tile...
		function() {
			if (!($(this).hasClass("out"))) {
				$(this).removeClass("in").addClass("out");
			}
			$(this).children("span").removeClass("fa-times fa-circle-o");
			// Animate cell change
			$(this).children("span").fadeOut(50);
		});

	// When a tile gets clicked...
	$("#wrapper td").click(function() {
		if (!$(this).hasClass("clicked")) {
			if (turn == 'X') { // It's the player's turn and their tile hasn't already been selected
				var id = parseInt($(this).attr('id')); // find the ID (0-9) of the current tile
				remainingTiles.splice(remainingTiles.indexOf(id), 1); // remove that ID from the set
				$(this).css("animation", "red-click 0.2s ease-in 0s 1 forwards");
				$(this).children("span").removeAttr("style");
				$(this).addClass("clicked");
				$(this).unbind('mouseenter mouseleave');
				if (checkBoard() == "move") {
					turn = 'O'; // set the game to the computer's turn
					id = remainingTiles.pop();
					$("#" + id).click();
				}
			} else {
				$(this).css("animation", "yellow-click 0.2s ease-in 0s 1 forwards");
				$(this).children("span").removeAttr("style").removeClass("fa-times").addClass("fa-circle-o").fadeIn(50);
				$(this).addClass("clicked");
				$(this).unbind('mouseenter mouseleave');
				if (checkBoard() != "move") {
					return;
				}
				turn = 'X'; // set the game to the player's turn
			}
		}
	});
}

$(document).ready(function() {
	initialize();
});
            
          
!
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