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

              
                <div class="board"></div>

<a href="#!" class="btn">New Game</a>

<div class="endscreen"></div>

<!-- need to style the settings first -->

<!-- <div class="settings">
	<label for="boardSize">board size</label>
	<input id="boardSize" type="range" min="0" max="20">
	
	<label for="tileSize">tile size</label>
	<input id="tileSize" type="range" min="10" max="100" step="10">
	
	<fieldset>
    <input type="radio" id="easy" name="difficulty" value="0.1" class="difficulty">
    <label for="easy">Easy</label> 
    <input type="radio" id="normal" name="difficulty" value="0.2" class="difficulty" checked>
    <label for="normal">Normal</label>
		<input type="radio" id="hard" name="difficulty" value="0.4" class="difficulty">
    <label for="hard">Hard</label> 
  </fieldset>
</div> -->
              
            
!

CSS

              
                $grey: #e2e2e2;
$bg: #ffebc5;
$red: crimson;

@mixin center() {
	position: absolute;
	margin: auto;
	left: 0;
	right: 0;
	top: 0;
	bottom: 0;
}

:root {
  --tileSize: 60px;
	--boardSize: 0px;
}

* {
	margin: 0;
	padding: 0;
	box-sizing:border-box;
	font-family: 'VT323', monospace;
	
	&::selection {
		background: transparentize(#7f8c8d, .4);
	}
}

html {
	width: 100%;
	height: 100%;
}

body {
	width: 100%;
	min-height: 100%;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	background: $bg;
}

.btn {
	background: darken($grey, 10%);
	padding: .5rem 1rem;
	text-decoration: none;
	color: darken($grey, 30%);
	font-size: 1.5rem;
	text-transform: uppercase;
	user-select: none;
	transition: background .2s ease, transform .2s ease;
	box-shadow: inset 0 2px 0 lighten($grey, 2%),
							inset 0 -2px 0 darken($grey, 15%),
							inset 2px 0 0 lighten($grey, 2%),
							inset -2px 0 0 darken($grey, 15%);
	text-shadow: 0 1px 0 lighten($grey, 5%),
							0 -1px 0 darken($grey, 40%);
	
	&:hover {
		background: darken($grey, 5%);
		transform: translateY(-1px);
	}
	
	&:active {
		background: darken($grey, 15%);
		transform: translateY(1px);
	}
}

.endscreen {
	position: fixed;
	background: $grey;
	padding: 1rem 2rem;
	font-size: 40px;
	display: none;
	box-shadow: inset 0 2px 0 lighten($grey, 2%),
							inset 0 -2px 0 darken($grey, 15%),
							inset 2px 0 0 lighten($grey, 2%),
							inset -2px 0 0 darken($grey, 15%),
							0 4px 8px 0 rgba(0,0,0,.2),
							0 16px 32px 0 rgba(0,0,0,.2);
	text-shadow: 0 1px 0 lighten($grey, 5%),
							0 -1px 0 darken($grey, 40%);
	
	&.show {
		display: block;
	}
	
	span {
		color: #2ecc71;
	}
}

.board {
	// width: 10 * $tilesize;
	// height: 10 * $tilesize;
	background: $grey;
	position: relative;
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
	margin-bottom: 2rem;
	border: 10px solid darken($grey, 5%);
	box-sizing: content-box;
	box-shadow: 0 6px 0 darken($grey, 15%),
							0 -6px 0 lighten($grey, 2%),
							6px 0 0 darken($grey, 15%),
							-6px 0 0 lighten($grey, 2%);
	
	&:before {
		content: '';
		position: absolute;
		width: 8px;
		height: 6px;
		background: $grey;
		top: -14px;
		right: -14px;
		transform: rotate(45deg);
		z-index: -1;
		box-shadow: 1px calc(var(--boardSize) * 1.465) 0 $grey;
	}
	
	&:after {
		content: '';
		position: absolute;
		width: 8px;
		height: 6px;
		background: lighten($grey, 40%);
		top: -14px;
		left: -14px;
		transform: rotate(-45deg);
		z-index: -1;
		box-shadow: -1px calc(var(--boardSize) * 1.465) 0 darken($grey, 20%);
	}
	
	.tile {
		background: $grey;
		width: var(--tileSize);
		height: var(--tileSize);
		position: relative;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: calc(var(--tileSize) * .8);
		cursor: pointer;
		user-select: none;
		transition: background .2s ease;
		// border: 1px solid darken($grey, 15%);
		box-shadow: inset 0 2px 0 0 lighten($grey, 10%),
								inset 2px 0 0 0 lighten($grey, 10%),
								inset 0 -2px 0 0 darken($grey, 10%),
								inset -2px 0 0 0 darken($grey, 10%);
		
		&:hover:not(.tile--checked) {
			background: lighten($grey, 5%);
		}
		
		&--checked {
			background: darken($grey, 5%);
			box-shadow: inset 0 1px 0 0 darken($grey, 10%),
									inset 1px 0 0 0 darken($grey, 10%),
									inset 0 -1px 0 0 darken($grey, 10%),
									inset -1px 0 0 0 darken($grey, 10%);
			
			&.tile--bomb {
				&:before, &:after {
					opacity: 1;
				}
			}
			
			&:hover {
				cursor: default;
			}
		}
		
		&--clear {
			
		}
		
		&--bomb {
			font-size: calc(var(--tileSize) * .5);
		}
		
		&--flagged {
			font-size: calc(var(--tileSize) * .5);
		}
	}
}

.settings {
	position: fixed;
	left: 0;
	top: 0;
	background: rgba(0,0,0,.1);
	display: flex;
	flex-direction: column;
	padding: 1rem;
	
	label {
		font-size: 1.2rem;
	}
}
              
            
!

JS

              
                console.clear();

let size = 10; // size x size tiles
let bombFrequency = 0.2; // percentage of bombs
let tileSize = 60;

const board = document.querySelectorAll('.board')[0];
let tiles;
let boardSize;

const restartBtn = document.querySelectorAll('.btn')[0];
const endscreen = document.querySelectorAll('.endscreen')[0]

// settings
const boardSizeBtn = document.getElementById('boardSize');
const tileSizeBtn = document.getElementById('tileSize');
const difficultyBtns = document.querySelectorAll('.difficulty');

let bombs = [];
let numbers = [];
let numberColors = ['#3498db', '#2ecc71', '#e74c3c', '#9b59b6', '#f1c40f', '#1abc9c', '#34495e', '#7f8c8d',];
let endscreenContent = {win: '<span>✔</span> you won!', loose: '💣 Booom! Game over.'};

let gameOver = false;

/* clear board */
const clear = () => {
	// console.clear();
	gameOver = false;
	bombs = [];
	numbers = [];
	endscreen.innerHTML = '';
	endscreen.classList.remove('show');
	tiles.forEach(tile => {
		tile.remove();
	});
	
	setup();
}


/* setup the game */
const setup = () => {
	for (let i = 0; i < Math.pow(size, 2); i++) {
		const tile = document.createElement('div');
		tile.classList.add('tile');
		board.appendChild(tile);
	}
	tiles = document.querySelectorAll('.tile');
	boardSize = Math.sqrt(tiles.length);
	board.style.width = boardSize * tileSize + 'px';
	
	document.documentElement.style.setProperty('--tileSize', `${tileSize}px`);
	document.documentElement.style.setProperty('--boardSize', `${boardSize * tileSize}px`);
	
	let x = 0;
	let y = 0;
	tiles.forEach((tile, i) => {
		// set tile coordinates
		tile.setAttribute('data-tile', `${x},${y}`);

		// add bombs
		let random_boolean = Math.random() < bombFrequency;
		if (random_boolean) {
			bombs.push(`${x},${y}`);
			if (x > 0) numbers.push(`${x-1},${y}`);
			if (x < boardSize - 1) numbers.push(`${x+1},${y}`);
			if (y > 0) numbers.push(`${x},${y-1}`);
			if (y < boardSize - 1) numbers.push(`${x},${y+1}`);
			
			if (x > 0 && y > 0) numbers.push(`${x-1},${y-1}`);
			if (x < boardSize - 1 && y < boardSize - 1) numbers.push(`${x+1},${y+1}`);
			
			if (y > 0 && x < boardSize - 1) numbers.push(`${x+1},${y-1}`);
			if (x > 0 && y < boardSize - 1) numbers.push(`${x-1},${y+1}`);
		}

		x++;
		if (x >= boardSize) {
			x = 0;
			y++;
		}
		
		/* rightclick */
		tile.oncontextmenu = function(e) {
			e.preventDefault();
			flag(tile);
		}
		
		/* leftclick */
		tile.addEventListener('click', function(e) {
			clickTile(tile);
		});
	});
	
	numbers.forEach(num => {
		let coords = num.split(',');
		let tile = document.querySelectorAll(`[data-tile="${parseInt(coords[0])},${parseInt(coords[1])}"]`)[0];
		let dataNum = parseInt(tile.getAttribute('data-num'));
		if (!dataNum) dataNum = 0;
		tile.setAttribute('data-num', dataNum + 1);
	});
}


/* flag a tile */
const flag = (tile) => {
	if (gameOver) return;
	if (!tile.classList.contains('tile--checked')) {
		if (!tile.classList.contains('tile--flagged')) {
			tile.innerHTML = '🚩';
			tile.classList.add('tile--flagged');
		} else {
			tile.innerHTML = '';
			tile.classList.remove('tile--flagged');
		}
	}
}


/* check if bomb or not */
const clickTile = (tile) => {
	if (gameOver) return;
	if (tile.classList.contains('tile--checked') || tile.classList.contains('tile--flagged')) return;
	let coordinate = tile.getAttribute('data-tile');
	if (bombs.includes(coordinate)) {
		endGame(tile);
	} else {
		/* check if nearby bomb */
		let num = tile.getAttribute('data-num');
		if (num != null) {
			tile.classList.add('tile--checked');
			tile.innerHTML = num;
			tile.style.color = numberColors[num-1];
			setTimeout(() => {
				checkVictory();
			}, 100);
			return;
		}
		
		checkTile(tile, coordinate);
	}
	tile.classList.add('tile--checked');
}


/* clicked the right one */
const checkTile = (tile, coordinate) => {
	
	console.log('✔');
	let coords = coordinate.split(',');
	let x = parseInt(coords[0]);
	let y = parseInt(coords[1]);
	
	/* check nearby tiles */
	setTimeout(() => {
		if (x > 0) {
			let targetW = document.querySelectorAll(`[data-tile="${x-1},${y}"`)[0];
			clickTile(targetW, `${x-1},${y}`);
		}
		if (x < boardSize - 1) {
			let targetE = document.querySelectorAll(`[data-tile="${x+1},${y}"`)[0];
			clickTile(targetE, `${x+1},${y}`);
		}
		if (y > 0) {
			let targetN = document.querySelectorAll(`[data-tile="${x},${y-1}"]`)[0];
			clickTile(targetN, `${x},${y-1}`);
		}
		if (y < boardSize - 1) {
			let targetS = document.querySelectorAll(`[data-tile="${x},${y+1}"]`)[0];
			clickTile(targetS, `${x},${y+1}`);
		}
		
		if (x > 0 && y > 0) {
			let targetNW = document.querySelectorAll(`[data-tile="${x-1},${y-1}"`)[0];
			clickTile(targetNW, `${x-1},${y-1}`);
		}
		if (x < boardSize - 1 && y < boardSize - 1) {
			let targetSE = document.querySelectorAll(`[data-tile="${x+1},${y+1}"`)[0];
			clickTile(targetSE, `${x+1},${y+1}`);
		}
		
		if (y > 0 && x < boardSize - 1) {
			let targetNE = document.querySelectorAll(`[data-tile="${x+1},${y-1}"]`)[0];
			clickTile(targetNE, `${x+1},${y-1}`);
		}
		if (x > 0 && y < boardSize - 1) {
			let targetSW = document.querySelectorAll(`[data-tile="${x-1},${y+1}"`)[0];
			clickTile(targetSW, `${x-1},${y+1}`);
		}
	}, 10);
}


/* Bomb clicked -> end game */
const endGame = (tile) => {
	console.log('💣 Booom! Game over.');
	endscreen.innerHTML = endscreenContent.loose;
	endscreen.classList.add('show');
	gameOver = true;
	tiles.forEach(tile => {
		let coordinate = tile.getAttribute('data-tile');
		if (bombs.includes(coordinate)) {
			tile.classList.remove('tile--flagged');
			tile.classList.add('tile--checked', 'tile--bomb');
			tile.innerHTML = '💣';
		}
	});
}

const checkVictory = () => {
	let win = true;
	tiles.forEach(tile => {
		let coordinate = tile.getAttribute('data-tile');
		if (!tile.classList.contains('tile--checked') && !bombs.includes(coordinate)) win = false;
	});
	if (win) {
		endscreen.innerHTML = endscreenContent.win;
		endscreen.classList.add('show');
		gameOver = true;
	}
}


/* start game */
setup();

/* click button for new game */
restartBtn.addEventListener('click', function(e) {
	e.preventDefault();
	clear();
});

// settings
boardSizeBtn.addEventListener('change', function(e) {
	console.log(this.value);
	size = this.value;
	clear();
});

tileSizeBtn.addEventListener('change', function(e) {
	console.log(this.value);
	tileSize = this.value;
	clear();
});

difficultyBtns.forEach(btn => {
	btn.addEventListener('click', function() {
		console.log(this.value);
		bombFrequency = this.value;
		clear();
	});
});

console.log(`${boardSize} x ${boardSize} tiles`);
console.log('bombs');
console.log(bombs);
console.log('numbers');
console.log(numbers);
              
            
!
999px

Console