Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html lang="en">
<head>
	<meta charset="UTF-8">
	<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous">
	<title>Tic Tac Toe</title>
</head>
<body>
	<div class="board">
		<div class="message-block"> </div>
		<div class="cells">
			<div class="cell c-0-0 d1 h0 v0"></div>
			<div class="cell c-0-1 h0 v1"></div>
			<div class="cell c-0-2 d2 h0 v2"></div>
			<div class="cell c-1-0 h1 v0"></div>
			<div class="cell c-1-1 d1 d2 h1 v1"></div>
			<div class="cell c-1-2 h1 v2"></div>
			<div class="cell c-2-0 d2 h2 v0"></div>
			<div class="cell c-2-1 h2 v1"></div>
			<div class="cell c-2-2 d1 h2 v2"></div>
		</div>
		<div class="modal-block">
			<div class="choice choice-x"></div>
			<p>or</p>
			<div class="choice choice-o"></div>
			<p>?</p>
		</div>
	</div>
</body>
</html>
              
            
!

CSS

              
                html, body, * {
	padding: 0;
	margin: 0;
}
body {
	color: #333;
	background: #eee;
}

.board {
	position: relative;
	padding-bottom: 42px;
	margin: 0 auto;
	margin-top: 100px;
	width: 235px;
	height: 235px;
	font-size: 0;
	background: #555;
}
.cells {
	margin-bottom: 20px;
}
.cell {
	position: relative;
	display: inline-block;
	margin-right: 5px;
	margin-bottom: 5px;
	width: 75px;
	height: 75px;
	border-radius: 5px;
	background: #eee;
	cursor: default;
}
.cell:nth-child(3n) {
	margin-right: 0;
}
.c-0-0 {
	border-radius: 15px 5px 5px 5px;
}
.c-0-2 {
	border-radius: 5px 15px 5px 5px;
}
.c-2-0 {
	border-radius: 5px 5px 5px 15px;
}
.c-2-2 {
	border-radius: 5px 5px 15px 5px;
}
.x:after,
.o:after {
	position: absolute;
	display: block;
	width: 75px;
	height: 75px;
	font-family: "FontAwesome";
	font-size: 78px;
	line-height: 70px;
	text-align: center;
	color: #555;
	text-shadow: 0px 0px 3px rgba(0, 0, 0, 0.5);
	cursor: default;
}
.x:after {
	content: "\f00d";
}
.o:after {
	content: "\f10c";
	font-size: 66px;
	line-height: 75px;
}
.highlighted:after {
	text-shadow: 0px 0px 10px rgba(255, 0, 0, 0.9);
}

.message-block {
	margin: 0 auto;
	padding-bottom: 10px;
	width: 235px;
	font-size: 28px;
	line-height: 32px;
	text-align: center;
	font-weight: 800;
	color: #555;
	text-shadow: 3px 3px 3px rgba(0, 0, 0, 0.25);
	background: #eee;
}

.modal-block {
	position: absolute;
	top: 300px;
	left: 50%;
	margin-left: -148px;
	padding: 20px;
	width: 250px;
	height: 76px;
	font-size: 0;
	text-align: center;
	border-radius: 10px;
	border: 3px solid #666;
	background: #e1e1e1;
}
.modal-block p {
	position: relative;
	top: 12px;
	display: inline-block;
	font-family: "Tahoma";
	font-size: 36px;
	line-height: 20px;
}
.choice {
	position: relative;
	display: inline-block;
	margin-left: 12px;
	margin-right: 12px;
	width: 75px;
	height: 79px;
	vertical-align: middle;
	cursor: pointer;
}
.choice-x:after,
.choice-o:after {
	display: inline-block;
	width: 75px;
	height: 75px;
	font-family: "FontAwesome";
	font-size: 80px;
	line-height: 72px;
	color: #fff;
	text-shadow: 0px 0px 12px rgba(0, 0, 0, 1);
}
.choice-x:after {
	content: "\f00d";
}
.choice-o:after {
	content: "\f10c";
	font-size: 66px;
	line-height: 78px;
}
              
            
!

JS

              
                $(document).ready(function() {
	var board = [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ];
	var ai = 1;
	var player = 2;

	centerBoard();
	$(window).resize(function() {
		centerBoard();
	});

	$('.choice-x').click(function() {
		makeChoice('x');
		startGame();
	});

	$('.choice-o').click(function() {
		makeChoice('o');
		startGame();
	});

	function clickCell() {
		if ($(this).hasClass('x') || $(this).hasClass('o')) {
			return;
		}

		$(this).addClass( player == 1 ? 'x' : 'o' );

		board[Math.floor($('.cell').index($(this)) / 3)][$('.cell').index($(this)) % 3] = player;

		if (getNodeValue(board, true)) {
			finishGame('Player wins');
			return;
		} else if (!generateChildNodes(board, true).length) {
			finishGame('Draw');
			return;
		}

		makeAiTurn();

		if (getNodeValue(board, false)) {
			finishGame('AI wins');
			return;
		} else if (!generateChildNodes(board, false).length) {
			finishGame('Draw');
			return;
		}
	}

	function startGame() {
		if (ai == 1) {
			board[Math.floor(Math.random() * 3)][Math.floor(Math.random() * 3)] = ai;
			refreshBoard(board);
		}
	}

	function finishGame(str) {
		$('.message-block').text(str);
		$('.modal-block').fadeIn(300);
		$('.cell').off('click', clickCell)
							.css('cursor', 'default');
		if (str !== 'Draw') {
			highlight(board);
		}
	}

	function makeAiTurn() {
		var possibleTurns = generateChildNodes(board, false);
		var bestTurns = [];
		var turnValues = [];

		possibleTurns.forEach(function(item) {
			turnValues.push( minimax(item, 5, true) );
		});

		var bestValue = Math.min.apply(null, turnValues);

		for (var i = 0; i < possibleTurns.length; i++) {
			if (turnValues[i] == bestValue) {
				bestTurns.push( possibleTurns[i] );
			}
		}

		board = bestTurns[Math.floor(Math.random() * bestTurns.length)];

		refreshBoard(board);
	}

	function minimax(node, depth, maxPlayer) {
		var childNodes = generateChildNodes(node, maxPlayer);
		var nodeValue = getNodeValue(node);

		if (!depth || nodeValue || !childNodes.length) {
			return nodeValue - depth;
		}

		if (maxPlayer) {
			var bestVal = -9000;

			childNodes.forEach(function(item) {
				var nVal = minimax(item, depth - 1, false);
				bestVal = Math.max(bestVal, nVal);
			});

			return bestVal;
		} else {
			var bestVal = 9000;

			childNodes.forEach(function(item) {
				var nVal = minimax(item, depth - 1, true);
				bestVal = Math.min(bestVal, nVal);
			});

			return bestVal;
		}
	}

	// high == true is used to find win combo for highlighting
	function getNodeValue(node, high) {
		//diagonal check
		if (node[0][0] == ai && node[0][0] == node[1][1] && node[0][0] == node[2][2]) {
			return high ? '.d1' : -10;
		} else if (node[0][0] == player && node[0][0] == node[1][1] && node[0][0] == node[2][2]) {
			return high ? '.d1' : 10;
		}

		if (node[0][2] == ai && node[0][2] == node[1][1] && node[0][2] == node[2][0]) {
			return high ? '.d2' : -10;
		} else if (node[0][2] == player && node[0][2] == node[1][1] && node[0][2] == node[2][0]) {
			return high ? '.d2' : 10;
		}
		//horizontal & vertical check
		for (var i = 0; i < node.length; i++) {
			if (node[i][0] == ai && node[i][0] == node[i][1] && node[i][0] == node[i][2]) {
				return high ? '.h' + i : -10;
			} else if (node[i][0] == player && node[i][0] == node[i][1] && node[i][0] == node[i][2]) {
				return high ? '.h' + i : 10;
			}

			if (node[0][i] == ai && node[0][i] == node[1][i] && node[0][i] == node[2][i]) {
				return high ? '.v' + i : -10;
			} else if (node[0][i] == player && node[0][i] == node[1][i] && node[0][i] == node[2][i]) {
				return high ? '.v' + i : 10;
			}
		}

		return 0;
	}

	function generateChildNodes(node, maxPlayer) {
		var childNodes = [];

		for (var i = 0; i < node.length; i++) {
			for (var j = 0; j < node[i].length; j++) {
				if (node[i][j] === 0) {
					node[i][j] = maxPlayer ? player : ai;
					//JSON used to clone nested arrays
					childNodes.push( JSON.parse(JSON.stringify(node)) );
					node[i][j] = 0;
				}
			}
		}

		return childNodes;
	}

	function highlight(node) {
		$(getNodeValue(node, true)).addClass('highlighted');
	}

	function refreshBoard(node) {
		var flatNode = [].concat.apply([], node);

		$('.cell').each(function(index) {
			if (flatNode[index] == 1 && !$(this).hasClass('x')) {
				$(this).addClass('x');
			} else if (flatNode[index] == 2 && !$(this).hasClass('o')) {
				$(this).addClass('o');
			}
		});
	}

	function clearBoard() {
		$('.cell').each(function(index) {
			$(this).removeClass('x o highlighted');
		});
	}

	function makeChoice(choice) {
		board = [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ];
		clearBoard();

		if (choice == 'x') {
			player = 1;
			ai = 2;
		} else if (choice == 'o') {
			player = 2;
			ai = 1;
		}

		$('.modal-block').fadeOut(300);
		$('.message-block').text(' ');
		$('.cell').click(clickCell)
							.css('cursor', 'pointer');
	}

	function centerBoard() {
		$('.board').css('marginTop', ($(window).innerHeight() - $('.board').height()) / 8);
	}
});
              
            
!
999px

Console