jeudi 19 avril 2018

Royal Flush not working for Card Hand Assignment [C]

I have tried everything! I'm supposed to add on the Royal Flush part. I understand the straight, flush, and straight flush part, but I cannot seem to get the Royal Flush part. In the end, the code is supposed to print how many straight, flushes, straight flushes, and royal flushes were in the deck of 50000 cards. This started out as a lab that I had with a partner, but now for extra credit, I'm supposed to add on the royal flush part and I'm clueless. Thanks in advance.

#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);


}

Aucun commentaire:

Enregistrer un commentaire