Essentialy what you want to do is to move all still valid cards in the deck to the begining of the array, marking the final cards as "empty", isn't it?
Well... notice your card_value enumeration starts with 2. Add an EMPTY=0 at the beginning:
Code:
enum card_value {EMPTY = 0, TWO = 2, THREE, FOUR,
FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
And use it as an EMPTY card (no need to use the empty_slot boolean - reducing the Card structure size). Here's an example code using integers:
Code:
#include <stdio.h>
#include <string.h>
int c[16] = { 1, 2, 0, 3, 4, 5, 0, 0, 0, 6, 7, 8, 0, 9, 10, 11 };
void shrink ( int *p, unsigned int count )
{
unsigned int i, j;
j = 0;
for ( i = 0; i < count; i++ )
{
if ( !p[j] )
{
memmove ( p + j, p + j + 1, sizeof ( *p ) * ( count - j - 1 ) );
// mark the last one as empty
// need to do this only once, but it doesn't hurt to do it multiple times.
p[count - 1] = 0;
}
else
j++; // advance only if no movement was made before.
}
}
int main ( void )
{
unsigned int i;
// print original array:
for ( i = 0; i < sizeof c / sizeof c[0]; i++ )
printf ( "%d ", c[i] );
putchar ( '\n' );
// rearrange...
shrink ( c, sizeof c / sizeof c[0] );
// print shrinked array
for ( i = 0; i < sizeof c / sizeof c[0]; i++ )
printf ( "%d ", c[i] );
putchar ( '\n' );
}
Compiling and linking:
Code:
$ cc -o test test.c
$ ./test
1 2 0 3 4 5 0 0 0 6 7 8 0 9 10 11
1 2 3 4 5 6 7 8 9 10 11 0 0 0 0 0