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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div class="centered-pen">
	<div class="inline">
		<button class="mb-3 btn btn-primary" onclick="undo();">Undo</button>
		<button class="mb-3 btn btn-primary" onclick="reset();">Reset</button>
		<p id="move-count" class="lead text-light">Moves: 0</p>
	</div>
	<div id="grid" class="grid"></div>
</div>

<p class="taco-signature">🌮 made this with 💕</p>
              
            
!

CSS

              
                html, body {
	background-color: rgb(60, 54, 173);
	background-image: linear-gradient( 113.3deg,  rgba(217,9,27,1) 6.9%, rgba(22,68,150,1) 75% );
}
.inline {
	display: inline-flex;
}
.inline * {
	margin: 5px;
}
.grid {
	width: 400px;
	height: 400px;
}
.grid div {
	color: #fff7;
	background-color: #fff5;
	font-size: 25px;
}
.grid div:empty {
	background-color: #0005;
}
.grid div:hover {
	cursor: pointer;
	background-color: #0009;
	color: #fff;
	transition: all 0.3s linear;
}
              
            
!

JS

              
                // Set the data for a grid of NxN size.
let gameData = [
	[ 0, 0, 0 ],
	[ 0, 9, 0 ],
	[ 0, 0, 0 ]
];

// Additional variables because we can.
var moveCount = 0;
var moves = [];

// Define what a tile looks like.
class Tile {
	constructor(x, y, value) {
		this.active = value != "";
		this.x = x;
		this.y = y;
		this.value = value;
	}
}

// Return the data displayed in the center of a tile.
function getTileData(tile) {
	if (tile.value == 0)
		return '';
	
	return tile.value;
}

// Build the grid.
function buildGrid(gridId, rawData) {
	// Create the return variable.
	let result = [];

	// Create the tile collection from the raw data.
	for (var x = 0; x < rawData.length; x++) {
		let row = [];
		for (var y = 0; y < rawData[x].length; y++) {
			let tile = new Tile(x, y, rawData[x][y]);
			row.push(tile);
		}

		result.push(row);
	}

	// Draw the grid and return the tile collection.
	drawGrid(gridId, result);
	return result;
}
function drawGrid(gridId, tiles) {
	// Get the grid element.
	let gridElement = document.getElementById(gridId);

	// Set the grid's size class.
	let gridHeight = tiles.length;
	let gridWidth = 0;
	for (var row of tiles)
		if (row.length > gridWidth)
			gridWidth = row.length;

	gridElement.classList.add("gh-" + gridHeight);
	gridElement.classList.add("gw-" + gridWidth);

	// Create the grid's inner HTML.
	let html = "";
	for (var row of tiles)
		for (var column of row)
			html += '<div onclick="tileClicked(' + column.x + "," + column.y + ');">' + getTileData(column) + "</div>";

	// Set the inner HTML of the grid.
	gridElement.innerHTML = html;
}

function tileClicked(x, y) {
	// Get the tile.
	let tile = activeData[x][y];
	if (!tile.active)
		return;
	
	// Increment the move count and record the move for the player.
	console.log('Move ' + (++moveCount) + ': T[' + (y + 1) + ', ' + (x + 1) + ']');
	document.getElementById('move-count').innerText = 'Moves: ' + moveCount;
	moves.push(tile);
	
	var adjacentCells = [];
	
	// Get the cell to the left.
	if (x >= 1)
		adjacentCells.push(activeData[x - 1][y]);
	
	// Get the cell to the right.
	if (x <= 1)
		adjacentCells.push(activeData[x + 1][y]);
	
	// Get the cell above.
	if (y >= 1)
		adjacentCells.push(activeData[x][y - 1]);
	
	// Get the cell below.
	if (y <= 1)
		adjacentCells.push(activeData[x][y + 1]);
	
	// Record the number of inactive cells before activating them.
	let inactiveTiles = 0;
	for (var cell of adjacentCells) {
		if (!cell.active)
			inactiveTiles++;
	}
	
	// Calculate the new face value.
	let newFaceValue = tile.value % 2 == 0 ? tile.value / 2 : (tile.value * 3) + 1;
	if (newFaceValue <= 0)
		newFaceValue = 0;
	
	// Adjust adjacent cell face values.
	for (var cell of adjacentCells) {
		if (!cell.active) {
			cell.value = parseInt(tile.value / 2);
			cell.active = true;
		}
		else {
			cell.value = cell.value % 2 == 0 ? cell.value / 2 : (cell.value * 3) + 1;
			if (cell.value < 0)
				cell.value = 0;
		
			cell.active = cell.value > 0;
		}
		activeData[cell.x][cell.y] = cell;
	}
	
	// Adjust the tile's value and active state.
	tile.value = newFaceValue;
	tile.active = tile.value > 0;
	
	activeData[tile.x][tile.y] = tile;
	drawGrid('grid', activeData);
}

function undo() {
	if (moves.length == 0)
		return;
	
	moves.pop();
	const movesCopy = JSON.parse(JSON.stringify(moves));
	moves = [];
	
	console.clear();
	moveCount = 0;
	document.getElementById('move-count').innerText = 'Moves: ' + moveCount;
	activeData = buildGrid('grid', gameData);
	
	for (let i = 0; i < movesCopy.length; i++)
		tileClicked(movesCopy[i].x, movesCopy[i].y);
}
function reset() {
	console.clear();
	moveCount = 0;
	moves = [];
	document.getElementById('move-count').innerText = 'Moves: ' + moveCount;
	activeData = buildGrid('grid', gameData);
}

// Trigger the build.
var activeData = buildGrid("grid", gameData);
              
            
!
999px

Console