Skip to content

Instantly share code, notes, and snippets.

@bleikamp
Created April 11, 2017 06:10
Show Gist options
  • Save bleikamp/078fbdc2fe1ec4274bf0b7806cf9e3f2 to your computer and use it in GitHub Desktop.
Save bleikamp/078fbdc2fe1ec4274bf0b7806cf9e3f2 to your computer and use it in GitHub Desktop.
// Goofing off with JS to learn about map, filter, and reduce
// A deck of cards has 4 suits and 13 ranks
const suits = ['s', 'd', 'c', 'h']
const ranks = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
// Turn arrays of ranks and suits into a card deck
// 52 cards
const deck = (ranks, suits) => {
const c = []
ranks.map(rank => suits.map(suit => c.push([rank, suit])))
return c;
}
// All possible 2 card combinations for Hold' Em
// 1,326 hands
const allHands = deck => {
const a = []
for (let i = 0; i < deck.length; i++) {
const card1 = deck[i];
for (let j = i + 1; j < deck.length; j++) {
const card2 = deck[j];
a.push([card1, card2])
}
}
return a;
}
// hand - an array of 2 arrays [[rank, suit], [rank, suit]]
const pocketPair = (hand) => {
if(hand[0][0] == hand[1][0]) {
return true
} else {
return false
}
}
// hand - an array of 2 cards [[rank, suit], [rank, suit]]
const suited = (hand) => {
if (hand[0][1] == hand[1][1]) {
return true
} else {
return false
}
}
const unsuited = (hand) => {
if (suited(hand) || pocketPair(hand)) {
return false
} else {
return true
}
}
const cards = deck(ranks, suits)
const hands = allHands(cards)
const allSuited = hands.filter(suited)
const allPocketPairs = hands.filter(pocketPair)
const allUnsuited = hands.filter(unsuited)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment