cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <div class="main-wrap">
	<div class="main-wrap__inside">
		<div class="board-instructions">
			Please select a square to place your mark.
		</div>
		<div class="board">
			<div class="board-square top-left">
				<div class="board-square__inside top left"></div>
			</div>
			<div class="board-square top-center">
				<div class="board-square__inside top center"></div>
			</div>
			<div class="board-square top-right">
				<div class="board-square__inside top right"></div>
			</div>
			<div class="board-square middle-left">
				<div class="board-square__inside middle left"></div>
			</div>
			<div class="board-square middle-center">
				<div class="board-square__inside middle center"></div>
			</div>
			<div class="board-square middle-right">
				<div class="board-square__inside middle right"></div>
			</div>
			<div class="board-square bottom-left">
				<div class="board-square__inside bottom left"></div>
			</div>
			<div class="board-square bottom-center">
				<div class="board-square__inside bottom center"></div>
			</div>
			<div class="board-square bottom-right">
				<div class="board-square__inside bottom right"></div>
			</div>
		</div>
	</div>
</div>

<div class="popup">
	<div class="popup__inside">
		<p></p>
		<div class="popup__closer">X</div>
	</div>
</div>
            
          
!
            
              body{
	position: relative;
	padding: 0;
	margin: 0;
}
.main-wrap{
	display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    text-align: center;
    line-height: 1.4;
    min-height: 100vh;
	background-color: #ece9e6;
}
.board-instructions{
	background-color: white;
	width: 332px;
	margin: 4em auto 1em;
	padding: 1em;
}
.board{
	background-color: white;
	width: 348px;
	height: 348px;
	margin: auto;
	display: flex;
	flex-wrap: wrap;
	padding: 8px;
}
.board-square{
	width: 100px;
	height: 100px;
	padding: 8px;
}
.board-square__inside{
	width: 100%;
	height: 100%;
	cursor: pointer;
	transition: 200ms;
	&.x-mark{
		text-align: center;
		&:before{
			content: 'X';
			display: block;
			transform: translateY(-13px);
			color: #0180B5;
			font-size: 570%;
			font-family: sans-serif;
		}
	}
	&.o-mark{
		text-align: center;
		&:before{
			content: 'O';
			display: block;
			transform: translateY(-13px);
			color: #8CA65F;
			font-size: 570%;
			font-family: sans-serif;
		}
	}
}
.top-left,
.middle-left{
	box-shadow: 1px 1px 0px 1px black;
}
.top-center,
.middle-center{
	box-shadow: 1px 1px 0px 1px black;
}
.top-right,
.middle-right{
	box-shadow: 0px 2px 0px 0px black;
}
.bottom-left,
.bottom-center{
	box-shadow: 2px 0px 0px 0px black;
}
.popup{
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	height: 100%;
	width: 100%;
	z-index: -1;
	background: radial-gradient(circle, rgba(#eef2f3, 0.8), rgba(#8e9eab, 0.8));
	p{
		font-size: 30px;
	}
	&__inside{
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		height: 100px;
		border-radius: 3px;
		width: 200px;
		margin: auto;
		background-color: white;
	}
	&__closer{
		position: absolute;
		cursor: pointer;
		top: -14px;
		right: -14px;
		border-radius: 50%;
		z-index: 50;
		width: 28px;
		height: 28px;
		background-color: #834d9b;
		color: white;
		font-size: 140%;
		font-family: sans-serif;
		padding: 4px;
		text-align: center;
		transition: 200ms;
		&:hover{
			background-color: #0180B5;
		}
	}
	&.active{
		z-index: 10;
	}
}
            
          
!
            
              $(document).ready(function() {
	var $allSquares = $(".board-square__inside");
	var count = 0;

	hoverEffect();
	play();

	function play(){
		$allSquares.on('click', function(){
			if($(this).hasClass('x-mark') || $(this).hasClass('o-mark')){
				// don't update the counter, don't add an extra class
			}else{
				hoverEffect();
				count++;
				var turn = '';
				if(count % 2 == 0){
					turn = 'x-mark';
				}else{
					turn = 'o-mark';
				}
				console.log(count);
				console.log(turn);
				$(this)
					.addClass(turn);
				checks();
			}
		});
	}
	function clearBoard(){
		$allSquares
			.removeClass('o-mark')
			.removeClass('x-mark');
	}

	function hoverEffect(){
		$('.board-square__inside').on({
			mouseenter: function() {
				$(this)
					.css('background-color', '#834d9b')
					.css('transition', '300ms');
			},
			mouseleave: function() {
				$(this)
					.css('background-color', 'white')
					.css('transition', '300ms');
			}
		})
	}
	
	function popUp(string){
		// alert(string);
		var $popUp = $('.popup');
		var $closer = $('.popup__closer');
		$popUp.addClass('active');
		$popUp.find('p').text(string);
		
		// remove stuff on click of close
		$closer.on('click', function(){
			$popUp.removeClass('active');
		});

	}
	
 //   )\._.,--....,'``.
 //  /,   _.. \   _\  (`._ ,.
 // `._.-(,_..'--(,_..'`-.;.'
	
	function checks(){
		if (
			$('.bottom').length == $('.bottom.o-mark').length ||
			$('.middle').length == $('.middle.o-mark').length ||
			$('.top').length == $('.top.o-mark').length ||
			$('.left').length == $('.left.o-mark').length ||
			$('.center').length == $('.center.o-mark').length ||
			$('.right').length == $('.right.o-mark').length ||
			// checking diagonals
			(
				$('.top.left').hasClass('o-mark') &&
				$('.middle.center').hasClass('o-mark') &&
				$('.bottom.right').hasClass('o-mark')
			) || (
				$('.top.right').hasClass('o-mark') &&
				$('.middle.center').hasClass('o-mark') &&
				$('.bottom.left').hasClass('o-mark')
			)
		){
			popUp('O wins!');
			clearBoard();
			hoverEffect();
		}else if (
			$('.bottom').length == $('.bottom.x-mark').length ||
			$('.middle').length == $('.middle.x-mark').length ||
			$('.top').length == $('.top.x-mark').length ||
			$('.left').length == $('.left.x-mark').length ||
			$('.center').length == $('.center.x-mark').length ||
			$('.right').length == $('.right.x-mark').length ||
			// checking diagonals
			(
				$('.top.left').hasClass('x-mark') &&
				$('.middle.center').hasClass('x-mark') &&
				$('.bottom.right').hasClass('x-mark')
			) || (
				$('.top.right').hasClass('x-mark') &&
				$('.middle.center').hasClass('x-mark') &&
				$('.bottom.left').hasClass('x-mark')
			)
		) {
			popUp('X Wins!');
			clearBoard();
			hoverEffect();
		}else if(
			( $('.x-mark').length == 5 && $('.o-mark').length == 4 ) ||
			( $('.x-mark').length == 4 && $('.o-mark').length == 5 )
		){
			popUp('Tie Game!');
			clearBoard();
			hoverEffect();
		}
	}
});
            
          
!
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