Untitled

mail@pastecode.io avatar
unknown
javascript
7 months ago
3.4 kB
3
Indexable
Never
let hand = [{ suit: 'hearts', value: '4' }, { suit: 'clubs', value: 'A' }]
let communityCards = [
    { suit: 'diamonds', value: '2' },
    { suit: 'spades', value: '5' },
    { suit: 'hearts', value: '6' },
    { suit: 'clubs', value: '7' },
    { suit: 'diamonds', value: '8' },
]

function pokercheker(hand, communityCards) {
    let suits = ['hearts', 'clubs', 'diamonds', 'spades']
    let cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
    if (hand === undefined || communityCards === undefined) throw "Error: Argument Not passed"
    if (!Array.isArray(hand) || !Array.isArray(communityCards)) throw "Error: hands and community cards should be array of objects containing suit and value"
    if (hand.length !== 2) throw "Error: Hand must have exactly 2 cards"
    if (communityCards.length < 3 || communityCards.length > 5) throw "Error: You can have 3-5 Community Cards"
    for (let card of hand) {
        if (Object.keys(card).length !== 2) throw "Error: Card should have two keys suit and value"
        if (!('suit' in card)) throw "Error: Card should have suit"
        if (!('value' in card)) throw "Error: Card should have value"
        if (!suits.includes(card.suit)) throw "Error: Suit can only be 'hearts','clubs','diamonds','spades' "
        if (!cards.includes(card.value)) throw "Error: Value can only be '2','3','4','5','6','7','8','9','10','J','Q','K','A' "
    }
    for (let card of communityCards) {
        if (Object.keys(card).length !== 2) throw "Error: Card should have two keys suit and value"
        if (!('suit' in card)) throw "Error: Card should have suit"
        if (!('value' in card)) throw "Error: Card should have value"
        if (!suits.includes(card.suit)) throw "Error: Suit can only be 'hearts','clubs','diamonds','spades' "
        if (!cards.includes(card.value)) throw "Error: Value can only be '2','3','4','5','6','7','8','9','10','J','Q','K','A' "
    }
}

function strainghflush(cards, map) {
    cards = cards.map(card => ({
        suit: card.suit,
        value: map[card.value]
    }))
    cards.sort((a, b) => a.value - b.value);
    let straight = false;
    for (let i = 0; i <= cards.length - 5; i++) {
        if (
            cards[i + 4].value - cards[i].value === 4 && cards[i].suit === cards[i + 1].suit && cards[i].suit === cards[i + 2].suit &&
            cards[i].suit === cards[i + 3].suit &&
            cards[i].suit === cards[i + 4].suit
        ) {
            straight = true;
            break;
        }
    }
    if (straight) return true
    else return false
}

function evaluatePokerHand(hand, communityCards) {
    pokercheker(hand,communityCards)
    let cards = [...hand, ...communityCards]
    const m1 = {
        '2': 2, '3': 3, '4': 4, '5': 5, '6': 6,
        '7': 7, '8': 8, '9': 9, '10': 10,
        'J': 11, 'Q': 12, 'K': 13, 'A': 14
    };
    const m2 = {
        '2': 2, '3': 3, '4': 4, '5': 5, '6': 6,
        '7': 7, '8': 8, '9': 9, '10': 10,
        'J': 11, 'Q': 12, 'K': 13, 'A': 1
    };
    if (strainghflush(cards, m1) || strainghflush(cards, m2)) return "Straight Flush"
    let ob = {}
    for (let card of cards) {
        ob[card.value] = (ob[card.value] || 0) + 1
        if (ob[card.value] === 3) return "Three of a Kind"
    }
    for (let card in ob) {
        if (ob[card] === 2) return "Pair"
    }
    return "High Card"
}

console.log(evaluatePokerHand(hand, communityCards))