1

私が取り組んでいる奇妙な小さなプロジェクト。答える前に、はい、vbscriptがおそらくこれに使用するのに最悪の言語であることを私は知っています。

各プレイヤーが何を持っているかを判断するのに助けが必要です。各カードには一意の番号があります(これは、その横に♥♦♣♠が付いたポーカー値に「変換」されます)。例えば:

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

等々。自分が持っているハンドを判断するのに助けが必要です。私はいくつかの方法を考えました。1つ目は、各カード値間のデルタを使用することです。たとえば、ストレートは次のようになります。

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

等々。たとえば、カード3、3 + 1 + 0、3 + 2 + 13、3 + 3 +(13 * 3)、3 + 4 +(13 * 2)

私に与えるだろう:4♥5♥6♦7♠8♣</ p>

私の質問は、これに正規表現を使用する必要がありますか?すべての手をハードコーディングせずに、彼が持っている手をコンピューターに伝える最良の方法は何ですか?

編集:ここに完全なコード:https ://codereview.stackexchange.com/questions/21338/how-to-tell-the-npc-what-hand-it-has

4

1 に答える 1

1

ポーカーのハンドはすべて、カードの相対的なランクやスートに依存します。

ランクとスーツを決定することから始めて、いくつかの効用関数を書くことをお勧めします。

したがって、あなたの表現のカードはint0..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をお楽しみいただけます。

于 2013-02-05T20:33:30.703 に答える