123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              	<div class="wrapper">
		<div id="board">
			<div class="pad green" id="0"></div>
			<div class="pad red" id="1"></div>
			<div class="pad yellow" id="2"></div>
			<div class="pad blue" id="3"></div>
		</div>

		<div class="controlBoard" id="controls">
			<h1 class="title">SIMON</h1>
			<div class="switchPanelA" id="panel_a">
				<div class="switch" id="screen">00</div>
				<div class="btn switch" id="start">Start</div>
				<div class="btn switch" id="strict">Strict</div>
			</div>
			<div class="switchPanelB" id="panel_b">
				<div class="label switchB">Reset:</div>
				<div id="resetBtn" class="reset switchB"></div>
			</div>
		</div><!-- end panel A -->
	</div><!-- end wrapper -->
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Play');

body {
	background: #e5e7e6;
}

.wrapper {
	width: 80%;
	margin: 0 auto;
}

#board {
	border: 10px solid #000;
	border-radius: 100%;
	box-shadow: 0px 0px 16px 0px #000;
	width: 600px;
	height: 600px;
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
}

.pad {
	width: 300px;
	height: 300px;
	float: left;
	box-sizing: border-box;
	transition: all 200ms ease-in-out;
	opacity: 0.6;
}

.pressed {
	opacity: 1;
}

.green {
	background: radial-gradient(ellipse, #00bf8f, #001510);
	background: -webkit-radial-gradient(ellipse, #00bf8f, #001510);
	background: -moz-radial-gradient(ellipse, #00bf8f, #001510);
	background-position: 20px 20px;
	border-top-left-radius: 100%;
}

.red {
	background: radial-gradient(ellipse, #da4453, #89216b);;
	background: -webkit-radial-gradient(ellipse, #da4453, #89216b);
	background: -moz-radial-gradient(ellipse, #da4453, #89216b);
	border-top-right-radius: 100%;
}

.yellow {
	background: radial-gradient(ellipse, #f7ff00, #db36a4);
	background: -webkit-radial-gradient(ellipse, #f7ff00, #db36a4);
	background: -moz-radial-gradient(ellipse, #f7ff00, #db36a4);
	border-bottom-left-radius: 100%;
}

.blue {
	background: radial-gradient(ellipse, #00d2ff, #3a7bd5);
	background: -webkit-radial-gradient(ellipse, #00d2ff, #3a7bd5);
	background: -moz-radial-gradient(ellipse, #00d2ff, #3a7bd5);
	border-bottom-right-radius: 100%;
}

.controlBoard {
	background: radial-gradient(ellipse, #232526, #414345);
	background: -webkit-radial-gradient(ellipse, #232526, #414345);
	background: -moz-radial-gradient(ellipse, #232526, #414345);
	border: 1px solid rgba(0, 0, 0, 1);
	border-radius: 100%;
	box-shadow: 0px 0px 16px 0px rgba(0, 0, 0, 0.9);
	width: 300px;
	height: 300px;
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	opacity: 0.9;
}

.controlBoard .title {
	text-align: center;
	margin-right: 5px;
	margin-top: 25px;
	color: #00d2ff;
	font-size: 42px;
	font-family: 'Play', sans-serif, Helvetica;
}

.switchPanelA {
	width: 100%;
	height: 60px;
	margin-top: 45px;
}

.switchPanelA .switch {
	float: left;
	box-sizing: border-box;
}

.switchPanelA #screen {
	border: 1px solid #00d2ff;
	border-radius: 15px;
	height: 100%;
	width: 75px;
	margin-left: 20px;
	padding-top: 2px;
	color: #da4453;
	font-size: 48px;
	text-align: center;
}

.switchPanelA .btn {
	border: 1px solid #00d2ff;
	border-radius: 4px;
	width: 75px;
	height: 30px;
	text-align: center;
	color: #00d2ff;
	padding-top: 2px;
	font-size: 20px;
	margin-top: 15px;
	margin-left: 15px;
	transition: all 400ms ease-in-out;
}

.switchPanelA .btn:hover {
	box-shadow: 0px 0px 8px 0px #00d2ff;
}

.switchPanelA .btn:active {
	box-shadow: 0px 0px 8px 0px #00bf8f;
	color: #da4453; 
}

/* styles for when strict mode is activated */
.switchPanelA .strictOn {
	border: 1px solid #db36a4;
	color: #db36a4;
}

.switchPanelA .strictOn:hover {
	box-shadow: 0px 0px 8px 0px #db36a4;
}

.switchPanelB {
	width: 80%;
	height: 30px;
	margin: 0 auto;
	margin-top: 20px;
}

.switchPanelB .switchB {
	box-sizing: border-box;
	float: left;
}

.switchPanelB .label {
	width: 60px;
	height: 100%;
	text-align: center;
	color: #00d2ff;
	padding-top: 5px;
	margin-left: 60px;
}

.switchPanelB .reset {
	background: radial-gradient(ellipse, #00d2ff, #3a7bd5);
	background: -webkit-radial-gradient(ellipse, #00d2ff, #3a7bd5);
	background: -moz-radial-gradient(ellipse, #00d2ff, #3a7bd5);
	border: 1px solid #00d2ff;
	border-radius: 100%;
	width: 25px;
	height: 25px;
	margin-top: 2px;
	margin-left: 13px;
	transition: all 400ms ease-in-out;
}

.switchPanelB .reset:hover {
	box-shadow: 0px 0px 8px 0px #00d2ff;
}

.switchPanelB .reset:active {
	background: radial-gradient(ellipse, #da4453, #89216b);
	background: -webkit-radial-gradient(ellipse, #da4453, #89216b);
	background: -moz-radial-gradient(ellipse, #da4453, #89216b);
	border: 1px solid #da4453;
	box-shadow: 0px 0px 8px 0px #da4453;
}
            
          
!
            
              //work in progress
const board = document.getElementById('board');
const strict = document.getElementById('strict');
const start = document.getElementById('start');
const resetBtn = document.getElementById('resetBtn');
const screen = document.getElementById('screen');

let strictMode = false;
let seq = [];
let playerSeq = undefined;
let level = 0;
const maxLevel = 20;
let errCount = 0;
const maxErr = 3;
let turn = false;
let running = false;
let random = 0;
let timer;
//A, E, CS, El - frequencies of musical notes to be played when a pad is chosen by ai or player
const A = 440.00;
const E = 329.6;
const CS = 277.2;
const Eh = 659.3; //octave higher than E
let note;
let now;

const audio = new (window.AudioContext || window.webkitAudioContext)();

class Sound {
	constructor(ctx) {
		this.ctx = ctx;
	}

	init() {
		this.oscillator = this.ctx.createOscillator();
		this.gainNode = this.ctx.createGain();

		this.oscillator.connect(this.gainNode);
		this.gainNode.connect(this.ctx.destination);
		this.oscillator.type = 'sine';
	}

	play(value, time) {
		this.init();

		this.oscillator.frequency.value = value;
		this.gainNode.gain.setValueAtTime(1, this.ctx.currentTime);
		this.oscillator.start(time);
		this.stop(time);
	}

	stop(time) {
		this.gainNode.gain.exponentialRampToValueAtTime(0.001, time + 1);
		this.oscillator.stop(time +1);
	}
}

function startGame() {
	if(running === false) {
		running = true;
		level++;
		padNumber(level);
		while(seq.length < maxLevel) {
			random = Math.floor(Math.random() * 4);
			seq.push(random);
		}
		simonPlay();
	}
}

function reset() {
	strictMode = (strictMode === true) ? true : false;
	seq.length = 0;
	playerSeq = undefined;
	level = 0;
	padNumber(level);
	errCount = 0;
	turn = false;
	running = false;
	//clearInterval needs to be called here
	//to properly reset the game if reset is called 
	//during the ai's turn to play
	clearInterval(timer);
}

function playSound(ele) {
	note = new Sound(audio);
	now = audio.currentTime;
	switch(ele.id) {
		case "0": note.play(Eh, now);
		break;
		case "1": note.play(A, now);
		break;
		case "2": note.play(CS, now);
		break;
		case "3": note.play(E, now);
		break;
	}
}

//light up the pad when it is chosen by the ai or player
function flash(ele, sound = true) {
	if(sound) {
		playSound(ele);
	}
	ele.classList.add('pressed');
	setTimeout(() => {
		ele.classList.remove('pressed');
	}, 700);
}

//display level with padding if less than 10, or without padding if greater than 10
function padNumber(num) {
	if(num < 10) {
		screen.innerText = `0${num}`;
	} else {
		screen.innerText = num;
	}
}

//animate the game pads when the player wins
function onWin() {
	let counter = 0;
	timer = setInterval(() => {
		flash(board.children[seq[counter]], false);
		counter++;

		if(counter >= level) {
			clearInterval(timer);
			reset();
		}
	}, 1000);
}

//animate the game pads when the page loads
function onLoad() {
	const startSeq = [0, 3, 1, 2];
	let counter = 0;
	running = true;
	timer = setInterval(() => {
		flash(board.children[startSeq[counter]]);
		counter++;

		if(counter >= startSeq.length) {
			clearInterval(timer);
			running = false;
		}
	}, 700);
}

//play animation when page loads
onLoad();

//the ai starts to play the seq generated when the game was started
function simonPlay() {
	let counter = 0;

	if(running === true) {
		//setting timer as a global variable
		//solved an issue where the interval would not be cleared
		//if strict was turned on/off during game play
		//preventing the game from resetting properly
		timer = setInterval(() => {
			flash(board.children[seq[counter]]);
			counter++;

			if(counter >= level) {
				clearInterval(timer);
				turn = true;
			}
		}, 1000);
	}
}

function player(ele) {
	if(playerSeq === undefined) {
		//starting this variable at 0 
		//causes issues, initializing to undefined solved the problem
		playerSeq = 0;
	}

	flash(ele);

	//handle errors when the player clicks the wrong pad
	if(seq[playerSeq] != ele.id && strictMode === true) {
		//end the game
		screen.innerText = '--';
		setTimeout(() => {
			flash(board.children[seq[playerSeq]]);
			reset();
			return;
		}, 700);
	} else if(seq[playerSeq] != ele.id && strictMode === false) {
		if(errCount === maxErr) {
			reset();
			return;
		} else {
			errCount++;
      turn = false;
			screen.innerText = '--';
			setTimeout(() => {
				padNumber(level);
				flash(board.children[seq[playerSeq]]);
				playerSeq = undefined;
			}, 1000);
			setTimeout(() => {
				simonPlay();
			}, 1000);
		}
	} else {
		//increment playerSeq if correct pad chosen
		playerSeq++;
	}

	//check for a win
	if(level === maxLevel && playerSeq >= maxLevel) {
		onWin();
		return;
	}

	if(playerSeq === level) {
		turn = false;
		//must be reset to undefined
		playerSeq = undefined;
		level++;
		padNumber(level);
		simonPlay();
	}
}

//event listeners --
board.addEventListener('click', function(event) {
	//check that event.target.id is not equal to board to prevent the game from registering 
	//accidental clicks on the board frame as incorrect sequence input from the player
	if(running === true && turn === true && event.target.id !== "board") {
		player(event.target);
	}
});

start.addEventListener('click', event => {
	startGame();
});

strict.addEventListener('click', event => {
	if(strictMode === false) {
		strictMode = true;
		strict.classList.add('strictOn');
		reset();
	} else if(strictMode === true) {
		strictMode = false;
		strict.classList.remove('strictOn');
		reset();
	}
});

resetBtn.addEventListener('click', event => {
	reset();
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console