Need help programming in C

imported_vr6

Platinum Member
Jul 6, 2001
2,740
0
0
Heres the story. I am simulating the game of Old maid with C with one computer player and a human player. Directions for those who doesn't know how to play old maid.

Okay heres what i accomplished. I generated a deck of 52 cards and stored into a array of structures. Heres the structure definition

typedef struct card
{
int suit;
int pip;
} CARD;

I already took out the queen, so now there are 51 cards, and i shifted the elements after the queen one element to the left.
Then i passed out two hands starting with the player, then the computer and named the arrays player[PlayCardCount], and
computer[CompCardCount].

Then i wrote a function that would take out all the pairs in each hand. so now my problem is shrinking the deck down to the correct size, removing all the blank spaces where the removed pairs used to be in. The computer and player deck should have no blank spaces between cards, making each card one after another. Heres what i did, but it does't appear to work.


void Shift(CARD deck[], int NumOfCards)
{
int i,j;
CARD blank;

blank.suit = 0;
blank.pip = 0;

for(i = 0;i < NumOfCards;i++)
{
if(deck.pip == 0)
{
for( j = i + 1;j < NumOfCards; j++)
{
if( deck[j] != 0)
{
deck = deck[j];
deck[j] = blank;
}
}
}
}

}


 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
It would probably be easier for you to use a linked list, instead of an array.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126

if size is the count INCLUDING blank cards

try something like
j = 0
for (i = 0 ; i < size ; i ++ )
{
if ** deck [ i ] is NOT blank ** && j != i
{
deck [ j ] = deck [ i ] ;
j ++ ;
}
}

after the loop j is the count of non-blank cards.

you can use a second loop (i =[ j ] ; i < size ; i++ ) to blank the rest of the cards.

return j to the caller if you need the count of non-blank cards.