cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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 id="ttt-wrapper">
		<h1>Tic - Tac - Toe</h1>
		<p>Player: <span class="player-num-wins">0</span></p>
		<p>Computer: <span class="computer-num-wins">0</span></p>
		<div class="box-wrapper">
			<!-- AJAX load goes here -->
			<div class="box open">
				<span class="player hide">X</span>
				<span class="computer hide">O</span>
			</div>
			<div class="box open">
				<span class="player hide">X</span>
				<span class="computer hide">O</span>
			</div>
			<div class="box open">
				<span class="player hide">X</span>
				<span class="computer hide">O</span>
			</div>
			<div class="box open">
				<span class="player hide">X</span>
				<span class="computer hide">O</span>
			</div>
			<div class="box open">
				<span class="player hide">X</span>
				<span class="computer hide">O</span>
			</div>
			<div class="box open">
				<span class="player hide">X</span>
				<span class="computer hide">O</span>
			</div>
			<div class="box open">
				<span class="player hide">X</span>
				<span class="computer hide">O</span>
			</div>
			<div class="box open">
				<span class="player hide">X</span>
				<span class="computer hide">O</span>
			</div>
			<div class="box open">
				<span class="player hide">X</span>
				<span class="computer hide">O</span>
			</div>
		</div>
	</div>	
            
          
!
            
              #ttt-wrapper {
	color: #5f5f5f;
	font-family: 'Lucida Sans', Arial, Helvetica, sans-serif;
	padding: 25px 50px;
	background-color: #F1F1F1;
	text-align: center;
  margin: 0 auto;
	h1 {
		margin: 0 auto;
		text-shadow: 1px 1px 1px #BCDEFB;
	}
  .box-wrapper {
    max-width: 600px;
    margin: 0 auto;
  }
	.box {
		display: inline-block;
		background-color: #5f5f5f;
		margin-bottom: 25px;
		margin-right: 25px;
		position: relative;
		border-radius: 8px;
		height: 150px;
		width: 150px;
		span {
			position: absolute;
			font-size: 100px;
			line-height: 145px;
			top: 0;
			left: 0;
			text-shadow: 3px -6px 0px #3F3E3E, 2px -4px 0px #3F3E3E, 1px -2px 0px #3F3E3E;
		}
		span.computer {
			margin-left: 35px;
			color: #BCDEFB;
		}
		span.player {
			margin-left: 42px;
			color: #FFDE90;
		}
	}
	p {
		background-color: #5f5f5f;
		padding: 12px 20px;
		border-radius: 8px;
		color: #f9f9f9;
		font-size: 0.9em;
		margin: 20px auto;
		letter-spacing: 1px;
		width: 125px;
		display: inline-block;
	}
	.player-num-wins {
		color: #FFDE90;
	}
	.computer-num-wins {
		color: #BCDEFB;
	}
	.open:hover {
		cursor: pointer;
	}
	.hide {
		display: none;
	}
	.permanent {
		display: block;
	}
}
/* media queries */
@media screen and (max-width: 700px) {
	#ttt-wrapper {
		.box-wrapper {
			max-width: 450px;
		}
		.box {
			width: 100px;
			height: 100px;
			margin-right: 20px;
			margin-bottom: 20px;
			span {
				font-size: 75px;
				line-height: 100px;
			}
			span.computer {
				margin-left: 20px;
			}
			span.player {
				margin-left: 22px;
			}
		}
		p {
			width: 100px;
		}
	}
}
@media screen and (max-width: 500px) {
	#ttt-wrapper {
		.box-wrapper {
			max-width: 350px;
		}
		.box {
			width: 75px;
			height: 75px;
			margin-right: 15px;
			margin-bottom: 15px;
			span {
				font-size: 50px;
				line-height: 75px;
			}
			span.computer {
				margin-left: 18px;
			}
		}
		p {
			padding: 8px;
		}
	}
}
@media screen and (max-width: 350px) {
	#ttt-wrapper {
		.box-wrapper {
			max-width: 250px;
		}
		.box {
			height: 60px;
			width: 60px;
			margin-right: 10px;
			margin-bottom: 10px;
			span {
				font-size: 30px;
				line-height: 60px;
				text-shadow: 1px -3px 0px #3F3E3E, 2px -4px 0px #3F3E3E, 1px -2px 0px #3F3E3E;
			}
			span.computer {
				margin-left: 18px;
			}
			span.player {
				margin-left: 20px;
			}
		}
		p {
			font-size: 0.8em;
			padding: 6px;
		}
	}
}
            
          
!
            
              $(document).ready(function() {
	//declare global variables
	playerTurn = true;
	var openBoxes;
	var randomBox;
	var allBoxes;
	var playerVictory = 0;
	var computerVictory = 0;
	var someoneWon = false;

	playGame();

	function playGame() {
		$('.open').hover(function() {
			//if player show x
			if (playerTurn) {
				$(this).find('.player').toggleClass('hide');
			}
		});
		
		$('.open').click(function() {
			//if player turn put x and remove 'open' class
			if (playerTurn) {
				$(this).find('.player').addClass('permanent');
				$(this).removeClass('open').addClass('x').unbind();
				//check if won
				var playerWon = checkWin('x', playerWins);

				if(!playerWon) {
					openBoxes = $('.open');
					//pick random open box
					randomBox = openBoxes[Math.floor(Math.random()*openBoxes.length)]
					//show o on that random box, remove 'open' class and binder on box container
					$(randomBox).find('.computer').addClass('permanent');
					$(randomBox).removeClass('open').addClass('o').unbind();
					// check if computer won
					checkWin('o', computerWins);
				}
			}
		});
	}
	
	function computerWins() {
		computerVictory++;
		alert('Computer wins! Let\'s play again');
		$('.computer-num-wins').html(computerVictory);
		playAgain();
	}

	function playerWins() {
		playerVictory++;
		alert('Player wins! Let\'s play again');
		$('.player-num-wins').html(playerVictory);
		playAgain();
	}

	function checkWin(mark, winningFunction) {
		allBoxes = $('.box');
		//check all rows
		for (i = 0; i < 9; i += 3) {
			if ($(allBoxes).eq(i).hasClass(mark) && $(allBoxes).eq(i+1).hasClass(mark) && $(allBoxes).eq(i+2).hasClass(mark)) {
				winningFunction();
				return true;
			}
		}
		//check all columns
		for (i = 0; i < 3; i++) {
			if ($(allBoxes).eq(i).hasClass(mark) && $(allBoxes).eq(i+3).hasClass(mark) && $(allBoxes).eq(i+6).hasClass(mark)) {
				winningFunction();
				return true;
			}
		}
		//check diagonal 
		if (($(allBoxes).eq(0).hasClass(mark) && $(allBoxes).eq(4).hasClass(mark) && $(allBoxes).eq(8).hasClass(mark)) || ($(allBoxes).eq(2).hasClass(mark) && $(allBoxes).eq(4).hasClass(mark) && $(allBoxes).eq(6).hasClass(mark)) ) {
				winningFunction();
				return true;
		}
		//if stalemate play again
		else if ($(allBoxes).length === 0) {
			alert('Stalemate! Play again');
		}
		playerTurn = !playerTurn;
		return false;
	}

	function cleanBoxes() {
		$('.box-wrapper').empty();
		for (i = 0; i < 9; i++) {
			$('.box-wrapper').append('<div class="box open"><span class="player hide">X</span><span class="computer hide">O</span></div>');
		}
	}

	function playAgain() {
		cleanBoxes();
		playerTurn = true;
		playGame();
	}

});


            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console