Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a poker game in C. At a minimum your game must deal 2 hands of cards, and

ID: 3859131 • Letter: C

Question

Create a poker game in C. At a minimum your game must deal 2 hands of cards, and print out what poker hand each has. It must also determine which hand has won the game – but breaking a tie will be for bonus points.   

Here is the output from dealing a hand of cards:

Ace of Hearts

Ten of Clubs

Ace of Clubs

Eight of Hearts

Seven of Diamonds

2 2 1 0   

0 0 0 0 0 1 1 0 1 0 0 0 2

You have a PAIR!

Use lots of small functions to help reduce the complexity of the code.

you must use the following representation for a poker hand:

make 2 arrays:   suitsInHand[4], and facesInHand[14].

suitsInHand is 4 counters that represents how many hearts, clubs, diamonds, spades are in the hand. These 4 counters must add up to 5 for a hand of 5 cards. For example if you have 5 hearts in the hand of cards, the array would have the values 5,0,0,0

facesInHand is 13 counters, that represent how many two’s, three’s, etc. you have in the hand. This must also total 5. For example, if you have a pair of 3’s, and three Kings’s, this array would have the values 0,2,0,0,0,0,0,0,0,0,3,0

While dealing a hand of cards, keep a count of the suitsInHand, and facesInHand.

This function will use the 2 arrays described above to determine if the hand has a flush, straight, etc. I have not included the parameters. You will need to pass in the hand of cards, and have the function pass back if there is a flush, straight, three of a kind, etc.

Explanation / Answer

Answer:

#include <stdio.h>

struct card{
    int group;
    int position;
};

typedef struct card Card;

int getposition(void){
    int position = getchar();

    if (position==84)
        return 10;
    if (position==74)
        return 11;
    if (position==81)
        return 12;
    if (position==75)
        return 13;
    if (position==65)
        return 14;
    return position - 48;
}

int getgroup(void){
    int group = getchar();

    if (group==68)
        return 0;
    if (group==83)
        return 1;
    if (group==72)
        return 2;
    return 3;
}

int find(Card read[],int n){
    int i;

    for (i=0;i<5;i++){
        if (read[i].position==n)
            return 1;
    }

    return 0;
}

int isStraight(Card read[], int *type){
    int i,min;

    min=20;

    for (i=0;i<5;i++)
        if (read[i].position<min)
            min=read[i].position;

    if (find(read,min+1))
        if (find(read,min+2))
            if (find(read,min+3))
                if (find(read,min+4)){
                    *type = min+4;
                    return 1;
                }

    return 0;

}

int isStraightEnd(Card read[]){
    int group = read[0].group;
    int i;

    for (i=1;i<5;i++)
        if (read[i].group!=group)
            return 0;

    return 1;
}

int isRoyal(Card read[]){
    if (find(read,14))
        return 1;
    return 0;
}

int isFour(Card read[], int *type){
    if (read[0].position==read[1].position&&read[1].position==read[2].position&&read[2].position==read[3].position){
        *type = read[0].position;
        return 1;
    }
    if (read[0].position==read[1].position&&read[1].position==read[2].position&&read[2].position==read[4].position){
        *type = read[0].position;
        return 1;
    }
    if (read[0].position==read[1].position&&read[1].position==read[3].position&&read[3].position==read[4].position){
        *type = read[0].position;
        return 1;
    }
    if (read[0].position==read[2].position&&read[2].position==read[3].position&&read[3].position==read[4].position){
        *type = read[0].position;
        return 1;
    }
    if (read[1].position==read[2].position&&read[2].position==read[3].position&&read[3].position==read[4].position){
        *type = read[1].position;
        return 1;
    }
    return 0;
}

int isThree(Card read[], int *type){
    if (read[0].position==read[1].position&&read[1].position==read[2].position){
        *type=read[0].position;
        return 1;
    }
    if (read[0].position==read[1].position&&read[1].position==read[3].position){
        *type=read[0].position;
        return 1;
    }
    if (read[0].position==read[1].position&&read[1].position==read[4].position){
        *type=read[0].position;
        return 1;
    }
    if (read[0].position==read[3].position&&read[3].position==read[4].position){
        *type=read[0].position;
        return 1;
    }
    if (read[0].position==read[2].position&&read[2].position==read[3].position){
        *type=read[0].position;
        return 1;
    }
    if (read[0].position==read[2].position&&read[2].position==read[4].position){
        *type=read[0].position;
        return 1;
    }
    if (read[2].position==read[3].position&&read[3].position==read[4].position){
        *type=read[2].position;
        return 1;
    }
    if (read[1].position==read[2].position&&read[2].position==read[3].position){
        *type=read[1].position;
        return 1;
    }
    if (read[1].position==read[2].position&&read[2].position==read[4].position){
        *type=read[1].position;
        return 1;
    }
    if (read[1].position==read[3].position&&read[3].position==read[4].position){
        *type=read[1].position;
        return 1;
    }

    return 0;
}

