gpt4 book ai didi

c - 皇家同花顺不适用于手牌分配 [C]

转载 作者:太空宇宙 更新时间:2023-11-04 04:17:49 25 4
gpt4 key购买 nike

我都试过了!我应该添加皇家同花顺部分。我了解同花顺、同花顺和同花顺部分,但我似乎无法理解皇家同花顺部分。最后,代码应该打印出 50000 张牌中有多少张顺子、同花顺、同花顺和皇家同花顺。这开始是我与合作伙伴的实验室,但现在为了获得额外的荣誉,我应该添加皇家同花顺部分,但我一无所知。提前致谢。

#include <stdio.h>

#define SIZE 50000

typedef struct card_t_struct{
int value; // 1 through 13
int suit; // 0 is clubes, 1 is diamonds, 2 is spades, 3 is hearts
} card_t;

void printCard(card_t card){
if(card.value == 1){
printf("Ace");
}else if(card.value == 11){
printf("Jack");
}else if(card.value == 12){
printf("Queen");
}else if(card.value == 13){
printf("King");
}else{
printf("%d", card.value);
}

printf(" of ");

if(card.suit == 0){
printf("Clubs.");
}else if(card.suit == 1){
printf("Diamonds.");
}else if(card.suit == 2){
printf("Spades.");
}else if(card.suit == 3){
printf("Hearts.");
}else{
printf("%d not a suit", card.suit); // Error message
}
printf("\n");
}

void hand(card_t* a){
int i = 0;
for(i = 0; i < 5; i++){
printCard(a[i]);
}
}

void shuffle(card_t* array, int length){
int i = 0;
for(i = 0; i < length * 2; i++){
int from = rand() % length;
int to = rand() % length;
card_t temp = array[from];
array[from] = array[to];
array[to] = temp;
}
}

void bubble(card_t* f, int length){
int i = 0;
int bub_num = 0;
for(bub_num = 0; bub_num < length; bub_num++){
for(i = 0; i < length - 1; i++){
if(f[i].value > f[i+1].value){ // Wrong order
int temp = f[i].value;
f[i].value = f[i+1].value;
f[i+1].value = temp;
}else{

}
}
}
}

int isFlush(card_t* deck){
int i = 0;
for(i = 1; i < 5; i++){
if(deck[i].suit != deck[0].suit){
return 0;
}
}
return 1;
}

int isStraight(card_t* deck){
int i = 0;
for(i = 1; i < 5; i++){
if(deck[i+1].value != deck[i].value + 1){
return 0;
}
}
return 1;
}

int isSFlush(card_t* deck){
int i = 0;
for(i = 1; i < 5; i++){
if(deck[i+1].value != deck[i].value + 1){
return 0;
}
}

for(i = 1; i < 5; i++){
if(deck[i].suit != deck[0].suit){
return 0;
}
}
return 1;
}

int isRFlush(card_t* deck){
int i = 0;
for(i = 1; i < 5; i++){
if(deck[i].value != deck[i].value){
return 0;
}
}

for(i = 1; i < 5; i++){
if(deck[i].suit != deck[0].suit){
return 0;
}
}
return 1;
}

int main(){
srand(time(0));
card_t deck[52] = {};
int i = 0;
int flush = 0;
int straight = 0;
int straight_flush = 0;
int royal_flush = 0;

int suit = 0;
for(suit = 0; suit < 4; suit++){
int value = 1;
for(value = 1; value <= 13; value++){
deck[i].suit = suit;
deck[i].value = value;
i++;
}
}

for(i = 0; i < SIZE; i++){
shuffle(deck, 52);
hand(deck);
if(isFlush(deck) ){
flush++;
}
printf("\n");

bubble(deck, 5);
if(isStraight(deck) ){
straight++;
}

if(isSFlush(deck) ){
straight_flush++;
}

if(isRFlush(deck) ){
royal_flush++;
}
}

printf("The number of flushes you have is: %d\n", flush);
printf("The number of straights you have is: %d\n", straight);
printf("The number of straight flushes you have is: %d\n", straight_flush);
printf("The number of royal flushes you have is: %d\n", royal_flush);
}

