please fix the code: const suits = ["♥", "♦", "♣", "♠"]; const ranks = ["2", "3", "4",…
- User
please fix the code: const suits = ["♥", "♦", "♣", "♠"]; const ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
class Card { constructor(suit, rank) { this.suit = suit; this.rank = rank; }
toString() { return
${this.rank}${this.suit}; } }function determineWinners(playerHands, boardCards) { const allHands = playerHands.map((hand) => [...hand, ...boardCards]); const bestHands = allHands.map((hand) => getBestHand(hand)); const maxRank = Math.max(...bestHands.map((hand) => hand.rank));
return bestHands.reduce((winners, hand, index) => { if (hand.rank === maxRank) { winners.push({ playerId: index, winningCombinationId: hand.combination.id, winningCards: hand.cards, }); } return winners; }, []); }
function getBestHand(cards) { const combinations = [ RoyalFlush, StraightFlush, FourOfAKind, FullHouse, Flush, Straight, ThreeOfAKind, TwoPairs, Pair ];
for (const Combination of combinations) { const result = Combination.test(cards); if (result != null) { return { rank: Combination.rank, combination: result.combination, cards: result.cards }; } } }
class Combination { constructor(name, id, rank) { this.name = name; this.id = id; this.rank = rank; }
static test(cards) {} }
class RoyalFlush extends Combination { constructor() { super("Royal flush", 9, 9); }
static test(cards) { const counts = {}; for (let card of cards) { counts[card.suit] = (counts[card.suit] || []).concat(card); } const flusharr = Object.keys(counts).filter((key, i, arr) => { if (counts[key].length >= 5) { return key; } }); if (flusharr.length !== 1) { return null; } const flushcards = counts[flusharr][0]; const sorted_rank = [...new Set(flushcards.map((card) => card.rank))].sort().join(""); const seq_pat = "AJKQT"; if (sorted_rank === seq_pat && cards.filter((c) => c.suit === flushcards[0].suit).length >= 5) { return { combination: this, cards: flushcards.filter((card) => seq_pat.indexOf(card.rank) !== -1), }; } else { return null; } return output; }}
///////////////////// function testRoyalFlush() { const royalFlushCards = [ new Card("♥", "A"), new Card("♥", "K"), new Card("♥", "Q"), new Card("♥", "J"), new Card("♥", "10"), new Card("♠", "7"), // should be ignored new Card("♦", "4"), // should be ignored ]; const notRoyalFlushCards = [ new Card("♣", "9"), new Card("♣", "8"), ...royalFlushCards, ];
const royalFlushResult = RoyalFlush.test(royalFlushCards); console.log(royalFlushResult) }
testRoyalFlush();
- Assistant
const suits = ["♥", "♦", "♣", "♠"]; const ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
class Card { constructor(suit, rank) { this.suit = suit; this.rank = rank; }
toString() { return
${this.rank}${this.suit}; } }function determineWinners(playerHands, boardCards) { const allHands = playerHands.map((hand) => [...hand, ...boardCards]); const bestHands = allHands.map((hand) => getBestHand(hand)); const maxRank = Math.max(...bestHands.map((hand) => hand.rank));
return bestHands.reduce((winners, hand, index) => { if (hand.rank === maxRank) { winners.push({ playerId: index, winningCombinationId: hand.combination.id, winningCards: hand.cards, }); } return winners; }, []); }
function getBestHand(cards) { const combinations = [ RoyalFlush, StraightFlush, FourOfAKind, FullHouse, Flush, Straight, ThreeOfAKind, TwoPairs, Pair, ];
for (const Combination of combinations) { const result = Combination.test(cards); if (result != null) { return { rank: Combination.rank, combination: result.combination, cards: result.cards }; } } }
class Combination { constructor(name, id, rank) { this.name = name; this.id = id; this.rank = rank; }
static test(cards) {} }
class RoyalFlush extends Combination { constructor() { super("Royal flush", 9, 9); }
static test(cards) { const counts = {}; for (let card of cards) { counts[card.suit] = (counts[card.suit] || []).concat(card); } const flushSuit = Object.keys(counts).find((key) => counts[key].length >= 5); if (!flushSuit) { return null; } const flushCards = counts[flushSuit]; const sortedRanks = [...new Set(flushCards.map((card) => card.rank))].sort((a, b) => ranks.indexOf(a) - ranks.indexOf(b)); const seqPat = ["A", "K", "Q", "J", "10"]; if (sortedRanks.join("") === seqPat.join("")) { return { combination: this, cards: flushCards.filter((card) => seqPat.indexOf(card.rank) !== -1), }; } else { return null; } }}
function testRoyalFlush() { const royalFlushCards = [ new Card("♥", "A"), new Card("♥", "K"), new Card("♥", "Q"), new Card("♥", "J"), new Card("♥", "10"), new Card("♠", "7"), // should be ignored new Card("♦", "4"), // should be ignored ]; const notRoyalFlushCards = [ new Card("♣", "9"), new Card("♣", "8"), ...royalFlushCards, ];
const royalFlushResult = RoyalFlush.test(royalFlushCards); console.log(royalFlushResult) }
testRoyalFlush();