gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:39:34 28 4
gpt4 key购买 nike

我有一个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);
}

最佳答案

从数组 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;

然后您可以计算付款人的手牌值(value)从07:

  • 五样:7分
  • 四样:6分
  • 葫芦:三张同色加一对:5分
  • 顺子:4分
  • 三样:3分
  • 两对:2分
  • 一对:1分
  • 胸围:0分

您可以通过根据最高骰子的值对不同的组合进行排名来细化分数。 5 个 6 击败5 个 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 -10755555: 5 5 5 5 5744444: 4 4 4 4 4744444: 4 4 4 4 4722222: 2 2 2 2 2711111: 1 1 1 1 1711111: 1 1 1 1 1711111: 1 1 1 1 1711111: 1 1 1 1 1666665: 6 6 6 5 6666665: 6 6 5 6 6

关于c - 使用循环计算骰子扑克结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56039324/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com