Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
¿Cómo hacer que un ACE sea equivalente a 14 y a 1 en una secuencia?

Estoy haciendo un juego de póquer y necesito encontrar una secuencia de 5 cartas en una matriz con 7 cartas. Como algunas cartas son figuras como una reina, un rey o un as, mapeé los valores en una matriz separada que uso para comparar. En el caso en cuestión encontró la respuesta correcta. Pero si trato de hacer una escalera con A, 2, 3, 4, 5 como son las reglas del poker, no puedo.

 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, }

Y luego uso este mapeo para revisar las tarjetas con este código

 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; } } }
  • mano final se ve así:
 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 answers
Answer question

0

Hay muchas formas diferentes de hacer esto, pero según lo que ya tiene, estos son los cambios más simples: primero, configure A: 14 y luego ordene la mano final (lo que parece que ya está haciendo).

Luego cambia el código así:

 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; } } }

Esto hace una verificación inicial en el extremo inferior de un As y, si existe, incrementa la cuenta antes de que comience el bucle.

También agregué else { count = 0; } rama porque tener cinco cartas adyacentes en la mano no es una escalera si hay espacios entre ellas.

about 4 years ago · Juan Pablo Isaza Report

0

Aquí hay una solución de valor de tarjeta de prueba de concepto (POC) para comenzar. Funciona, pero no es el mejor código.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!