css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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="game">
	
	<div class="menu">
		<div class="square x choose-player"><span>x</span></div>
		<span class='or'>or</span>
		<div class="square o choose-player"><span>o</span></div>
	</div>
		
	<div id="0" class="square open-square"><span></span></div>
	<div id="1" class="square open-square"><span></span></div>
	<div id="2" class="square open-square"><span></span></div>
	<div id="3" class="square open-square"><span></span></div>
	<div id="4" class="square open-square"><span></span></div>
	<div id="5" class="square open-square"><span></span></div>
	<div id="6" class="square open-square"><span></span></div>
	<div id="7" class="square open-square"><span></span></div>
	<div id="8" class="square open-square"><span></span></div>
</div>
            
          
!
            
              * {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}

body {
	background-color: #e5ba73;
	background-image: url("data:image/svg+xml,%3Csvg width='64' height='64' viewBox='0 0 64 64' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M8 16c4.418 0 8-3.582 8-8s-3.582-8-8-8-8 3.582-8 8 3.582 8 8 8zm0-2c3.314 0 6-2.686 6-6s-2.686-6-6-6-6 2.686-6 6 2.686 6 6 6zm33.414-6l5.95-5.95L45.95.636 40 6.586 34.05.636 32.636 2.05 38.586 8l-5.95 5.95 1.414 1.414L40 9.414l5.95 5.95 1.414-1.414L41.414 8zM40 48c4.418 0 8-3.582 8-8s-3.582-8-8-8-8 3.582-8 8 3.582 8 8 8zm0-2c3.314 0 6-2.686 6-6s-2.686-6-6-6-6 2.686-6 6 2.686 6 6 6zM9.414 40l5.95-5.95-1.414-1.414L8 38.586l-5.95-5.95L.636 34.05 6.586 40l-5.95 5.95 1.414 1.414L8 41.414l5.95 5.95 1.414-1.414L9.414 40z' fill='%23263238' fill-opacity='0.1' fill-rule='evenodd'/%3E%3C/svg%3E");
	color: #263238;
	font-family: 'Nunito', sans-serif;
	display: flex;
	align-items: center;
	justify-content: center;
	height: 100vh;
	width: 100vw;
}

.menu {
	position: absolute;
	display: flex;
	align-items: center;
	justify-content: space-around;
	background: #263238;
	width: 400px;
	height: 400px;
	margin-left: -10px;
	margin-top: -10px;
	padding: 30px;
	border-radius: 20px;
	text-align: center;
	font-size: 30px;
	color: white;
	z-index: 1000;
}
.menu .square {
	display: inline-block;
	vertical-align: middle;
	color: #263238;
	
}
.menu .square:hover {
	cursor: pointer;
}

.menu .square span {
	bottom: 0;
	user-select: none;
}
.menu .square span:hover{
	cursor: pointer;
}


.game {
	display: flex;
	flex-wrap: wrap;
	justify-content: space-between;
	align-content: space-between;
	min-width: 400px;
	width: 400px;
	height: 400px;
	background: #263238;
	padding: 10px;
	border-radius: 20px;
}

.square {
	width: 120px;
	height: 120px;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
	background: #CFD8DC;
	border-radius: 10px;
	transition: all .4s ease-out;
}

.choose-player:hover {
	box-shadow: 0 0 0 5px #e5ba73;
}

.x {
	background: #E57373;
}

.o {
	background: #29B6F6;
}

.open-square:hover {
	background: white;
	cursor: pointer;
}

.square span {
	user-select: none;
	position: relative;
	bottom: 10px;
	font-size: 150px;
	line-height: 100px;
	text-overflow: clip;
}
.square span:hover {
	cursor: default;
}

            
          
!
            
              /////////////////////////////////////////////
// TicTacToe 
// Designed & Developed by Andrew Creech 2017
/////////////////////////////////////////////

$(document).ready(function(){
	var square = $('.square');
	var menu = $('.menu');
	var player1;
	var player2;
	var winningCombo = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];
	var board = [null,null,null,null,null,null,null,null,null];
	var currentPlayer;
	var gameOver;
	var numberOfMoves = 0;
	
	////////////////
	// choose x or o
	$('.choose-player').click(function(){
		if ($(this).hasClass('x')){
			player1 = 'x';
			player2 = 'o';
			currentPlayer = player1;
			$(this).parent().children('.o, span').hide(500);
		} else {
			player1 = 'o';
			player2 = 'x';
			currentPlayer = player2;
			$(this).parent().children('.x, span').hide(500);
		}
		gameOver = false;
		menu.delay(1500).fadeOut(400);
	});
	
	/////////////////
	// take your turn
	square.click(function(){
		// make sure square is open
		if ($(this).hasClass('open-square') && !gameOver){
			$(this).removeClass('open-square');
			// increment numberOfMoves
			numberOfMoves++;
			// display current move in DOM
			$(this).addClass(currentPlayer);
			$(this).children().html(currentPlayer).hide().fadeIn(400);
			// load current move into the board array
			var currentSquare = parseInt($(this).attr('id'));
			board.splice(currentSquare, 1, currentPlayer);
			// check for the win
			if (checkForWin(currentPlayer, board, winningCombo)){
				gameOver = true;
				// display winner
				menu.children('.x, .o').hide();
				menu.delay(800).fadeIn(400).append('<div class="square winner '+ currentPlayer +'"><span>'+ currentPlayer +'</span></div><span class="winner">wins</span>');
				// call resetGame function
				setTimeout(resetGame, 1300);
			} 
			// no win so check for draw
			else if (numberOfMoves === 9){
				menu.children('.x, .o').hide();
				menu.delay(800).fadeIn(400).append('<div class="winner"><span>It was a draw</span></div>');
				setTimeout(resetGame, 1300);
			}
			// if game is not over... swap currentPlayer
			if (currentPlayer === player1) {
				currentPlayer = player2;
			} else {
				currentPlayer = player1;
			}
		}
	});
	
	///////////////////////
	// checkForWin function
	function checkForWin(currentPlayer, board, winningCombo){
		// loop through sets of winningCombos
		for (i=0; i<winningCombo.length; i++){
			// preset winning to true
			var hasAWinningCombo = true;
			// loop through each sets values
			for (j=0; j<winningCombo[i].length; j++){
				var test = winningCombo[i][j];
				// if no match after all sets, return false & game will continue
				if (board[test] !== currentPlayer){
						hasAWinningCombo = false;
						} 
			}
			// if not false we have a winner, game will end
			if (hasAWinningCombo){
					return true;
			}	
		}
	}
	
	/////////////////////
	// resetGame function
	function resetGame() {
		// start game again
		$('.winner').delay(1500).fadeOut(400);
		$('.choose-player, .or').delay(2500).fadeIn(500);
		// reset boards
		board = [null,null,null,null,null,null,null,null,null];
		$('#0,#1,#2,#3,#4,#5,#6,#7,#8')
			.removeClass('x o')
			.addClass('open-square')
			.empty()
			.html('<span></span>');
		// reset numberOfMoves
		numberOfMoves = 0;
	}
});
            
          
!
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.
Loading ..................

Console