Pen Settings

HTML

CSS

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

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

              
                <html>
<head>
	<title>Column Sorting Game</title>
	<meta name="viewport" content="width=device-width, user-scalable=no">
	<link rel="stylesheet" href="style.css" />
	<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js" integrity="sha512-ahmSZKApTDNd3gVuqL5TQ3MBTj8tL5p2tYV05Xxzcfu6/ecvt1A0j6tfudSGBVuteSoTRMqMljbfdU0g2eDNUA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
	<script type="text/javascript" src="game.js"></script>
</head>
<body>
	<div class="container">
		<h1 class="main-title">Column Sorting Game - Level 1</h1>
		<div class="game-description">Use the controls in each column to sort the colors vertically in as few "moves" as possible. Click the top button to swap the bottom tile with another column. Click the bottom button to cycle the tiles vertically.<a href="https://i.imgur.com/LoEo1vW.gif" target="_blank">Example Gameplay</a><br /><br /></div>
		<div class="game-wrapper"></div>
		<div class="moves_wrapper">
			<div class="staging_wrapper"></div>
			<div class="moves">Moves:<br />0</div><br />
			<div class="total_moves">Total Moves:<br />0</div>
		</div>
		<br class="clear" />
		<div class="point_archive">
			<h2>Point Archive:</h2>
		</div>
    
	</div>
</body>
</html>

              
            
!

CSS

              
                body {
	font-family: Helvetica;
}
h2 { margin: 0 0 10px 0; }
.container { 
	width:335px;
	margin:0 auto;
}
.game-wrapper {
	margin:0 auto;
	padding:1px 1px 2px 1px;
	width: 87px;
	height:auto;
	border:1px solid #000;
	float:left;
	-webkit-border-radius: 8px;
	-moz-border-radius: 8px;
	border-radius: 8px;
}
.point_archive { 
	display:none;
	width:auto;
	border:1px solid #000;
	padding: 5px;
	margin-top:10px;
}
.staging_wrapper {
	padding: 5px 7px 7px 5px;
	width:40px;
	height:40px;
	margin: 0 0 10px 10px;
	border:1px solid #000;
	-webkit-border-radius: 8px;
	-moz-border-radius: 8px;
	border-radius: 8px;
	
}
.moves_wrapper { 
	float:left;
}
.moves, .total_moves {
	border:1px solid #000;
	-webkit-border-radius: 8px;
	-moz-border-radius: 8px;
	border-radius: 8px;
	text-align:center;
	padding: 2px;
	margin: 0 0 0 10px;
	font-family: Helvetica, Arial;
}
.column { 
	float:left;
	margin:1px 1px 0 0;
	padding:0 0 1px 0;
	width:42px;
	height:auto;
	background-color:#666;
	-webkit-border-radius: 6px;
	-moz-border-radius: 6px;
	border-radius: 6px;
}
.column:first-child { 
	margin-left:1px;	
}
.control-move-button:focus, .control-cycle-button:focus { outline: none; }
.control-move-button { 
	width:40px;
	height:40px;
	/* background: transparent url(arrow-switch.svg) no-repeat; */
	background: transparent url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path d="M7.72 21.78a.75.75 0 001.06-1.06L5.56 17.5h14.69a.75.75 0 000-1.5H5.56l3.22-3.22a.75.75 0 10-1.06-1.06l-4.5 4.5a.75.75 0 000 1.06l4.5 4.5zm8.56-9.5a.75.75 0 11-1.06-1.06L18.44 8H3.75a.75.75 0 010-1.5h14.69l-3.22-3.22a.75.75 0 011.06-1.06l4.5 4.5a.75.75 0 010 1.06l-4.5 4.5z"></path></svg>') no-repeat;
	background-position: 8px 9px;
	border:0;
	cursor: pointer;
}
.control-move-button.disabled { 
	cursor:default;
	background-position: -38px -78px;
}
.control-cycle-button { 
	width:40px;
	height:40px;
	/* background: transparent url(sync.svg) no-repeat; */
	background: transparent url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path d="M3.38 8A9.502 9.502 0 0112 2.5a9.502 9.502 0 019.215 7.182.75.75 0 101.456-.364C21.473 4.539 17.15 1 12 1a10.995 10.995 0 00-9.5 5.452V4.75a.75.75 0 00-1.5 0V8.5a1 1 0 001 1h3.75a.75.75 0 000-1.5H3.38zm-.595 6.318a.75.75 0 00-1.455.364C2.527 19.461 6.85 23 12 23c4.052 0 7.592-2.191 9.5-5.451v1.701a.75.75 0 001.5 0V15.5a1 1 0 00-1-1h-3.75a.75.75 0 000 1.5h2.37A9.502 9.502 0 0112 21.5c-4.446 0-8.181-3.055-9.215-7.182z"></path></svg>') no-repeat;

	background-position: 8px 9px;
	border:0;
	cursor: pointer;
}
.control-cycle-button.disabled { 
	cursor:default;
	background-position: -123px -78px;
}
.piece { 
	color:#fff;
	text-shadow: 1px 1px 1px #000;
	font-weight:bold;
	float:left;
	margin:1px 1px 0 1px;
	padding:5px;
	text-align:center;
	width:28px;
	height:28px;
	background-color:#000;
	margin: 2px 0 0 2px;
	-webkit-border-radius: 4px;
	-moz-border-radius: 4px;
	border-radius: 4px;
}
.clear { clear: both; }
              
            
!

JS

              
                //window.grid_total = 25;
window.click_source = "";
window.click_target = "";
window.current_level = 0;
window.moves = 0;
window.total_moves = 0;
window.won = false;
window.container_widths = ["87px", "130px", "173px", "216px", "259px"];
window.point_archive = []
window.animation_duration = 100;
window.box_shadow = "3px 2px 2px 3px #777";
window.easing = "easeInOutElastic";
if(!window.starting_level) { window.starting_level = 1; }
$(document).ready(function() {
	var game_wrapper	= ".game-wrapper";	
	run_game(game_wrapper, window.starting_level);
	
	//Initialize Controls
	$("body").on("click", ".control-move-button", function(){
		console.log("Move Button Clicked");
		$(".column").css("background-color","#666").css("padding-top", "0");
		if(!window.click_source) { 
			window.click_source = $(this).attr("id").replace(/move_col_/, "");
			$(".control-cycle-button").attr("disabled","disabled").addClass("disabled");
			$("#col_"+window.click_source).css("background-color","#888888")
				.animate({  "padding-top": "+=41px" });
			var tmp_source = $("#col_"+window.click_source+" .piece").last().attr("id");
			var tmp_topOffset = $("#"+tmp_source).offset().top+"px";
			var tmp_leftOffset = $("#"+tmp_source).offset().left+"px";
			$("#"+tmp_source).hide()
				.css("position","absolute")
				.css("top",tmp_topOffset)
				.css("left",tmp_leftOffset).show()
				.css("-moz-box-shadow",window.box_shadow)
				.css("-webkit-box-shadow",window.box_shadow)
				.css("box-shadow",window.box_shadow)
				.animate({
					"top":Math.floor(($(".staging_wrapper").offset().top)+5)+"px", 
					"left":Math.floor(($(".staging_wrapper").offset().left)+5)+"px"
				}, window.animation_duration, window.easing, function() {
					$("#"+tmp_source).appendTo(".staging_wrapper")
						.css("position","relative")
						.css("top","auto")
						.css("left","auto")
						.css("-moz-box-shadow","none")
						.css("-webkit-box-shadow","none")
						.css("box-shadow","none");
				});
		} else { 
			window.click_target = $(this).attr("id").replace(/move_col_/, "");
			
			$(".control-cycle-button").removeAttr("disabled","disabled").removeClass("disabled");
		}
		if(window.click_source && window.click_target) {
			if(window.click_source === window.click_target) {
				var target_col = window.click_source;
				$("#col_"+target_col).css("background-color","#666")
					.animate({  "padding-top": "+=1px" }, window.animation_duration, window.easing);
				var tmp_source = $(".staging_wrapper .piece").last().attr("id");
				log("Undo move: "+tmp_source);
				var tmp = $("#col_"+target_col+" .piece").last();
				var tmp_topOffset = $("#"+tmp_source).offset().top+"px";
				var tmp_leftOffset = $("#"+tmp_source).offset().left+"px";
				$("#"+tmp_source).hide()
					.css("position","absolute")
					.css("top",tmp_topOffset)
					.css("left",tmp_leftOffset).show()
					.css("-moz-box-shadow",window.box_shadow)
					.css("-webkit-box-shadow",window.box_shadow)
					.css("box-shadow",window.box_shadow)
					.animate({
						"top":Math.floor((tmp.offset().top)+5)+"px", 
						"left":Math.floor((tmp.offset().left)+5)+"px"
					}, window.animation_duration, window.easing, function() {
						log("Attach: #"+tmp_source+" back to #col_"+target_col);
						$("#"+tmp_source).appendTo("#col_"+target_col).hide()
							.css("position","relative")
							.css("top","auto")
							.css("left","auto")
							.css("-moz-box-shadow","none")
							.css("-webkit-box-shadow","none")
							.css("box-shadow","none");
						$("#move_col_"+target_col).appendTo("#col_"+target_col);
						$("#cycle_col_"+target_col).appendTo("#col_"+target_col);
						$("#"+tmp_source).show();
				});		
				window.click_source = "";
				window.click_target = "";
				window.won = false;
			} else {
				log("Move Piece: "+window.click_source+"->"+window.click_target)
				move_piece(window.click_source, window.click_target, 1);	
				window.click_source = "";
				window.click_target = "";
				window.moves++;
				window.total_moves++;
				$(".moves").html("Moves:<br />"+window.moves);
				$(".total_moves").html("Total:<br />"+window.total_moves);
				if(check_win(game_wrapper)) {
					$(".column").css("background-color","#666").css( "margin-top", "1px");
					console.log("You've won this round in "+window.moves+" moves!");
					window.won = true;
				} else { 
					//log("Not won...");
					window.won = false;
				}
			}
		}
	});
	$("body").on("click", ".control-cycle-button", function(){
		console.log("Cycle button clicked");
		$(".column").css("background-color","#666").css("padding-top", "0");
		if(!window.click_source) { 
			window.click_source = $(this).attr("id").replace(/cycle_col_/, "");
		}
		window.click_target = window.click_source;
		if(window.click_source && window.click_target) {
			log("Move Piece: "+window.click_source+"->"+window.click_target)
			move_piece(window.click_source, window.click_target, 1);	
			window.click_source = "";
			window.click_target = "";
			window.moves++;
			window.total_moves++;
			$(".moves").html("Moves:<br />"+window.moves);
			$(".total_moves").html("Total:<br />"+window.total_moves);
			if(check_win(game_wrapper)) {
				$(".column").css("background-color","#666").css( "margin-top", "1px");
				console.log("You've won this round in "+window.moves+" moves!!");
				window.won = true;
			} else { 
				//log("Not won...");
				window.won = false;
			}
		}
	});
});
function run_game(game_wrapper, level) {
	console.log('run_game(game_wrapper, '+level+')');
	window.current_level = level;
	var cols = 2;
	var rows = 2;
	$(".main-title").html("Column Sorting Game - Level "+level);
	if(level>1)	{
		$(".point_archive").show();
		$(".point_archive").html($(".point_archive").html()
			+"Level "+(window.current_level-1)+" complete in "
			+window.point_archive[(window.current_level-2)]
			+" "+(window.point_archive[(window.current_level-2)] < 2 ? "move" : "moves")+"<br />");
	}
	switch(level) {
		case 1:
			cols = 2;
			rows = 2;
			init_game(game_wrapper, cols, rows);
			shuffle_game(game_wrapper, cols, rows);
			var check_win = setInterval(function() { 
				if(window.won) { 
					window.clearInterval(check_win); 
					window.point_archive.push(window.moves); 
					run_game(game_wrapper, (level+1)); 
				} 
			}, 1000);
			break;
		case 2:
			cols = 3;
			rows = 2;
			init_game(game_wrapper, cols, rows);
			shuffle_game(game_wrapper, cols, rows);
			var check_win = setInterval(function() { 
				if(window.won) { 
					window.clearInterval(check_win); 
					window.point_archive.push(window.moves); 
					run_game(game_wrapper, (level+1)); 
				} 
			}, 1000);
			break;
		case 3:
			cols = 3;
			rows = 3;
			init_game(game_wrapper, cols, rows);
			shuffle_game(game_wrapper, cols, rows);
			var check_win = setInterval(function() { 
				if(window.won) { 
					window.clearInterval(check_win); 
					window.point_archive.push(window.moves); 
					run_game(game_wrapper, (level+1)); 
				} 
			}, 1000);
			break;
		case 4:
			cols = 4;
			rows = 3;
			init_game(game_wrapper, cols, rows);
			shuffle_game(game_wrapper, cols, rows);
			var check_win = setInterval(function() { 
				if(window.won) { 
					window.clearInterval(check_win); 
					window.point_archive.push(window.moves); 
					run_game(game_wrapper, (level+1)); 
				} 
			}, 1000);
			break;
		case 5:
			cols = 4;
			rows = 4;
			init_game(game_wrapper, cols, rows);
			shuffle_game(game_wrapper, cols, rows);
			var check_win = setInterval(function() { 
				if(window.won) { 
					window.clearInterval(check_win); 
					window.point_archive.push(window.moves); 
					run_game(game_wrapper, (level+1)); 
				} 
			}, 1000);
			break;
		case 6:
			cols = 5;
			rows = 4;
			init_game(game_wrapper, cols, rows);
			shuffle_game(game_wrapper, cols, rows);
			var check_win = setInterval(function() { 
				if(window.won) { 
					window.clearInterval(check_win); 
					window.point_archive.push(window.moves); 
					run_game(game_wrapper, (level+1)); 
				} 
			}, 1000);
			break;
		case 7:
			cols = 5;
			rows = 5;
			init_game(game_wrapper, cols, rows);
			shuffle_game(game_wrapper, cols, rows);
			var check_win = setInterval(function() { 
				if(window.won) { 
					window.clearInterval(check_win); 
					window.point_archive.push(window.moves); 
					run_game(game_wrapper, (level+1)); 
				} 
			}, 1000);
			break;
		case 8:
			cols = 6;
			rows = 5;
			init_game(game_wrapper, cols, rows);
			shuffle_game(game_wrapper, cols, rows);
			var check_win = setInterval(function() { 
				if(window.won) { 
					window.clearInterval(check_win); 
					window.point_archive.push(window.moves); 
					run_game(game_wrapper, (level+1)); 
				} 
			}, 1000);
			break;
		case 9:
			cols = 6;
			rows = 6;
			init_game(game_wrapper, cols, rows);
			shuffle_game(game_wrapper, cols, rows);
			var check_win = setInterval(function() { 
				if(window.won) { 
					window.clearInterval(check_win); 
					window.point_archive.push(window.moves); 
					run_game(game_wrapper, (level+1)); 
				} 
			}, 1000);
			break;
		case 10:
		default:
			cols = 6;
			rows = level-3;
			init_game(game_wrapper, cols, rows);
			shuffle_game(game_wrapper, cols, rows);
			var check_win = setInterval(function() { 
				if(window.won) { 
					window.clearInterval(check_win); 
					window.point_archive.push(window.moves); 
					run_game(game_wrapper, (level+1)); 
				} 
			}, 1000);
			break;
	}
}
function init_game(game_wrapper, column_num, icons_per_col) {
	$(game_wrapper).html("");
	window.won = false;
	window.moves = 0;	
	$(".moves").html("Moves:<br />"+window.moves);
	$(".total_moves").html("Total:<br />"+window.total_moves);
	$(game_wrapper).css("width", window.container_widths[(column_num-2)]);
	var col_text = "";
	var colors = get_colors();
	for(var i = 0; i < column_num; i++) {
		col_text = '<div class="column" id="col_'+i+'">';
		for(var j = 0; j < icons_per_col; j++) {
			//col_text = col_text+'<div class="piece" id="p_'+i+'_'+j+'" style="background-color:'+colors[i]+'">'+i+':'+j+'</div>';
			col_text = col_text+'<div class="piece" id="p_'+i+'_'+j+'" style="background-color:'+colors[i]+'"></div>';
		}
		col_text = col_text
			+'<button class="control-move-button" id="move_col_'+i+'">&nbsp;</button>'
			+'<button class="control-cycle-button" id="cycle_col_'+i+'">&nbsp;</button>'
			+'</div>';
		$(col_text).appendTo(game_wrapper);
	}
	$('<br class="clear" />').appendTo(game_wrapper);
}

function check_win(game_wrapper) {
	var has_won = true;
	$(game_wrapper+" .column").each(function(i){
	//for(var i = 0; i < cols; i++) {
		var crnt_color = "";
		var prev_color = "";
		$("#col_"+i+" .piece").each(function() {
			prev_color = crnt_color;
			crnt_color = $(this).css("background-color");
			if(prev_color) {
				//log("Does crnt_color["+crnt_color+"] match the prev_color["+prev_color+"]? "+(crnt_color === prev_color ? 'Yes' : 'No'));
				if(crnt_color !== prev_color) { has_won = false; }
			}
		});
	//}
	});
	if(has_won) { return true; } else { return false; }
}
function move_piece(source, target, animation) {
	var d_piece = $("#col_"+target+" .piece").last().attr("id");
	if($(".staging_wrapper").children().length > 0) { 
		var s_piece = $(".staging_wrapper .piece").last().attr("id");
	} else { 
		var s_piece = $("#col_"+source+" .piece").last().attr("id");
	}
	log("Source Piece: "+s_piece+" - Destination Piece: "+d_piece);
	if(!animation) {
		$("#"+s_piece).prependTo("#col_"+target);//Animate
		$("#"+d_piece).prependTo("#col_"+source);//Animate
	} else {
		var s_topOffset = $("#"+s_piece).offset().top+"px";
		var s_leftOffset = $("#"+s_piece).offset().left+"px";
		var d_topOffset = $("#"+d_piece).offset().top+"px";
		var d_leftOffset = $("#"+d_piece).offset().left+"px";
		$("#"+s_piece).hide()
			.css("position","absolute")
			.css("top",s_topOffset)
			.css("left",s_leftOffset).show()
			.css("-moz-box-shadow",window.box_shadow)
			.css("-webkit-box-shadow",window.box_shadow)
			.css("box-shadow",window.box_shadow)
			.animate({
				"top":Math.floor(($("#col_"+target).offset().top)+5)+"px", 
				"left":Math.floor(($("#col_"+target).offset().left)+5)+"px"
				}, window.animation_duration, window.easing, function() {
					$("#"+s_piece).prependTo("#col_"+target)
						.css("position","relative")
						.css("top","auto")
						.css("left","auto")
						.css("-moz-box-shadow","none")
						.css("-webkit-box-shadow","none")
						.css("box-shadow","none");
				}
			);
		$("#"+d_piece).hide()
			.css("position","absolute")
			.css("top",d_topOffset)
			.css("left",d_leftOffset).show()
			.css("-moz-box-shadow",window.box_shadow)
			.css("-webkit-box-shadow",window.box_shadow)
			.css("box-shadow",window.box_shadow)
			.animate({
				"top":Math.floor(($("#col_"+source).offset().top)+5)+"px", 
				"left":Math.floor(($("#col_"+source).offset().left)+5)+"px"
				}, window.animation_duration, window.easing, function() {
					$("#"+d_piece)
						.prependTo("#col_"+source)
						.css("position","relative")
						.css("top","auto")
						.css("left","auto")
						.css("-moz-box-shadow","none")
						.css("-webkit-box-shadow","none")
						.css("box-shadow","none");
					if(check_win(".game-wrapper")) {
						$(".column").css("background-color","#666").css( "margin-top", "1px");
						console.log("You've won this round in "+window.moves+" moves!!!");
						window.won = true;
					} else { 
						//log("Not won...");
						window.won = false;
					}
				}
			);
	}
}
function shuffle_game(game_wrapper, cols, rows) {
	for(var i = 0; i < (cols*rows); i++) {
		var rand_source = Math.floor((Math.random()*cols)+1)-1;
		var rand_target = Math.floor((Math.random()*cols)+1)-1;
		move_piece(rand_source, rand_target, 0);
		//log("Source: "+rand_source+ " - Destination: "+rand_target);
		
	}
}
function get_colors() {
	var arr = new Array("#00FF00","#0066FF","#FF0000","#660066","#FFFF00","#FFFFFF");
	return arr;
}
function log(msg) {
	if(console) { console.log(msg); }
}


              
            
!
999px

Console