使用循环计算骰子扑克结果

时间:2019-05-08 11:05:40

标签: c arrays loops

我有一个Dice poker作业。我设法生成了一个数组,其中包含滚动值的频率,但是我无法编写循环来确定手的值是多少。

希望获得有关使用哪种循环组合来确定我握着哪只手的建议。从那里,我应该能够将其转换为一个函数,并编写一个函数来将该手与其他人进行比较。

#define _CRT_SECURE_NO_WARNINGS
#define handsz 5    //NO ACES IN THE POCKET WITH THIS GUY!
#define diesz 6     //All dice played will have 6 sides
#define ranks 7     //Seven ranks to hold

#include <stdio.h>  
#include <stdlib.h> 
#include <time.h>   

int roll_die(void); 

int main() {
    srand(time(NULL));

    int i;
    int player_hand[handsz] = { 0 };
    int donkeyvalue = 0;    
    int RandomNo;       

    for (i = 0; i < handsz; i++) {
        RandomNo = roll_die(donkeyvalue);  
        player_hand[i] = RandomNo;
    }

    int player_die_count[7] = { 0 }; 
    for (i = 0; i < handsz; i++) {
        player_die_count[player_hand[i]] = player_die_count[player_hand[i]] + 1;
    }
    return 0;
}

int roll_die(void) {
    return (1 + rand() % 6);
}

1 个答案:

答案 0 :(得分:1)

通过数组player_die_count,使用循环,您可以确定:

  • 一种五个的数量
  • 一种四个的数量
  • 一种三种的数量
  • 对数

使用一个简单的公式,您可以确定自己是否有直线:

has_straight = (player_die_count[1] == 1 && player_die_count[2] == 1 &&
                player_die_count[3] == 1 && player_die_count[4] == 1 && 
                player_die_count[5] == 1) ||
               (player_die_count[2] == 1 && player_die_count[3] == 1 &&
                player_die_count[4] == 1 && player_die_count[5] == 1 &&
                player_die_count[6] == 1);

可以简化为:

has_straight = (player_die_count[2] * player_die_count[3] *
                player_die_count[4] * player_die_count[5]) == 1;

然后,您可以计算从07的付款人的手价:

  • 五种:7分
  • 4种:6分
  • 满屋一种三张加一对:5分
  • 直线:4分
  • 三种类型:3分
  • 两对:2分
  • 一对:1分
  • 胸围:0分

您可以通过根据最高骰子的值对不同组合进行排名来优化得分。 6的五种胜过5的五种,等等。

这是一个完整的程序,可以使用rand()或使用所有组合的顺序分布来输出许多平局的得分和平局:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define handsz 5
#define diesz  6     // All dice played will have 6 sides, numbered 1 to 6

static int roll_die(void) {
    return 1 + rand() % 6;
}

int main(int argc, char *argv[]) {
    int iter = 1;
    int use_random = 1;

    if (argc > 1) {
        iter = strtol(argv[1], NULL, 0);
        if (iter < 0) {
            use_random = 0;
            iter = -iter;
        }
    }

    srand(clock());

    for (int n = 0; n < iter; n++) {
        int player_hand[handsz];

        if (use_random) {
            for (int i = 0; i < handsz; i++)
                player_hand[i] = roll_die();
        } else {
            for (int i = 0, mm = n; i < handsz; i++, mm /= 6)
                player_hand[i] = 1 + mm % 6;
        }

        int player_die_count[7] = { 0 };
        for (int i = 0; i < handsz; i++) {
            player_die_count[player_hand[i]] += 1;
        }

        int pairs, threes, fours, fives, score;
        pairs = threes = fours = fives = score = 0;
        for (int i = diesz; i > 0; i--) {
            switch (player_die_count[i]) {
            case 5:
                fives = i * 11111;
                break;
            case 4:
                fours = i * 1111;
                break;
            case 3:
                threes = i * 111;
                break;
            case 2:
                pairs = pairs * 100 + i * 11;
                break;
            case 1:
                score = score * 10 + i;
                break;
            }
        }
        if (fives)
            score += 700000 + fives;
        else
        if (fours)
            score += 600000 + fours * 10;
        else
        if (threes && pairs)
            score += 500000 + threes * 100 + pairs;
        else
#ifndef NO_STRAIGHTS
        if (score == 54321 || score == 65432)
            score += 400000;
        else
#endif
        if (threes)
            score += 300000 + threes * 100;
        else
        if (pairs >= 100)
            score += 200000 + pairs * 10;
        else
        if (pairs)
            score += 100000 + pairs * 1000;

        printf("%d: %d %d %d %d %d\n",
               score, player_hand[0], player_hand[1],
               player_hand[2], player_hand[3], player_hand[4]);
    }
    return 0;
}

使用参数7776运行程序来检查简单的roll_die函数提供的实际分布是​​非常有趣的。在我的系统上,伪随机分布有时会给出令人惊讶的结果:

chqrlie$ ./pokerdice 7776 | sort -nr | head -10
755555: 5 5 5 5 5
744444: 4 4 4 4 4
744444: 4 4 4 4 4
722222: 2 2 2 2 2
711111: 1 1 1 1 1
711111: 1 1 1 1 1
711111: 1 1 1 1 1
711111: 1 1 1 1 1
666665: 6 6 6 5 6
666665: 6 6 5 6 6