I really need some help with understanding how to do the logic for this card game. It's quite complicated and I've been trying to do it all day and it's due in some hours..It's a lot of code but the main shouldn't be much to check out on.
I have two players, a computer and a user(player).
The player starts off the game by entering the amount of cards to play. Or the player could choose to skip a turn. If the player skips a turn then the computer wins the round. The computer then selects how much cards to play and does his move. Then the player would play. If the computer is unable to play he would skip a turn and the player would start the new round. The new round starts again with the player being asked how much cards to play, then the player plays then the computer plays..If the player is unable to play then he would pass.
A check also has to be done after every play to see whose hand is empty first..whether it be the player or the computer. The first hand to be empty wins the game. I tried putting everything in a loop in my main to get the code to work but I'm having a lot of trouble especially since the player can get to choose how much cards to play or if a computer plays and it switches to the player then he would have to follow along with the same number of cards the computer was playing with. Please help me with the logic..
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define DECK_SIZE 52
#define NFACES 13
#define NSUITS 4
#define NCARDS 13
#define ROGUE -999
#define PLAYER 222
#define COMPUTER 333
#define OVER 1
#define START 0




/*enum for card suits*/
enum suits_e{
    SUIT_DIAMONDS,
    SUIT_CLUBS,
    SUIT_HEARTS,
    SUIT_SPADES
};


/*card structure definition*/
 typedef struct Card{
    int face; /* define face */
    int suit; /* define suit */
}Card, *CardPtr;/* end structure card */




/*function prototypes*/
void createDeck(CardPtr deck);
void printCard(CardPtr card);
void shuffle(CardPtr deck);
void shareCards(CardPtr deck, CardPtr Hand1, CardPtr Hand2, CardPtr Hand3, CardPtr Hand4);
void highLowSwap(CardPtr Hand1, CardPtr Hand2);
void cardSort(CardPtr Hand, int n);
void removeCard(CardPtr Hand, int index);
void printCardsToTable(int x, CardPtr playerCards);
void printCurrentHand(CardPtr Hand1);
int numofCardstoPlay();
int checkFace(int numCards, CardPtr playerCards);
int isEmpty(CardPtr Hand);
int aiPlayFirst(CardPtr Hand, int numCards, CardPtr playerCards);
CardPtr storeCards(int numCards, CardPtr Hand1, CardPtr playerCards);
CardPtr aiStoreCards(CardPtr Hand, CardPtr playerCards, int arrIndex, int *arr, int numCards);






int aiCompare(CardPtr Hand, int faceValue, int *arr);
int aiCheckNum(int *arr, int numCards);




int main(){


    /*Declarations*/
    Card deck[DECK_SIZE]; /*Creating deck array*/
    /*Creating hand arrays*/
    Card Hand1[NCARDS]; /*President Hand*/
    Card Hand2[NCARDS]; /*Prime Minister Hand*/
    Card Hand3[NCARDS]; /*Minister Hand*/
    Card Hand4[NCARDS]; /*Slave Hand*/
    int i=0, a=1; /*counters*/




    /*Functions before gameplay begin*/
    createDeck(deck);
    shuffle(deck);
    shareCards(deck, Hand1, Hand2, Hand3, Hand4);
    highLowSwap(Hand4, Hand1);
    highLowSwap(Hand4, Hand1);
    highLowSwap(Hand3, Hand2);


    /*Sorting the players' hands*/
    cardSort(Hand1, 13);
    cardSort(Hand2, 13);
    cardSort(Hand3, 13);
    cardSort(Hand4, 13);


    /*Printing the Player's Hand to the screen for interaction*/
    printf("President's Hand: PLAYER 1\n");
    printCurrentHand(Hand1);


    printf("\n");


    /*Printing the Player's Hand to the screen for interaction*/
    //printf("Prime Minister's Hand: PLAYER 2\n");
    //printCurrentHand(Hand2);


    /*Player 1 begins gameplay*/
    int turn = PLAYER;
    int game = START;//start of game


    while (game == START){
    int numCards=0;
    Card playerCards[numCards]; //address of the array of cards being played, this array acts as the game board


        /*PLAYER TURN*/
        int choice;
        printf("Select 1 to PLAY and 0 to SKIP TURN: ");
        scanf("%d", &choice);
        int b=1;
        turn = PLAYER;


        if (turn == PLAYER){


            /*Player plays*/
            if (choice==1 && b==1){
                numCards = numofCardstoPlay();
                /*Error check for number of cards to play.
                The user cannot choose to play 0 cards or anything over 4 cards and if a number out of the range 1-4 is selected,
                the user will be prompted to start over
                User can only play a maximum of 4 cards since there are only four different suits of each card.
                A user can play a minimum of 1 card and a maximum of 4 cards*/
                while (numCards < 1 || numCards > 4){
                    printf("Invalid number of cards. Try again.\n");
                    printf("You can only play cards with the same face. Select how many cards to play: ");
                    scanf("%d", &numCards);
                }
                storeCards(numCards, Hand1, playerCards);
                checkFace(numCards, playerCards);
                /*Prints the Player's Cards Played*/
                printCardsToTable(numCards, playerCards);
                printCurrentHand(Hand1);
            }


            if (turn == PLAYER){
                storeCards(numCards, Hand1, playerCards);
                checkFace(numCards, playerCards);
                /*Prints the Player's Cards Played*/
                printCardsToTable(numCards, playerCards);
                printCurrentHand(Hand1);
            }


            if (choice!=1){//player skips turn
                printf("Computer WINS Round!\n");
                int a=1;
                turn = COMPUTER; //Computer to play
            }




            else if (isEmpty(Hand1)!= ROGUE){
                printf("Player 1 is the WINNER\n");
                game = OVER;
            }


        turn = COMPUTER;
        }


        else
            if (turn == COMPUTER && a==1){
                numCards = aiPlayFirst(Hand2, numCards, playerCards);
                printf("\n");
                //printf("Prime Minister's Hand: PLAYER 2\n");
                //printCurrentHand(Hand2);
                printf("\n");
                printf("The number of cards player can play is %d\n", numCards);
                printf("\n");
                printf("\n");
                printCurrentHand(Hand1);
                printf("\n");


            }


            if (turn == COMPUTER){
                int faceValue;
                faceValue = playerCards->face;  //storing the face played so comparisons can be made
                printf("Face value: %d\n", faceValue);
                int arr[NCARDS]; //creating a second array to keep track of the occurrences of each value
                aiCompare(Hand2, faceValue, arr);
                int arrIndex;
                arrIndex = aiCheckNum(arr,numCards);
                if (arrIndex != ROGUE){
                    printf("The face that can be played is: %d\n", arrIndex);   //player has the amount and a higher card value to play
                    aiStoreCards(Hand2, playerCards, arrIndex, arr, numCards);
                    printf("\n");
                    printf("The cards played by Minister are:\n");
                    /*Prints the Player's Cards Played*/
                    printCardsToTable(numCards, playerCards);


                    a=1;
                    printf("Prime Minister's Hand: Player 2\n");
                    printCurrentHand(Hand2);
                }


            }


            /*if (choice!=1){//player skips turn
                    printf("Computer WINS Round!\n");
                    int a=1;
                    turn = COMPUTER; //Computer to play
                }*/




                else if (isEmpty(Hand2)!= ROGUE){
                    printf("Computer is the WINNER\n");
                    game = OVER;
                }
        turn = PLAYER;




    }


return 0;
}


/*Creates deck*/
void createDeck(CardPtr deck){
    int face, suit;
    int i=0;    /*counter*/


    for (suit=SUIT_DIAMONDS; suit<NSUITS; suit++){


            for (face=0; face<NFACES; face++){
                deck[i].face= face;
                deck[i].suit= suit;
                i++;
            }


    }


}


/*Prints a Card*/
void printCard(CardPtr card){


    /*Face array*/
    const char *face[NFACES+1] = {
        "Deuce", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"
    };


    /*Suit array*/
    const char *suit[NSUITS+1] = {
       "Diamonds", "Clubs", "Hearts", "Spades"
    };


    printf("%s of %s\n", face[card->face], suit[card->suit]);
}


/*Shuffles deck*/
void shuffle(CardPtr deck){
    int i, index;
    Card temp;
    srand(time(NULL)); // randomize seed with time


    for(i=DECK_SIZE-1; i>0; i--){
        index = rand()% (i + 1);
        temp = deck[index];
        deck[index]= deck[i];
        deck[i]= temp;
    }
}


/*Share cards*/
/*Since the deck is shuffled already, we will just assign the first 13 cards to the President, the next 13 to the Prime Minister and so on...*/
void shareCards(CardPtr deck, CardPtr Hand1, CardPtr Hand2, CardPtr Hand3, CardPtr Hand4){
    int i=0;
    int j=0;
    int k=0;
    int l=0;  /*counters*/
    for (i=0;i<DECK_SIZE;i++){
        if (i<13){
            Hand1[i] = deck[i];
        }
            else if (i<26){
                Hand2[j] = deck[i];
                j++;
            }


                else if (i<39){
                    Hand3[k] = deck[i];
                    k++;
                }


                    else if (i<DECK_SIZE){
                        Hand4[l] = deck[i];
                        l++;
                    }
    }   /*end for*/
}


/*This function finds the highest and lowest card from two separate Hands and swap them into separate Hands
In the game, the President will have to give the Slave his lowest card while he will give the President his highest card.
Also, the Prime Minister would give the Minister his lowest card while the Minister would give him his highest card.
This function caters for that functionality
*/
void highLowSwap(CardPtr Hand1, CardPtr Hand2){
    printf("\n"); /*Program is crashing without a printf statement in this function, not sure why*/
    int max=0;  /*holds highest number*/
    int i=0;    /*counter*/
    Card temp;
    int loc1;   /*Keeps track of Location1*/


    for (i=0;i<NCARDS;i++){
        if (max<=Hand1[i].face){
            max =  Hand1[i].face;
            temp = Hand1[i];
            loc1 = i;


        }
    }




/*Find Card with lowest value*/
    int j=0;    /*counter*/
    int min;  /*holds lowest number*/
    int loc2;


    for (j=0;j<NCARDS;j++){
        if (min >= Hand2[j].face){
            min =  Hand2[j].face;
            loc2 = j;


        }
    }


    temp = Hand1[loc1]; /*Holding the location of the card in Hand 1 as the temp variable before overwriting the value in Hand 1*/
	Hand1[loc1] = Hand2[loc2];  /*swapping the card from Hand 2 to Hand 1*/
	Hand2[loc2] = temp; /*Place location for Hand 1 into Hand 2*/
	/*Swap is completed*/


return;
}


/*Ask user to enter how much cards he would like to play*/
int numofCardstoPlay(){
    int numCards = 0;  /*Total of cards to play in one turn*/
    printf("You can only play cards with the same face. Select how many cards to play: ");
    scanf("%d",&numCards);
return numCards;
}




int checkFace(int numCards, CardPtr playerCards){
    if (numCards == 1){
        return 0;   /*since there is just one card so there's no card faces to compare*/
    }


    int i=1; /*counter*/
    for (i=1;i<numCards;){
        if (playerCards[0].face != playerCards[i].face){
            printf("Faces are not the same! Try again\n ");
        return ROGUE;
        }
        else i++;
    }
return 0;
}




/*This function accepts the cards of the player's choice and stores them to a place in memory
so computation can be done with/among the cards*/
CardPtr storeCards(int numCards, CardPtr Hand1, CardPtr playerCards){
    int i=0; /*counter*/
    int cardplay=0; /*to keep track of the index being selected*/
    /*loop enters card n times*/
    for (i=0;i<numCards;i++){
        printf("State the index of your card to play. The index is the number between the []: ");
        scanf("%d", &cardplay);
        cardplay = cardplay--; //since index is 1 less than the actual array index...e.g the first card would be in index 0 instead of 1
        playerCards[i] = Hand1[cardplay];
        removeCard(Hand1, cardplay);
    }


return playerCards;
}


void removeCard(CardPtr Hand, int index){
    Card temp;
    temp.face = ROGUE;
    temp.suit = ROGUE;
    Hand[index] = temp;
}


/*This function compares the cards in AI's hand to find out if there's a card higher than the previous one played
and to give a count of how many of the same faces of cards AI has*/
int aiCompare(CardPtr Hand, int faceValue, int *arr){
    int i,j=0; /*counters*/
    for (i=0;i<NCARDS;i++){
        arr[i] = 0;
    }
   for (i=0;i<NCARDS;i++){
        if (Hand[i].face > faceValue){
            arr[Hand[i].face]++;
        }
    }
return 0;
}




/*This function checks if AI has the total number of cards with the same faces in order to play*/
int aiCheckNum(int *arr, int numCards){
    int i; /*counter*/
    for (i=0;i<NFACES;){
        if (arr[i] >= numCards){
            return i;
        }
    i++;
    }
return ROGUE;
}




/*This function sorts the hand to make it easier for the user to read and
also, to aid in searching for card faces*/
void cardSort(CardPtr Hand, int n){
    int i,j;
    Card currentIndex;


    for (i=1;i<n;i++){
        currentIndex = Hand[i];
        j = i-1;
        while (j >= 0 && Hand[j].face > currentIndex.face){
            Hand[j+1] = Hand[j];
            j=j-1;
        }
    Hand[j+1] = currentIndex;
    }


}




/*This function searches through AI's hand for the face that was
counted for in the second array. It then stores the faces to an array
called playerCards which acts like the board and then removes the
various cards from AI's hand after it is played to the board*/
CardPtr aiStoreCards(CardPtr Hand, CardPtr playerCards, int arrIndex, int *arr, int numCards){
    int i=0;
    int j=0, k=0;
    for (i=0; i<numCards;i++){
        while (Hand[j].face != arrIndex){
            j++;
        }
        playerCards[k] = Hand[j];
        printf("Player card's face is: %d\n", playerCards[k].face);
        removeCard(Hand, j);
        printf("index: %d\n",j);
        k++;
    }
return playerCards;
}








/*Prints the Player's Cards Played*/
void printCardsToTable(int x, CardPtr playerCards){
    int i, a=1;
    for(i=0; i<x; i++){
            printf("[%d]. ", a);
            printCard(&playerCards[i]);
            a++;
        }
}






/*This function prints the player's current hand. Any card that is already played
will be marked as empty and the player will be unable to play it.*/
void printCurrentHand(CardPtr Hand1){
    int a=1, i=0;
    printf("\n");
    for(i=0; i<NCARDS; i++){
        printf("[%d]. ", a);
        if (Hand1[i].face == ROGUE && Hand1[i].suit == ROGUE){
            printf("Empty\n");
        }
        else {
        printCard(&Hand1[i]);
        }
        a++;
    }
}




/*  Checks if a player's hand is empty.
    The first player to have an empty hand wins the game so this function is necessary.
    Return: 1 if hand is empty, ROGUE if otherwise
*/
int isEmpty(CardPtr Hand) {
    int i;
    for (i = 0 ; i < NCARDS ; i++) {
        if (Hand[i].face != ROGUE || Hand[i].suit != ROGUE) return ROGUE;
    }
    return 1;
}






int aiPlayFirst(CardPtr Hand, int numCards, CardPtr playerCards){
    int arr[NCARDS];
    int i; /*counter*/
    int hold;   /*holds the face value*/
    int max=0;
    for (i=0;i<NCARDS;i++){
        arr[i] = 0;
    }
    for (i=0;i<NCARDS;i++){
        hold = Hand[i].face;
        arr[hold]++;    //increments the value stored in the array at the index hold


    }


    i=0;
    if (arr[12] > 0){  /*Checks if AI's Hand has a card with the highest face, Ace*/
        while (Hand[i].face != 12){ /*This while loop checks through the player's hand to search for the first ACE*/
           i++;
        }
        playerCards[0] = Hand[i];
        numCards = changeNumCards(numCards, 1);
        max = numCards;
        removeCard(Hand, i);
        printf("The Cards played by the Prime Minister are as follows: \n");
        printCardsToTable(numCards, playerCards);
        //printf("The number of cards player can play is %d\n", numCards);
    }
    else {
        int index=0;
        max=0;
        for (i=0;i<NCARDS;i++){
            if (arr[i] > max){
                max = arr[i];
                index=i;
            }
        }


        if (max == 0){
            printf("Player is out of cards\n");
            return 0;
        }


        else {
        numCards = max;
        printf("Max is %d and is at location %d\n", max, index);




        //printf("The number of cards player can play is %d\n", numCards);
        int j=0,k=0;
        for (i=0;i<numCards;i++){
            while (Hand[j].face != index){
                j++;
            }
            playerCards[k]= Hand[j];
            removeCard(Hand, j);
            k++;
        }
        printf("The Cards played by the Prime Minister are as follows: \n");
        printCardsToTable(numCards, playerCards);
        }
    }


return max;
}


int changeNumCards(int numCards, int n){
    numCards = n;
return n;
}
P.S I'm sorry for the inconvenience with this amount of code, I just didn't know which part would be exactly important.