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

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.

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

              
                .ttt__container
	h2.ttt__title Let's play Tic Tac Toe!
	.ttt__square(data-location="top-left")
	.ttt__square(data-location="top-center")
	.ttt__square(data-location="top-right")
	.ttt__square(data-location="center-left")
	.ttt__square(data-location="center-center")
	.ttt__square(data-location="center-right")
	.ttt__square(data-location="bottom-left")
	.ttt__square(data-location="bottom-center")
	.ttt__square(data-location="bottom-right")
	h4.ttt__ending
              
            
!

CSS

              
                .ttt__container {
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	grid-template-rows: repeat(5, 1fr);
	grid-gap: 5px;
	width: 100vmin;
	height: 100vmin;
	padding: 2rem;
	box-sizing: border-box;
	margin: 0 auto;
	
	color: white;
	
	background-color: black;
}

.ttt__container--victory,
.ttt__container--failure,
.ttt__container--draw {
	
	.ttt__square { opacity: 0.4; }
}

.ttt__container--victory { background-color: green; }

.ttt__container--failure { background-color: red; }

.ttt__container--draw { background-color: gray; }

.ttt__title {
	grid-row: 1;
	grid-column: 1 / span 3;
	
	text-align: center;
}

.ttt__ending {
	grid-row: 5;
	grid-column: 1 / span 3;
	
	text-align: center;
}

.ttt__square {
	position: relative;
	
	background-color: white;
	background-size: contain;
	background-position: center;
	background-repeat: no-repeat;
}

.ttt__square--x {
	background-image: url("https://images.landofnod.com/is/image/LandOfNod/Letter_Giant_Enough_X_236462_LL/$web_zoom$&wid=550&hei=550&/130831065659/not-giant-enough-letter-x.jpg");
}

.ttt__square--o {
	background-image: url("http://www.drodd.com/images15/letter-o29.jpg");
}
              
            
!

