How to resolve the algorithm Poker hand analyser step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Poker hand analyser step by step in the JavaScript programming language

Table of Contents

Problem Statement

Create a program to parse a single five card poker hand and rank it according to this list of poker hands.

A poker hand is specified as a space separated list of five playing cards. Each input card has two characters indicating face and suit.

Faces are:    a, 2, 3, 4, 5, 6, 7, 8, 9, 10, j, q, k Suits are:    h (hearts),   d (diamonds),   c (clubs),   and   s (spades),   or alternatively,   the unicode card-suit characters:    ♥ ♦ ♣ ♠

Duplicate cards are illegal. The program should analyze a single hand and produce one of the following outputs:

The programs output for the above examples should be displayed here on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Poker hand analyser step by step in the JavaScript programming language

This JavaScript code defines a function, analyzeHand, that takes a string representing a hand of cards as input and returns a string describing the hand's rank:

  • FACES and SUITS are constant arrays containing the valid card faces and suits, respectively.

  • analyzeHand begins by processing the input hand string, separating the actual cards from any "joker" cards. The function counts the number of jokers and maps each card to its corresponding face and suit indices in the FACES and SUITS arrays.

  • It checks for invalid hands where cards are duplicated or have invalid face or suit values.

  • Next, it calculates various hand characteristics:

    • flush: Checks if all cards have the same suit.
    • groups: Counts the number of cards with each face value.
    • shifted: Shifts the face values by one to calculate the distance between the highest and lowest face values.
    • straight: Checks if the hand is a straight (a sequence of five consecutive face values).
  • Finally, the function uses the calculated characteristics to determine the hand's rank based on the following rules:

    • five-of-a-kind: Five cards of the same face value.
    • straight-flush: A straight with all cards of the same suit.
    • four-of-a-kind: Four cards of the same face value.
    • full-house: Three cards of one face value and two cards of another face value.
    • flush: Five cards of the same suit.
    • straight: Five consecutive face values.
    • three-of-a-kind: Three cards of the same face value.
    • two-pair: Two sets of two cards with the same face value.
    • one-pair: Two cards of the same face value.
    • high-card: No special combinations; the highest-ranking card determines the hand.
  • The code includes a set of test hands and prints the rank of each hand to the console.

Source code in the javascript programming language

const FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k', 'a'];
const SUITS = ['♥', '♦', '♣', '♠'];

function analyzeHand(hand){
	let cards  = hand.split(' ').filter(x => x !== 'joker');
	let jokers = hand.split(' ').length - cards.length;
	
	let faces = cards.map( card => FACES.indexOf(card.slice(0,-1)) );
	let suits = cards.map( card => SUITS.indexOf(card.slice(-1)) );
	
	if( cards.some( (card, i, self) => i !== self.indexOf(card) ) || faces.some(face => face === -1) || suits.some(suit => suit === -1) ) 
		return 'invalid';
	
	let flush    = suits.every(suit => suit === suits[0]);
	let groups   = FACES.map( (face,i) => faces.filter(j => i === j).length).sort( (x, y) => y - x );
	let shifted  = faces.map(x => (x + 1) % 13);
	let distance = Math.min( Math.max(...faces) - Math.min(...faces), Math.max(...shifted) - Math.min(...shifted));
	let straight = groups[0] === 1 && distance < 5;
	groups[0] += jokers;
	
	if      (groups[0] === 5)                    return 'five-of-a-kind'
	else if (straight && flush)                  return 'straight-flush'
	else if (groups[0] === 4)                    return 'four-of-a-kind'
	else if (groups[0] === 3 && groups[1] === 2) return 'full-house'
	else if (flush)                              return 'flush'
	else if (straight)                           return 'straight'
	else if (groups[0] === 3)                    return 'three-of-a-kind'
	else if (groups[0] === 2 && groups[1] === 2) return 'two-pair'
	else if (groups[0] === 2)                    return 'one-pair'
	else                                         return 'high-card';
}


let testHands = [
	"2♥ 2♦ 2♣ k♣ q♦", 
	"2♥ 5♥ 7♦ 8♣ 9♠", 
	"a♥ 2♦ 3♣ 4♣ 5♦", 
	"2♥ 3♥ 2♦ 3♣ 3♦", 
	"2♥ 7♥ 2♦ 3♣ 3♦", 
	"2♥ 7♥ 7♦ 7♣ 7♠", 
	"10♥ j♥ q♥ k♥ a♥", 
	"4♥ 4♠ k♠ 5♦ 10♠",
	"q♣ 10♣ 7♣ 6♣ 4♣",
	"joker 4♣ k♣ 5♦ 10♠",
	"joker 2♦ 2♠ k♠ q♦",
	"joker 3♥ 2♦ 3♠ 3♦",
	"joker 7♥ 7♦ 7♠ 7♣",
	"joker 2♦ joker 4♠ 5♠",
	"joker 2♠ joker a♠ 10♠",
	"joker q♦ joker a♦ 10♦"
];
	
for(hand of testHands) console.log(hand + ": " + analyzeHand(hand));


  

You may also check:How to resolve the algorithm Function composition step by step in the LFE programming language
You may also check:How to resolve the algorithm Keyboard input/Flush the keyboard buffer step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Five weekends step by step in the Julia programming language
You may also check:How to resolve the algorithm Non-decimal radices/Convert step by step in the Ada programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Euphoria programming language