Today I learned – Poker

Solved problems

Today, I had a day off from work. I decided to make good use of my time and started working on JavaScript homework assignments. One particular task caught my interest. It involved creating a deck of cards, drawing 5 cards randomly, and determining the poker hand.

The task was challenging for me because I had never played poker before, and I had to familiarize myself with the rules at least a bit. Initially, I didn’t know how to start, but I decided to break down the task into smaller parts (deck generation, drawing 5 cards, result-checking algorithm) and tackle each part separately. Properly sorting the selected 5 cards turned out to be crucial, making the implementation of algorithms much easier.

const exampleRoyalFlush = [
    { rank: '10', suit: 'Hearts' },
    { rank: 'J', suit: 'Hearts' },
    { rank: 'Q', suit: 'Hearts' },
    { rank: 'K', suit: 'Hearts' },
    { rank: 'A', suit: 'Hearts' }
];
checkPlayerCards(exampleRoyalFlush); // Royal Flush!

const exampleStraightFlush = [
    { rank: '7', suit: 'Hearts' },
    { rank: '8', suit: 'Hearts' },
    { rank: '9', suit: 'Hearts' },
    { rank: '10', suit: 'Hearts' },
    { rank: 'J', suit: 'Hearts' }
];
checkPlayerCards(exampleStraightFlush); // Straight Flush!

const exampleFourOfAkind = [
    { rank: '9', suit: 'Hearts' },
    { rank: '9', suit: 'Diamonds' },
    { rank: '9', suit: 'Clubs' },
    { rank: '9', suit: 'Spades' },
    { rank: '3', suit: 'Hearts' }
];
checkPlayerCards(exampleFourOfAkind); // Four of a Kind!

const exampleFullHouse = [
    { rank: '9', suit: 'Hearts' },
    { rank: '9', suit: 'Diamonds' },
    { rank: '3', suit: 'Clubs' },
    { rank: '3', suit: 'Spades' },
    { rank: '3', suit: 'Hearts' }
];
checkPlayerCards(exampleFullHouse); // Full House!

const exampleFlush = [
    { rank: '9', suit: 'Hearts' },
    { rank: '8', suit: 'Hearts' },
    { rank: 'J', suit: 'Hearts' },
    { rank: 'A', suit: 'Hearts' },
    { rank: '2', suit: 'Hearts' }
];
checkPlayerCards(exampleFlush); // Flush!

const exampleStraight = [
    { rank: 'A', suit: 'Hearts' },
    { rank: '2', suit: 'Hearts' },
    { rank: '3', suit: 'Diamonds' },
    { rank: '4', suit: 'Hearts' },
    { rank: '5', suit: 'Hearts' }
];
checkPlayerCards(exampleStraight); //Straight!

const exampleThreeOfAKind = [
    { rank: 'A', suit: 'Hearts' },
    { rank: 'J', suit: 'Hearts' },
    { rank: 'J', suit: 'Diamonds' },
    { rank: '4', suit: 'Hearts' },
    { rank: 'J', suit: 'Clubs' }
];
checkPlayerCards(exampleThreeOfAKind); // Three of a Kind!

const exampleTwoPair = [
    { rank: 'A', suit: 'Hearts' },
    { rank: 'J', suit: 'Hearts' },
    { rank: 'J', suit: 'Diamonds' },
    { rank: '4', suit: 'Hearts' },
    { rank: '4', suit: 'Clubs' }
];
checkPlayerCards(exampleTwoPair); // Two Pair!

const exampleOnePair = [
    { rank: '3', suit: 'Hearts' },
    { rank: 'J', suit: 'Spades' },
    { rank: 'J', suit: 'Diamonds' },
    { rank: '4', suit: 'Hearts' },
    { rank: '2', suit: 'Clubs' }
];
checkPlayerCards(exampleOnePair); // One Pair!

The task was enjoyable, and it was a great opportunity to practice algorithms. It’s always something different – a new challenge and a chance to expand my knowledge into other areas.

Leave a Reply

Your email address will not be published. Required fields are marked *