Skip to content

Instantly share code, notes, and snippets.

@drenther drenther/initialState.js
Last active Mar 5, 2019

Embed
What would you like to do?
Initial State for Line up the Heroes and the shuffle algorithm
/** App.js **/
import { HEROES, COMICS } from './custom/data';
import { shuffle, GAME_STATE } from './custom/utils';
const initialState = {
// we initialize the state by populating the bench with a shuffled collection of heroes
bench: shuffle(HEROES),
[COMICS.DC]: [],
[COMICS.MARVEL]: [],
gameState: GAME_STATE.READY,
timeLeft: 0,
};
/** custom/utils.js **/
// the Knuth shuffle algorithm
export function shuffle(array) {
let currentIndex = array.length;
let temporaryValue;
let randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// enums for representing the game state
export const GAME_STATE = {
READY: 'ready',
PLAYING: 'playing',
DONE: 'done',
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.