int isFull(Card read[],int *type){
    int second;
    int i=0;
    while (read[i].position==*type)
        i++;
    second = read[i].position;

    for (i=i+1;i<5;i++){
        if (read[i].position!=*type&&read[i].position!=second)
            return 0;
    }
    return 1;
}

int isPair(Card read[],int *type){
    int i,j;

    for (i=0;i<4;i++)
        for (j=i+1;j<5;j++)
            if (read[i].position==read[j].position){
                *type=read[i].position;
                return 1;
            }

    return 0;
}

int isTwoPairs(Card read[], int *type, int *secondType){
    int i,j;

    for (i=0;i<4;i++)
        for (j=i+1;j<5;j++)
            if (read[i].position==read[j].position&&read[i].position!=*type){
                if (read[i].position<*type){
                    *secondType=read[i].position;
                    return 1;
                }
                else{
                    *secondType=*type;
                    *type=read[i].position;
                    return 1;
                }

            }

    return 0;
}

int findHighDouble(Card read[],int *type, int *secondType, int *highCard){
    int i;

    for (i=0;i<5;i++)
        if (read[i].position!=(*type)&&read[i].position!=(*secondType)){
            *highCard = read[i].position;
            return 1;
        }

    return 0;
}

int findHighSimple(Card read[],int *type, int *highCard){
    int i;
    int max = 0;

    for (i=0;i<5;i++)
        if (read[i].position!=*type&&read[i].position>max){
            max = read[i].position;
        }

    *highCard = max;
    return 1;
}

int findHigh(Card read[], int *highCard){
    int i;
    int max = 0;

    for (i=0;i<5;i++)
        if (read[i].position>max)
            max = read[i].position;

    *highCard = max;
    return 1;
}

int isEnd(Card read[],int *highCard){
    int i;
    int group;

    group = read[0].group;

    for (i=1;i<5;i++)
        if (read[i].group!=group)
            return 0;

    return 1;
}

int getPoints(Card read[], int *type, int *highCard,int *secondType){
    if (isFour(read, type))
        return 8;
    if (isThree(read,type)){
        if (isFull(read,type))
            return 7;
        else
            return 4;
    }

    if (isStraight(read,type)){
        if(isStraightEnd(read)){
            if (isRoyal(read))
                return 10;
            else
                return 9;
        }
        return 5;
    }

    if (isEnd(read,highCard))
        return 6;

    if (isPair(read,type)){
        if (isTwoPairs(read,type,secondType)){
            findHighDouble(read,type,secondType,highCard);
            return 3;
        }
        else{
            findHighSimple(read,type,highCard);
            return 2;
        }
    }

    findHigh(read,highCard);
    return 1;

}

int findWinner(Card read1[], Card read2[]){
    int points1,points2;
    int type1, highCard1,type2,highCard2;
    int secondType1,secondType2;

    points1 = getPoints(read1,&type1,&highCard1,&secondType1);
    points2 = getPoints(read2,&type2,&highCard2,&secondType2);

    printf("points1 = %d points2 = %dn",points1,points2);

    if (points1>points2)
        return 1;
    else if (points2>points1)
        return 0;
    else{
        if (points1==1){
            if (highCard1>highCard2)
                return 1;
            else
                return 0;
        }
        if (points1==2){
            if (type1>type2)
                return 1;
            else if (type2>type1)
                return 0;
            else{
                if (highCard1>highCard2)
                    return 1;
                else
                    return 0;
            }
        }
        if (points1==3){
            if (type1>type2)
                return 1;
            else if (type2>type1)
                return 0;
            else {
                if (secondType1>secondType2)
                    return 1;
                else if(secondType2>secondType1)
                    return 0;
                else{
                    if (highCard1>highCard2)
                        return 1;
                    else
                        return 0;
                }
            }
        }
        if (points1==4){
            if (type1>type2)
                return 1;
            else
                return 0;
        }
        if (points1==5){
            if (type1>type2)
                return 1;
            else
                return 0;
        }
        if (points1==9){
            if (type1>type2)
                return 1;
            else
                return 0;
        }
        if (points1==6){
            if (highCard1>highCard2)
                return 1;
            else
                return 0;
        }
    }

    return 2;
}

int main(){
    Card read1[5];
    Card read2[5];
    int i,r;
    int wins = 0;

    for (r=0;r<1000;r++){


        for (i=0;i<4;i++){
            read1[i].position = getposition();
            read1[i].group = getgroup();
            getchar();
        }
        read1[4].position = getposition();
        read1[4].group = getgroup();
        getchar();
        for (i=0;i<4;i++){
            read2[i].position = getposition();
            read2[i].group = getgroup();
            getchar();
        }
        read2[4].position = getposition();
        read2[4].group = getgroup();
        getchar();
        getchar();

        if (findWinner(read1,read2)){
            printf("Player One Winsn");
            wins++;
        }
        else
            printf("Player Two Winsn");

    }

    printf("Player One wins %d timesn",wins);
    printf("Player Two wins %d timesn",1000-wins);

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote