Last active
May 21, 2022 13:40
-
-
Save vicsstar/c439795a8c27d04cf26dedca0d6e2bf0 to your computer and use it in GitHub Desktop.
FLAMES game, written in JavaScript - I liked the logic behind the game and wanted to represent it in code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// flames.js | |
// FLAMES game. Algorithm can be found here: http://flamesgame.appspot.com/algorithm | |
const getFlame = (name1, name2) => | |
name1.toLowerCase().split(''). // take all characters in "name1", to compare | |
filter(c => name2.toLowerCase().includes(c)). // keep only characters (in "name1") also found in "name2" | |
reduce((result, c) => [ | |
result[0].replace(new RegExp(`${c}`, 'g'), ''), // replace common characters in "name1" | |
result[1].replace(new RegExp(`${c}`, 'g'), '') // replace common characters in "name2" | |
], [name1.toLowerCase(), name2.toLowerCase()]). | |
reduce((length, letters) => [length.shift() + letters.length], [0]). // determine length of characters left, after removal | |
map(length => { | |
const findStop = (flames, startAt) => { | |
if (flames.length === 1) return flames; // stop, if one flame character is left | |
const position = new Array(length).fill(0). // move "length" number of times | |
reduce(beginAt => beginAt + 1 > flames.length ? 1 : beginAt + 1, startAt - 1) // determine where to stop, in one move | |
flames.splice(position - 1, 1); // remove the "flame" character where we stopped | |
return findStop(flames, position); // find the next stop | |
}; | |
return findStop('FLAMES'.split(''), 1)[0]; // begin finding stops | |
}). | |
map(result => | |
['Friendship', 'Love', 'Affection', 'Marriage', 'Enemy', 'Sibling']. | |
filter(flame => flame.toLowerCase().startsWith(result.toLowerCase()))[0] // present result in readable form | |
)[0]; | |
alert(getFlame('Victor', 'Chisom')); // sample test: "FLAMES" between my wife and I :D *drum_roll* Marriage! \o/ | |
// comment out the next line, to test | |
// alert(getFlame(prompt('Your name'), prompt('Partner\'s name'))); | |
// test with other names at https://es6console.com/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Goood