Skip to content

Instantly share code, notes, and snippets.

@lewdev
Last active October 17, 2025 05:51
Show Gist options
  • Select an option

  • Save lewdev/b5aa00553ad50c077e987d23ed3cfb37 to your computer and use it in GitHub Desktop.

Select an option

Save lewdev/b5aa00553ad50c077e987d23ed3cfb37 to your computer and use it in GitHub Desktop.
๐Ÿ”ข Array Shuffling Methods
<style>*{font-size: 50px}</style>
<div id=o></div>
<div id=msg></div>
<script>
const BACK = "๐ŸŽด";
const FOOD = [..."๐Ÿ„๐Ÿ…๐Ÿ†๐Ÿ‡๐Ÿˆ๐Ÿ‰๐ŸŠ๐Ÿ‹๐ŸŒ๐Ÿ๐ŸŽ๐Ÿ๐Ÿ๐Ÿ‘๐Ÿ’๐Ÿ“๐Ÿ”๐Ÿ•๐Ÿ–๐Ÿ—๐Ÿ˜๐Ÿ™๐Ÿš๐Ÿ›๐Ÿœ๐Ÿ๐Ÿž๐ŸŸ๐Ÿ ๐Ÿก๐Ÿข๐Ÿฃ๐Ÿค๐Ÿฅ๐Ÿฆ๐Ÿง๐Ÿจ๐Ÿฉ๐Ÿช๐Ÿซ๐Ÿฌ๐Ÿญ๐Ÿฎ๐Ÿฏ๐Ÿฐ๐Ÿฑ๐Ÿฒ๐Ÿณ"];
const TYPE_COUNT = 4;
const COLUMNS = Math.ceil(Math.sqrt(TYPE_COUNT * 2));
const cards = [];
let indexA = indexB = -1;
for (let i = 0; i < TYPE_COUNT; i++) {
cards.push(a = FOOD[i], a);
}
let matched, unflipTimeout, startTime, attempts = 0;
const shuffle = r => {for(i=r.length;--i;){t=r[i];r[i]=r[j=Math.random()*(i+1)|0];r[j]=t}return r};
const init = _ => {
matched = new Array(TYPE_COUNT * 2).fill(0);
attempts = 0;
shuffle(cards);
startTime = new Date().getTime();
msg.innerHTML = "Start!";
};
const unflipCards = _ => {
indexA = indexB = -1;
msg.innerHTML = `Attempts: ${attempts}`;
render();
};
render = _=> o.innerHTML = cards.map((f, i) => (matched[i] || [indexA, indexB].includes(i) ? `<button>${cards[i]}</button>` : `<button onclick="flip(${i})">${BACK}</button>`) + ((i + 1) % COLUMNS ? '' : '<br>')).join``;
const flip = index => {
if (unflipTimeout) clearTimeout(unflipTimeout);
if (indexA === -1) indexA = index;
else if (indexB === -1) {
indexB = index;
if (cards[indexA] === cards[indexB]) {
matched[indexA] = matched[indexB] = 1;
msg.innerHTML = "Match!";
}
else attempts++;
// win condition
if (matched.filter(a => a).length === matched.length) {
const totalTime = (new Date().getTime() - startTime) / 1e3;
msg.innerHTML = `Win! Attempts: ${attempts}, Cards: ${cards.length}, Time: ${totalTime}s`;
}
else unflipTimeout = setTimeout(_=> unflipCards(), 2e3);
}
else unflipCards();
render();
};
init();
unflipCards();
//for(i=0x1f1e6;i<0x1F6FF;i++)if(i<0x1F200||i>0x1F2FF)o.innerHTML+=String.fromCodePoint(i);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment