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

Question 1: Create a card game: war sample: You will write an implementation of

ID: 3859205 • Letter: Q

Question

Question 1: Create a card game: war

sample:

You will write an implementation of a simple card game called War. Details of the game's rules can be found here: http://www.bicyclecards.com/how-to-play/war/ This game uses a "standard" deck of playing cards, and involves two players. Each player is dealt half of a randomly shuffled deck (so 26 cards each). During each turn, both players simultaneously flip the top card of their deck. The player who flips the highest value card wins the turn, and "captures" both cards by adding them to the bottom of their deck. If both players flip over cards with the same value, then a "war" begins: each player takes the next two cards from their deck, flipping over one and leaving the second face-down. The flipped cards are compared, and the highest value of these cards determines the winner, who now takes the "war pile" all six cards). If these are also the same value, then the war continues; both players continue drawing two cards from their deck and flipping only the first one to compare values. The ultimate winner takes all cards in the "war pile". After a war, play resumes as normal by flipping and comparing only one card each at a time. Your gameshouldthre class Card,Deck,and Demo. Your game should use three class: Card, Deck, and Demo

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