JS

              
                function tictactoe_game(patternId) {
	let board = $("#" + patternId);

	// Consistent component variables
	const v = {
		match_ongoing: true,
		
		board: [],
		
		board_full: [
			"top-left",
			"top-center",
			"top-right",
			"center-left",
			"center-center",
			"center-right",
			"bottom-left",
			"bottom-center",
			"bottom-right"
		],

		board_wins: [
			["top-left", "top-center", "top-right"],
			["center-left", "center-center", "center-right"],
			["bottom-left", "bottom-center", "bottom-right"],
			["top-left", "center-left", "bottom-left"],
			["top-center", "center-center", "bottom-center"],
			["top-right", "center-right", "bottom-right"],
			["top-left", "center-center", "bottom-right"],
			["top-right", "center-center", "bottom-left"]
		],

		board_checks: [
			{
				setup: ["top-center", "top-right"],
				move: "top-left"
			},
			{
				setup: ["top-center", "top-right"],
				move: "top-left"
			},
			{
				setup: ["top-left", "top-center"],
				move: "top-right"
			},
			{
				setup: ["center-center", "center-right"],
				move: "center-left"
			},
			{
				setup: ["center-left", "center-right"],
				move: "center-center"
			},
			{
				setup: ["center-left", "center-center"],
				move: "center-right"
			},
			{
				setup: ["bottom-center", "bottom-right"],
				move: "bottom-left"
			},
			{
				setup: ["bottom-left", "bottom-right"],
				move: "bottom-center"
			},
			{
				setup: ["bottom-left", "bottom-center"],
				move: "bottom-right"
			},
			{
				setup: ["center-left", "bottom-left"],
				move: "top-left"
			},
			{
				setup: ["top-left", "bottom-left"],
				move: "center-left"
			},
			{
				setup: ["top-left", "center-left"],
				move: "bottom-left"
			},
			{
				setup: ["center-center", "bottom-center"],
				move: "top-center"
			},
			{
				setup: ["top-center", "bottom-center"],
				move: "center-center"
			},
			{
				setup: ["top-center", "center-center"],
				move: "bottom-center"
			},
			{
				setup: ["center-right", "bottom-right"],
				move: "top-right"
			},
			{
				setup: ["top-right", "bottom-right"],
				move: "center-right"
			},
			{
				setup: ["top-right", "center-right"],
				move: "bottom-right"
			},
			{
				setup: ["center-center", "bottom-right"],
				move: "top-left"
			},
			{
				setup: ["top-left", "bottom-right"],
				move: "center-center"
			},
			{
				setup: ["top-left", "center-center"],
				move: "bottom-right"
			},
			{
				setup: ["center-center", "bottom-left"],
				move: "top-right"
			},
			{
				setup: ["top-right", "bottom-left"],
				move: "center-center"
			},
			{
				setup: ["top-right", "center-center"],
				move: "bottom-left"
			}
		]
	};

	// Reusable functions, used frequently the component
	const f = {
		update_match_status: function() {
			// Check if all spaces have been filled or one side has won
			let 
			    all_spaces_filled = v.board.length == 9 ? true : false,
				 player_moves = [],
				 enemy_moves = [],
				 player_win = false,
				 enemy_win = false,
				 draw = false,
				 ending_message = ''
			;

			$.each(v.board, function(index, value) {
				let 
					board_square = $('.ttt__square[data-location="' + value + '"]'),
					move_info = { position: value }
				;

				board_square.hasClass("ttt__square--x")
					? enemy_moves.push(value)
					: player_moves.push(value);
			});

			$.each(v.board_wins, function(index, value) {
				if (f.contains_all(value, player_moves)) {
					v.match_ongoing = false;
					board.addClass("ttt__container--victory");
					player_win = true;
					return false;
				} else if (f.contains_all(value, enemy_moves)) {
					v.match_ongoing = false;
					board.addClass("ttt__container--failure");
					enemy_win = true;
					return false;
				}
			});
 
			if (all_spaces_filled) {
				v.match_ongoing = false;
				board.addClass("ttt__container--draw");
				draw = true;
			}

			if ( player_win ) {
				ending_message = 'Congrats, you\'re a winner!';
			} else if ( enemy_win ) {
				ending_message = 'Sorry, you\'re a loser!';
			} else if ( draw ) {
				ending_message = 'Looks like it\'s a draw!';
			}
			
			$('.ttt__ending').text(ending_message);
		},

		select_square: (square, selector) => {
			if (!square.hasClass("ttt__square--checked")) {
				let selected_square = square.attr("data-location"), letter = "x";

				v.board.push(selected_square);
				if (selector == "player") { letter = "o"; }

				square.addClass("ttt__square--checked ttt__square--" + letter);
			}
			
			f.update_match_status();
		},

		enemy_select: function() {
			let
				move_check = this.check_moves(),
				empty_squares = this.empty_squares(),
				random_square =
					empty_squares[Math.floor(Math.random() * empty_squares.length)],
				random_square_el = $('.ttt__square[data-location="' + random_square + '"]')
			;

			if (move_check !== undefined && move_check.player != "3-neither") {
				var move_check_el = $(
					'.ttt__square[data-location="' + move_check.move + '"]'
				);
				this.select_square(move_check_el);
			} else {
				this.select_square(random_square_el);
			}
			
			f.update_match_status();
		},

		empty_squares: function() {
			let empty_squares = v.board_full.filter(function(val) {
				return v.board.indexOf(val) == -1;
			});

			return empty_squares;
		},
 
		check_moves: function() {
			let 
				all_move_checks = [],
				possible_moves = []
			;

			for (var i = 0; i < v.board_checks.length; i++) {
				let move_check = this.contains_all(v.board_checks[i].setup, v.board);

				move_check !== false ? all_move_checks.push(i) : null;
			}

			possible_moves = all_move_checks.map(function(x) {
				let move_info = {
					move: v.board_checks[x].move,
					setup: v.board_checks[x].setup,
					player: []
				};

				$.each(move_info.setup, function(index, value) {
					let board_square = $('.ttt__square[data-location="' + value + '"]');

					if (board_square.hasClass("ttt__square--x")) {
						move_info.player.push("x");
					} else if (board_square.hasClass("ttt__square--o")) {
						move_info.player.push("o");
					}
				});

				move_info.player.sort();

				if (move_info.player[0] == "o" && move_info.player[1] == "o") {
					move_info.player = "2-player";
				} else if (move_info.player[0] == "x" && move_info.player[1] == "x") {
					move_info.player = "1-enemy";
				} else {
					move_info.player = "3-neither";
				}

				return move_info;
			});

			possible_moves = f.removeDuplicates(possible_moves);

			var final_possible_moves = possible_moves.filter(function(x) {
				var possible_space = x.move, index = v.board.indexOf(possible_space);

				if (index == -1) {
					return x;
				} else {
					return false;
				}
			});

			final_possible_moves.sort(function(a, b) {
				if (a.player < b.player) return -1;
				if (a.player > b.player) return 1;
				return 0;
			});

			// console.log(final_possible_moves[0]);

			return final_possible_moves[0];
		},

		contains_all: (small_array, large_array) => {
			for (var i = 0, len = small_array.length; i < len; i++) {
				if ($.inArray(small_array[i], large_array) == -1) return false;
			}
			return small_array;
		},

		removeDuplicates: array => {
			let unique_values = [];

			$.each(array, function(i, el) {
				if ($.inArray(el, unique_values) === -1) unique_values.push(el);
			});

			return unique_values;
		}
	};

	// What to do when the page loads
	function init() {}

	// Events triggered by the user, mostly click events
	function setEvents() {
		board.on("click", ".ttt__square:not(.ttt__square--checked)", function() {
			if (v.match_ongoing) { f.select_square($(this), "player"); }
			if (v.match_ongoing) { f.enemy_select(); } 
		});
	}

	// Running the initial and event functions for the Super Menu when the page loads
	function docReady() {
		init();
		setEvents();
	}

	$(document).on({
		ready: docReady()
	});
}

// Getting/adding all the super menus and either using its unique ID or adding a custom one
$(".ttt__container").each(function() {
	var id = $(this).attr("id");

	if (typeof id === typeof undefined && id !== false) {
		id = "IDUNIQUE_" + Math.floor(Math.random() * 999999999999 + 1);
		$(this).attr("id", id);
	}

	tictactoe_game(id);
});

              
            
!
999px

Console