Domanda

Odd little project I am working on. Before you answer, yes, I know that vbscript is probably the worst language to use for this.

I need help determining what each player has. Each card has a unique number (which I 'translate' into it's poker value with a ♥♦♣♠ next to it). For example:

A♥ = 0
2♥ = 1
3♥ = 2
...

and so on. I need help determining what hand I have. I have thought of a few ways. The first is using the delta between each card value. For example, a straight would be:

n
n +/- (1+ (13 * (0 or 1 or 2 or 3)))
n +/- (2 + (13 * (0 or 1 or 2 or 3 )))
... 

and so on. For example cards 3, 3+1+0, 3+2+13, 3+3+(13*3), 3+4+(13*2)

would give me: 4♥ 5♥ 6♦ 7♠ 8♣

My questions is, should I attempt to use regex for this? What is the best way to tell the computer what hand he has without hardcoding every hand?

EDIT: FULL CODE HERE: https://codereview.stackexchange.com/questions/21338/how-to-tell-the-npc-what-hand-it-has

È stato utile?

Soluzione

Poker hands all depend on the relative ranks and/or suits of cards.

I suggest writing some utility functions, starting with determining a rank and suit.

So a card in your representation is an int from 0..51. Here are some useful functions (pseudo-code):

// returns rank 0..12, where 0 = Ace, 12 = King
getRank(card) {
    return card % 13;
}


// returns suit 0..3, where 0 = Heart, 1 = Diamond, 2 = Club, 3 = Spade
getSuit(card) {
    return card / 13;  // or floor(card / 13) if lang not using floored division
}

Now that you can obtain the rank and suit of a set of hands you can write some utilities to work with those.

// sort and return the list of cards ordered by rank
orderByRank(cards) {
    // ranked = []
    // for each card in cards:
    //   get the rank
    //   insert into ranked list in correct place
}

// given a ranked set of cards return highest number of identical ranks
getMaxSameRank(ranked) {
    duplicates = {}  // map / hashtable
    for each rank in ranked {
        duplicates[rank] += 1
    }
    return max(duplicates.vals())
}

// count the number of cards of same suit
getSameSuitCount(cards) {
    suitCounts = {} // a map or hashtable if possible
    // for each card in cards:
    //   suitCounts{getSuit(card)} += 1
    // return max suit count (highest value of suitCounts)
}

You will need some more utility functions, but with these you can now look for a flush or straight:

isFlush(cards) {
    if (getSameSuitCount(cards) == 5) {
        return true
    }
    return false
}

isStraight(cards) {
    ranked = orderByRank(cards)
    return ranked[4] - ranked[0] == 3 && getMaxSameRank(ranked) == 1     
}

isStraightFlush(cards) {
    return isFlush(cards) && isStraight(cards)
}

And so on.

In general, you will need to check each hand against the possible poker hands, starting with the best, working down to high card. In practice you will need more than that to differentiate ties (two players have a fullhouse, the winner is the player with the higher ranked three of a kind making their fullhouse). So you need to store a bit more information for ranking two hands against one another, such as kickers.

// simplistic version
getHandRanking(cards) {
  if (isStraightFlush()) return STRAIGHT_FLUSH
  if (isQuads()) return QUADS
  ...
  if (isHighCard) return HIGH_CARD
}

getWinner(handA, handB) {
  return max(getHandRanking(handA), getHandRanking(handB))
}

That would be my general approach. There is a wealth of information on poker hand ranking algorithms out there. You might enjoy the Unit 1: Winning Poker Hands from Peter Norvig's Udacity course Design of Computer Programs

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top