ポーカーのハンドはすべて、カードの相対的なランクやスートに依存します。
ランクとスーツを決定することから始めて、いくつかの効用関数を書くことをお勧めします。
したがって、あなたの表現のカードはint
0..51からです。いくつかの便利な関数(擬似コード)は次のとおりです。
// 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
}
一連のハンドのランクとスーツを取得できるようになったので、それらを操作するためのユーティリティをいくつか作成できます。
// 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)
}
さらにいくつかのユーティリティ関数が必要になりますが、これらを使用すると、フラッシュまたはストレートを探すことができます。
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)
}
等々。
一般的に、あなたは可能なポーカーハンドに対して各ハンドをチェックする必要があります。最高のものから始めて、ハイカードに向かって進みます。実際には、関係を区別するためにそれ以上のものが必要になります(2人のプレーヤーがフルハウスを持っており、勝者は、上位3人がフルハウスを作っているプレーヤーです)。したがって、キッカーなど、2つの手を互いにランク付けするためにもう少し情報を保存する必要があります。
// 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))
}
それが私の一般的なアプローチです。ポーカーハンドランキングアルゴリズムに関する豊富な情報があります。ユニット1:PeterNorvigのUdacityコースDesignofComputerProgramsのWinningPokerHandsをお楽しみいただけます。