Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
How to make an ACE be equivalent to 14 and to 1 in a sequence?

Im making a poker game and i need to find a sequence of 5 cards into a array with 7 cards. As some cards are figures like a Queen, King or Ace, i mapped the values into a separate Array that i use to compare. On the case in question it found the right answer. But if i try to make a straight with A, 2, 3, 4, 5 as the poker rules are, i can't.

const CARD_VALUE_MAP = {
  "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,
}

And then i use this mapping to go through the cards with this code

for(let i=0; i<6; i++){
              if(CARD_VALUE_MAP[finalhand[i].value] === CARD_VALUE_MAP[finalhand[i+1].value]-1){
                count++;
                if(count === 5){
                console.log("straight");
                return 500;
                }
               }
}
  • finalhand looks like this:
  finalhand = [{suit: '♥', value: '3'}, 
  {suit: '♥', value: 'J'}, 
  {suit: '♥', value: 'Q'}, 
  {suit: '♥', value: 'K'},
  {suit: '♥', value: 'A'}
  {suit: '♥', value: '10'},
  {suit: '♦', value: '4'}]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

There's a lot of different way to do this, but based on what you already have, here is the simplest changes: First, set A: 14 and then sort the finalhand (which it appears you are already doing).

Then change the code like so:

count = 0;
if (CARD_VALUE_MAP[finalhand[0].value] = "2" && CARD_VALUE_MAP[finalhand[6].value] = A) {
    count++;
}
for(let i=0; i<6; i++){
              if(CARD_VALUE_MAP[finalhand[i].value] === CARD_VALUE_MAP[finalhand[i+1].value]-1){
                count++;
                if(count === 4){
                console.log("straight");
                return 500;
                }
              else{
                count = 0;
                }
              }
}

This does an initial check at the low-end for an Ace and if it exists, increments the count before the loop starts.

I also added the else { count = 0; } branch because just having five adjacent cards in hand is not a straight if there are any gaps between them.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is a Proof-Of-Concept (POC) card value solution to get you started. It works, but it's not the best code.

const CARD_VALUE_MAP = {
  "2": 2,
  "3": 3,
  "4": 4,
  "5": 5,
  "6": 6,
  "7": 7,
  "8": 8,
  "9": 9,
  "10": 10,
  J: 11,
  Q: 12,
  K: 13,
}

const aceValues = function() {
    return Object.assign(
        Object.assign({}, CARD_VALUE_MAP), 
        {
            AL: CARD_VALUE_MAP[2] - 1,    // Ace Low
            AH: CARD_VALUE_MAP["K"] + 1,  // Ace High
        },
    );
}();

function rankStraight(hand) {
    if (hand.length < 5) {
        return [];
    }

    let cards = [];
    for (let card of hand) {
        if (card === "A") {
            cards.push("AL");
            card = "AH";
        }
        cards.push(card);
    }
    cards.sort(function(a, b) {
        return aceValues[b] - aceValues[a];
    }); 

    let straight = [cards[0]];
    for (let [i, card] of cards.entries()) {
        let last = straight[straight.length - 1];
        if (card === last) {
            continue;
        }
        if ((aceValues[last] - aceValues[card]) !== 1) {
            straight = [card];
            continue
        }
        if (straight.push(card) >= 5) {
            break;
        }
        if ((straight.length + (cards.length - (i + 1))) < 5) {
            break;
        }
    }
    if (straight.length != 5) {
        return [];
    }
    if (straight[0] === "AH") {
        straight[0] = "A";
    }
    if (straight[straight.length - 1] === "AL") {
        straight[straight.length - 1] = "A";
    }
    return straight;
}

let hand = [];
hand = ["A", "K", "Q","J",10];
console.log(rankStraight(hand)); 
hand = [5, 4, 3, 2, "A"];
console.log(rankStraight(hand));
hand = ["A", 5, 4, 3, 2];
console.log(rankStraight(hand));
hand = ["A", 5, 7, 4, 3, "K", 2, "A"];
console.log(rankStraight(hand));

$ node poker.js
[ 'A', 'K', 'Q', 'J', 10 ]
[ 5, 4, 3, 2, 'A' ]
[ 5, 4, 3, 2, 'A' ]
[ 5, 4, 3, 2, 'A' ]
$ 
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda