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 StraightFlush extends Combination { constructor() { super("Straight flush", 8, 8); }
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; } let flushCards = counts[flusharr]; let sorted_rank = [...new Set(flushCards.map((card) => card.rank))].sort((a, b) => Number(b) - Number(a)); const Ace = sorted_rank.includes("A") && sorted_rank.includes("K"); const Sequentialized_Ace = ["5", "4", "3", "2"].includes(sorted_rank[0]) && Ace; if ((!Ace && sorted_rank.length < 5) || (!Sequentialized_Ace && sorted_rank.length < 5)) { return null; } if (Sequentialized_Ace) { sorted_rank.shift(); sorted_rank.push("14"); } let rank_index = 0, possible_straight = [sorted_rank[0]]; for (let i = 1; i < sorted_rank.length; i++) { if (Number(sorted_rank[i - 1]) === Number(sorted_rank[i]) + 1) { possible_straight.push(sorted_rank[i]); } else if (Number(sorted_rank[i - 1]) !== Number(sorted_rank[i])) { rank_index = i; possible_straight = [sorted_rank[rank_index]]; } let remainingCards = []; for (let i = 0; i < flushCards.length; i++) { let test = true; if (possible_straight.indexOf(flushCards[i].rank) !== -1) { possible_straight.splice(possible_straight.indexOf(flushCards[i].rank), 1); test = false; } if (test === true) { remainingCards.push(flushCards[i]); } } if (possible_straight.length === 0) { let straightArr = []; for (let j = 0; j < flushCards.length; j++) { if (sorted_rank.indexOf(flushCards[j].rank) !== -1) { straightArr.push(flushCards[j]); } } return { combination: this, cards: straightArr.slice(0, 5), }; } } return null; } - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.