最佳答案

基于现有的IsStraight(...)函数,我假设你的 isRFlush(...)函数正在传递一张五张牌(不是一副牌),其牌是按升序排列的?

int isRFlush(card_t* deck) {

int i = 0; // zero-based index into your hand of five cards (0-4)

// This next bit DOESN'T ACTUALLY DO ANYTHING. The reason is that comparing anything
// with itself will always result in an equality (i.e. 1==1, 3.14==3.14, a==a, etc),
// so this comparison always returns false, and the return 0 after the if statement
// is never taken, regardless of the value of the cards passed in.
/*
for (i = 1; i < 5; i++) {
if (deck[i].value != deck[i].value) { // <--- Never false, no matter what
return 0;
}
}
*/

// What the other isXxxx methods above were doing was ensuring that each 'next' card
// was 'one higher' than the card that came before it in the hand. If that's found to
// be untrue for any card in the hand, then the function exits with a zero.
//
// That's the logic for a STRAIGHT, but we're looking for a ROYAL STRAIGHT, so the
// hand HAS to be "10 J Q K A" (all of the same suit, but we'll check that part later).
//
// Since the Ace represents a non-sequential value, I think you may as well just do
// this using five comparisons without the loop:
if ( deck[0].value != 1 // if 1st card isn't Ace
|| deck[1].value != 10 // or 2nd card isn't 10
|| deck[2].value != 11 // or 3rd card isn't Jack
|| deck[3].value != 12 // or 4th card isn't Queen
|| deck[4].value != 13) { // or 5th card isn't King

return 0; // Not a royal straight
}

// If we've made it this far, we know that we have a ROYAL STRAIGHT (e.g. 10, J, Q, K, A)

// Now, we need to ensure that we also have a FLUSH

// Loop through the 2nd to last cards, ensuring they are all
// of the same suit as the first card in the hand
for (i = 1; i < 5; i++) {
if (deck[i].suit != deck[0].suit) {
return 0; // Not a flush
}
}

return 1; // This hand IS a Royal Straight Flush (aka Royal Flush)
}

最后的注释:

  • 此程序中可能还有其他错误。例如,isStraight(...)跳过第一张卡片并循环遍历最后四张卡片,但它正在向索引 i 添加一个什么时候应该减一:

    // Note: This function doesn't recognize a non-sequential "ace-high" flush
    // (e.g. 10,J,Q,K,A).
    int isStraight(card_t* deck) {
    int i = 0;
    for (i = 1; i < 5; i++) {
    // Loop values of i will be [1, 2, 3, 4], which correspond to the 2nd through 5th card in the hand passed in.
    // To check these against the previous card, you should subtract one (vs adding one).
    /*
    if (deck[i+1].value != deck[i].value + 1) { // <--- Array Index Out Of Bounds
    */

    // If card in hand isn't 'one higher' than the previous card...
    if (deck[i-1].value != deck[i].value + 1) {
    return 0;
    }
    }

    return 1;
    }
  • 由于您已经有一个检查手牌是否齐平的函数,您可以通过在 isRFlush(...) 中调用它来利用它来节省一些输入。功能:

    int isRFlush(card_t* deck) {

    if ( deck[0].value != 1 // if 1st card isn't Ace
    || deck[1].value != 10 // or 2nd card isn't 10
    || deck[2].value != 11 // or 3rd card isn't Jack
    || deck[3].value != 12 // or 4th card isn't Queen
    || deck[4].value != 13) { // or 5th card isn't King

    return 0; // Not a royal straight
    }

    // At this point, you know it's a ROYAL STRAIGHT, so just return whether or not it's also
    // a FLUSH
    return isFlush(deck);
    }

关于c - 皇家同花顺不适用于手牌分配 [C],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49929623/

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