Skip to content

Instantly share code, notes, and snippets.

@Ephellon
Created June 8, 2020 00:41
Show Gist options
  • Save Ephellon/001bf322a240ffcfbb20fac1d37517bd to your computer and use it in GitHub Desktop.
Save Ephellon/001bf322a240ffcfbb20fac1d37517bd to your computer and use it in GitHub Desktop.
Quick Gist of Uno deck
class Deck {
constructor(...suits) {
let self = [],
uuid = (...values) => values.join('-') + '-' + Math.random().toString(36).replace(/^\d*\./, '').slice(-4),
properties = { configurable: false, enumerable: false, writable: false };
for(let suit of suits) {
// Cards that have 2 copies (suit-oriented)
for(let card of [1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C'])
self.push(
{ value: card, owner: null, uuid: uuid(suit, card), suit },
{ value: card, owner: null, uuid: uuid(suit, card), suit }
);
// Cards that have 1 copy (suit-oriented)
for(let card of [0])
self.push({ value: card, owner: null, uuid: uuid(suit, card), suit });
// Cards that have 4 copies (suit-free)
for(let card of ['D', 'E'])
self.push({ value: card, owner: null, uuid: uuid(suit, card) });
}
self = self.sort(() => Math.random() >= 0.5? +1: -1);
for(let card of self)
this[card.uuid] = card;
for(let property of 'length'.split(' '))
Object.defineProperty(this, property, { value: self[property], ...properties });
Object.defineProperty(this, '__introspection__', { value: [...self], ...properties });
return this;
}
shuffle() {
let self = this.__introspection__;
this.__introspection__ = self = self.sort(() => Math.random() >= 0.5? +1: -1);
for(let card of self)
this[card] = self[card];
return this;
}
}
/* 0-9, A (skip), B (reverse), C (+2), D (wild), E (+4 & wild) */
let Uno = new Deck('red', 'yellow', 'green', 'blue');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment