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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css" integrity="sha256-2XFplPlrFClt0bIdPgpz8H7ojnk10H69xRqd9+uTShA=" crossorigin="anonymous" />
<link href="https://fonts.googleapis.com/css2?family=Vollkorn:ital,wght@1,700&display=swap" rel="stylesheet">
<div class="navbar">
	<div class="generate-maze">Generate New Maze</div>
	<div class="visualize-maze">Visualize Maze Generator</div>
	<div class="checkbox"><i class="fas fa-check"></i></div>
</div>
              
            
!

CSS

              
                * {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}

body {
	height: 100vh;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;

	background: #a1ffce;
	background: -webkit-linear-gradient(to right, #faffd1, #a1ffce);
	background: linear-gradient(to right, #faffd1, #a1ffce);
}

.row {
	display: flex;
	justify-content: center;
	align-items: center;
}

.cell {
	height: 45px;
	width: 45px;
	border-bottom: 4px solid darkgreen;
	border-right: 4px solid darkgreen;

	position: relative; /*To fix the Corner Border */

	/*display:flex so that the <img> within it is centered*/
	display: flex;
	align-items: center;
	justify-content: center;
}

.first-col {
	border-left: 4px solid darkgreen;
}

.first-row {
	border-top: 4px solid darkgreen;
}

/* This fixes the Corner Border */
.bottomRightCorner::after {
	position: absolute;
	content: "";
	right: 0;
	bottom: 0;
	width: 4px;
	height: 4px;
	background: darkgreen;
}

.container {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	margin-top: 4px;
}

.middle-row {
	display: flex;
}

.direction-button {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 50px;
	width: 50px;
	font-size: 3rem;
	border: 3px solid darkgreen;
	border-radius: 50%;
	color: darkgreen;
	cursor: pointer;
}

.middle-row .direction-button:first-of-type {
	margin-right: 50px;
}

.top-margin {
	margin-top: 10px;
}

.navbar {
	display: flex;
	justify-content: center;
	color: darkgreen;
	margin: 0 10px 20px 10px;

	font-family: "Vollkorn", serif;
	font-size: 1.2rem;
	font-weight: bold;
}

.generate-maze {
	border: 3px solid darkgreen;
	padding: 3px 10px;
	margin-right: 20px;
	cursor: pointer;
}

.generate-maze:hover,
.checkbox:hover {
	background: rgba(173, 255, 47, 0.6);
}

.visualize-maze {
	border-top: 3px solid darkgreen;
	border-left: 3px solid darkgreen;
	border-bottom: 3px solid darkgreen;
	padding: 3px 10px;
}

.checkbox {
	border: 3px solid darkgreen;
	padding: 3px;
	cursor: pointer;
	width: 45px;

	display: flex;
	align-items: center;
	justify-content: center;
}

.highlight {
	background: greenyellow;
}

.heart {
	position: absolute;
	animation: fly 3s linear forwards;
	color: #e7273f;
	z-index: 1;
}

@keyframes fly {
	to {
		transform: translateY(-50px);
	}
}

              
            
!

JS

              
                const nrows = Math.floor(innerHeight / 45) - 6;
const ncols = Math.floor(innerWidth / 45) - 1;
const cat = `<img src="https://i.imgur.com/e9LJtL4.png" height="27px" width="27px"></img>`;
const burger = `<img src="https://i.imgur.com/GugJK1Z.png" height="27px" width="27px"></img>`;

function createCells() {
	for (let i = 0; i < nrows; i++) {
		let row = document.createElement("div");
		row.classList.add("row");
		if (i === 0) row.classList.add("first-row");
		document.body.appendChild(row);

		for (let j = 0; j < ncols; j++) {
			let col = document.createElement("div");
			col.classList.add("cell");
			if (j === 0) col.classList.add("first-col");
			row.appendChild(col);
		}
	}
}

createCells();
document.body.innerHTML += `
	<div class="container">
		<div class="direction-button top-margin"><i class="fas fa-caret-up"></i></div>
		<div class="middle-row">
			<div class="direction-button"><i class="fas fa-caret-left"></i></div>
			<div class="direction-button"><i class="fas fa-caret-right"></i></div>
		</div>
		<div class="direction-button"><i class="fas fa-caret-down"></i></div>
	</div>
`;

class Cell {
	constructor(i, j) {
		this.r = i;
		this.c = j;

		this.neighbors = [];
		this.walls = [true, true, true, true]; //Top Right Bottom Left
		this.visited = false;
	}
	show() {
		if (!this.walls[0])
			cellElements[this.r * ncols + this.c].style["border-top"] = "none";
		if (!this.walls[1])
			cellElements[this.r * ncols + this.c].style["border-right"] = "none";
		if (!this.walls[2])
			cellElements[this.r * ncols + this.c].style["border-bottom"] = "none";
		if (!this.walls[3])
			cellElements[this.r * ncols + this.c].style["border-left"] = "none";

		if (this.visited)
			cellElements[this.r * ncols + this.c].style.background = "greenyellow";
	}
	createNeighbors() {
		if (this.r > 0) this.neighbors.push(grid[(this.r - 1) * ncols + this.c]); //Top
		if (this.c < ncols - 1)
			this.neighbors.push(grid[this.r * ncols + (this.c + 1)]); //Right
		if (this.r < nrows - 1)
			this.neighbors.push(grid[(this.r + 1) * ncols + this.c]); //Bottom
		if (this.c > 0) this.neighbors.push(grid[this.r * ncols + (this.c - 1)]); //Left
	}
	fix() {
		if (!this.walls[1] && !this.walls[2])
			cellElements[this.r * ncols + this.c].classList.add("bottomRightCorner");
	}
}

function fillGrid() {
	for (let i = 0; i < nrows; i++) {
		for (let j = 0; j < ncols; j++) {
			grid.push(new Cell(i, j));
		}
	}
	for (let i = 0; i < nrows; i++) {
		for (let j = 0; j < ncols; j++) {
			grid[i * ncols + j].createNeighbors();
		}
	}
}

let cellElements = document.querySelectorAll(".cell");
let grid = [];
fillGrid();

let current = grid[0];
let stack = [];
let play = false;
let goal = grid[nrows * ncols - 1];
let drawLoop = setInterval(animate, 50);
let canAnimate = true;
let popUps = [];

//Maze Generator Algorithm
function BacktrackingDFS() {
	cellElements[current.r * ncols + current.c].style.background = "olive";
	if (!current.visited) {
		current.visited = true;
		stack.push(current);
	}

	let unvisited = [];
	for (let neigh of current.neighbors) {
		if (!neigh.visited) {
			unvisited.push(neigh);
		}
	}

	if (unvisited.length > 0) {
		let randomIndex = Math.floor(Math.random() * unvisited.length);
		let next = unvisited[randomIndex];

		//Remove Walls
		if (current.r - next.r === 1) {
			//TOP
			current.walls[0] = false;
			next.walls[2] = false;
		} else if (current.c - next.c === -1) {
			//RIGHT
			current.walls[1] = false;
			next.walls[3] = false;
		} else if (current.r - next.r === -1) {
			//BOTTOM
			current.walls[2] = false;
			next.walls[0] = false;
		} else if (current.c - next.c === 1) {
			//LEFT
			current.walls[3] = false;
			next.walls[1] = false;
		}

		current = next;
	} else if (stack.length > 0) {
		current = stack.pop();
	} else {
		clearInterval(drawLoop);
		play = true;
		cellElements[current.r * ncols + current.c].style.background = "greenyellow";
		cellElements[goal.r * ncols + goal.c].innerHTML = burger;
		cellElements[current.r * ncols + current.c].innerHTML = cat;
	}
}

function animate() {
	for (let i = 0; i < nrows; i++) {
		for (let j = 0; j < ncols; j++) {
			grid[i * ncols + j].show();
		}
	}
	for (let i = 0; i < nrows; i++) {
		for (let j = 0; j < ncols; j++) {
			grid[i * ncols + j].fix();
		}
	}
	BacktrackingDFS();
}

// GAME PLAY PHASE BEGINS HERE
let btn = document.querySelector(".generate-maze");
btn.addEventListener("click", function () {
	clearInterval(drawLoop);
	for (let popUp of popUps) {
		popUp.style.display = "none";
	}
	for (let i = 0; i < nrows; i++) {
		for (let j = 0; j < ncols; j++) {
			cellElements[i * ncols + j].removeAttribute("style");
			cellElements[i * ncols + j].classList.remove("bottomRightCorner");
			cellElements[i * ncols + j].innerHTML = "";
		}
	}
	grid = [];
	fillGrid();
	current = grid[0];
	stack = [];
	play = false;
	goal = grid[nrows * ncols - 1];
	if (canAnimate) drawLoop = setInterval(animate, 50);
	else {
		while (!play) {
			animate();
		}
	}
});

let checkbox = document.querySelector(".checkbox");
checkbox.addEventListener("click", function () {
	if (checkbox.innerHTML.includes("check"))
		checkbox.innerHTML = `<i class="fas fa-times"></i>`;
	else checkbox.innerHTML = `<i class="fas fa-check"></i>`;
	canAnimate = !canAnimate;
});

let directionButtons = document.querySelectorAll(".direction-button");

for (let k = 0; k < directionButtons.length; k++) {
	directionButtons[k].addEventListener("mousedown", function () {
		if (play) {
			cellElements[current.r * ncols + current.c].innerHTML = ``;
			if (k == 0) upArrowPressed();
			if (k == 1) leftArrowPressed();
			if (k == 2) rightArrowPressed();
			if (k == 3) downArrowPressed();
			cellElements[current.r * ncols + current.c].innerHTML = cat;
			checkWin();
		}
	});
}

document.body.addEventListener("keydown", function (event) {
	if (play) {
		cellElements[current.r * ncols + current.c].innerHTML = ``;
		if (event.key === "ArrowUp") upArrowPressed();
		if (event.key === "ArrowLeft") leftArrowPressed();
		if (event.key === "ArrowRight") rightArrowPressed();
		if (event.key === "ArrowDown") downArrowPressed();
		cellElements[current.r * ncols + current.c].innerHTML = cat;
		checkWin();
	}
});

document.body.addEventListener("keyup", function (event) {
	for (let k = 0; k < directionButtons.length; k++) {
		directionButtons[k].classList.remove("highlight");
	}
});

document.body.addEventListener("mouseup", function (event) {
	for (let k = 0; k < directionButtons.length; k++) {
		directionButtons[k].classList.remove("highlight");
	}
});

function checkWin() {
	if (current === goal) {
		console.log("WON");
		play = false;
		victoryMessage();
	}
}
function upArrowPressed() {
	if (!current.walls[0]) current = grid[(current.r - 1) * ncols + current.c];
	directionButtons[0].classList.add("highlight");
}
function leftArrowPressed() {
	if (!current.walls[3]) current = grid[current.r * ncols + (current.c - 1)];
	directionButtons[1].classList.add("highlight");
}
function rightArrowPressed() {
	if (!current.walls[1]) current = grid[current.r * ncols + (current.c + 1)];
	directionButtons[2].classList.add("highlight");
}
function downArrowPressed() {
	if (!current.walls[2]) current = grid[(current.r + 1) * ncols + current.c];
	directionButtons[3].classList.add("highlight");
}

function victoryMessage() {
	popUps = [];
	for (let i = 0; i < 10; i++) {
		const popUp = document.createElement("span");
		popUp.classList.add("heart");
		popUp.innerHTML = `<i class="fas fa-heart"></i>`;

		popUp.style.top = Math.random() * 50 + 50 + "%";
		popUp.style.left = Math.random() * 100 + "%";
		popUp.style.fontSize = Math.random() * 10 + 5 + "px";

		popUps.push(popUp);
	}

	for (let i = 0; i < 10; i++) {
		setTimeout(function () {
			cellElements[(goal.r - 1) * ncols + goal.c].appendChild(popUps[i]);
			setTimeout(function () {
				popUps[i].remove();
			}, 2000);
		}, i * 100);
	}
}

              
            
!
999px

Console