Skip to content

Instantly share code, notes, and snippets.

@jtrussell
Last active October 3, 2023 17:46
Show Gist options
  • Save jtrussell/b851907ceba6780a40e51b73f54b7fe9 to your computer and use it in GitHub Desktop.
Save jtrussell/b851907ceba6780a40e51b73f54b7fe9 to your computer and use it in GitHub Desktop.
class Player extends GameObject {
// [... lots o' other stuff ...]
shuffleDeck() {
this.game.emitEvent(
'onDeckShuffled',
{ player: this }
)
this.deck = _.shuffle(this.deck)
if (
this.isTopCardOfDeckVisible() &&
this.deck.length > 0
) {
this.deck[0].facedown = false
this.deck.slice(1).forEach((card) => {
card.facedown = true
})
this.addTopCardOfDeckVisibleMessage()
}
}
// [... more other stuff ...]
}
class Player extends GameObject {
// [... lots o' other stuff ...]
takeMulligan() {
let size = this.hand.length
for (let card of this.hand) {
this.moveCard(card, 'deck')
}
this.shuffleDeck()
this.drawCardsToHand(size - 1)
this.takenMulligan = true
}
// [... more other stuff ...]
}
function random(min, max) {
if (max == null) {
max = min;
min = 0;
}
return min + Math.floor(Math.random() * (max - min + 1));
}
function sample(obj, n, guard) {
if (n == null || guard) {
if (!isArrayLike(obj)) obj = values(obj)
return obj[random(obj.length - 1)]
}
var sample = toArray(obj)
var length = getLength(sample)
n = Math.max(Math.min(n, length), 0)
var last = length - 1
for (var index = 0; index < n; index++) {
var rand = random(index, last)
var temp = sample[index]
sample[index] = sample[rand]
sample[rand] = temp
}
return sample.slice(0, n)
}
function shuffle(obj) {
return sample(obj, Infinity)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment