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

              
                <h1>Whack-a-Mole</h1>

<p>Click as many moles as you can in 25 seconds.</p>

<div id="app" aria-live="polite"></div>
              
            
!

CSS

              
                body {
	margin: 0 auto;
	max-width: 40em;
	width: 88%;
}

/**
 * Add box sizing to everything
 * @link http://www.paulirish.com/2012/box-sizing-border-box-ftw/
 */
*,
*:before,
*:after {
  -webkit-box-sizing: border-box;
          box-sizing: border-box;
}

svg {
    pointer-events: none;
}

/**
 * Mole Holes
 */

.field {
	background-color: #3d9970;
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	grid-template-rows: repeat(3, 1fr);
	grid-column-gap: 0;
	grid-row-gap: 0;
	padding: 1em 0;
}

.plot {
	height: 6em;
	padding: 0.5em;
	text-align: center;
}

#plot0 { grid-area: 1 / 1 / 2 / 2; }
#plot1 { grid-area: 1 / 2 / 2 / 3; }
#plot2 { grid-area: 1 / 3 / 2 / 4; }
#plot3 { grid-area: 2 / 1 / 3 / 2; }
#plot4 { grid-area: 2 / 2 / 3 / 3; }
#plot5 { grid-area: 2 / 3 / 3 / 4; }
#plot6 { grid-area: 3 / 1 / 4 / 2; }
#plot7 { grid-area: 3 / 2 / 4 / 3; }
#plot8 { grid-area: 3 / 3 / 4 / 4; }

.hole {
	background-color: #654321;
	border-radius: 50%;
	display: inline-block;
	height: 4em;
	max-width: 100%;
	width: 4em;
}

/**
 * Score
 */
#stats {
	display: grid;
	font-size: 1.3125em;
	font-weight: bold;
	grid-template-columns: repeat(2, 1fr);
	grid-template-rows: 1fr;
	grid-column-gap: 0;
	grid-row-gap: 0;
	margin-bottom: 0.5em;
}

#score { grid-area: 1 / 1 / 2 / 2; }
#timer {
	grid-area: 1 / 2 / 2 / 3;
	text-align: right;
}


/**
 * Buttons
 */
#start-game {
	background-color: #ff4136;
	border: none;
	color: #ffffff;
	display: block;
	font-size: 1.3125em;
	margin: 2em auto 0;
	padding: 0.5em 2em;
}

#start-game:focus,
#start-game:hover {
	background-color: #c5342b;
}

button {
	cursor: pointer;
}

button:active {
	box-shadow: inset 0 0.15625em 0.25em rgba(0, 0, 0, 0.15), 0 1px 0.15625em rgba(0, 0, 0, 0.05);
	outline: 0;
}


/**
 * Mole
 */

.mole {
	background: transparent;
	border: none;
	display: inline-block;
	padding: 0;

}

.mole svg {
	fill: #654321;
	height: 4.307692308em;
	max-width: 100%;
	width: 4em;
}
              
            
!

JS

              
                //
// Variables
//

var timer, moles;

//
// Methods
//

/**
 * Get a random integer with a minimum and maximum value
 * @param  {Integer} min  The minimum value
 * @param  {Integer} max  The maximum value
 * @return {Integer}      A random number
 */
var randomNumber = function (min, max) {
	return Math.floor(Math.random() * (max - min + 1) + min);
};

var getMoleNumber = function (props) {
	if (props.seconds < 10) return randomNumber(2, 3);
	if (props.seconds < 20) return randomNumber(1, 2);
	return 1;
};

var getMoleLocations = function (props) {
	var num = getMoleNumber(props);
	var locations = [];
	for (var i = 0; i < num; i++) {
		locations.push(randomNumber(0, props.holes - 1));
	}
	return locations;
};

var getMole = function (id) {
	return `
		<button class="mole" data-mole="${id}">
			'<svg height="1em" width="0.928571429em" viewBox="0 0 98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9 1.6-5.5l-3.3-2.2c-1.5 2.3-2.3 4.9-2.3 7.8 0 8.7 7.6 15 18 15z"/></g></svg>
		</button>`;
};

var hasMole = function (moles, index) {
	if (!moles) return false;
	return moles.indexOf(index) > -1;
};

var getStats = function (props) {
	return `
		<div id="stats">
			<div id="score">${props.caught} moles</div>
			<div id="timer">${props.seconds} seconds</div>
		</div>`;
};

var getMoleHoles = function (props) {
	var html = '';
	for (var i = 0; i < props.holes; i++) {
		html += `<div id="plot${i}" class="plot">${hasMole(props.moles, i) ? getMole(i) : '<span class="hole"></span>'}</div>`;
	}
	return html;
};

var getField = function (props) {
	return `
		<div class="field">
			${getMoleHoles(props)}
		</div>`;
};

var app = new Reef('#app', {
	data: {
		gameState: 'new',
		holes: 9,
		moles: null,
		caught: 0,
		seconds: 25,
	},
	template: function (props) {

		// Create HTML string
		var html = '';

		// If game being played or not
		if (props.gameState === 'playing') {
			return getStats(props) + getField(props);
		} else {
			return getStats(props) +
				`<button id="start-game">${props.gameState === 'new' ? 'Start the Game' : 'Play Again'}</button>`;
		}
	}
});

var stopMoles = function () {
	clearTimeout(moles);
};

var hideMoles = function () {
	moles = setTimeout(function () {
		app.data.moles = null;
		showMoles();
	}, randomNumber(1500, 3500));
};

var showMoles = function () {
	moles = setTimeout(function () {
		app.data.moles = getMoleLocations(app.data);
		hideMoles();
	}, randomNumber(100, 300));
};

var stopTimer = function () {
	clearInterval(timer);
};

var startTimer = function () {
	timer = setInterval(function () {

		// Update the UI
		app.data.seconds--;

		// If there's no time left, end the game
		if (app.data.seconds < 1) {
			endGame();
		}

	}, 1000);
};

var endGame = function () {
	stopTimer();
	stopMoles();
	app.data.gameState = 'gameover';
};

var startGame = function (event) {
	if (event.target.id !== 'start-game') return;
	app.data.gameState = 'playing';
	app.data.moles = 0;
	app.data.seconds = 25;
	startTimer();
	showMoles();
};

var whackMole = function (event) {
	if (!event.target.classList.contains('mole')) return;
	app.data.moles.splice(app.data.moles.indexOf(parseFloat(event.target.getAttribute('data-mole'))), 1);
	app.data.caught++;
};


//
// Inits & Event Listeners
//

document.addEventListener('click', function (event) {
	startGame(event);
	whackMole(event);
});

// Load game into the DOM
app.render();
              
            
!
999px

Console