for this example i have an array of 52 cards i take five cards off the top and mark the slots empty then ii want to move the 6th card into the position of the 1st card (array element 5 to array element 0) and mark element 0 as taken then take the 7th card and put it as the second and so on until i have run out of cards then mark the last 5 slots (elements 48 - 52) as empty.
i get the following out after the first call to print_deck:Code:#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
enum card_value {TWO = 2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
enum Suit {SPADES = 0, HEARTS, CLUBS, DIAMONDS};
typedef struct
{
enum Suit suit;
enum card_value value;
bool empty_slot;
} Card;
void print_deck(Card deck[])
{
int i;
for (i = 0; i < 52; i++)
{
if (deck[i].empty_slot)
{
printf("ee ");
}
else
{
printf("%d%d ", deck[i].suit, deck[i].value);
}
}
printf("\n");
}
void move_deck(Card deck[])
{
int i, card_index = 0;
for (i = 0; i < 52; i++)
{
if (deck[i].empty_slot)
{
card_index++;
}
}
for (i = 0; i < 52; i++)
{
if (card_index < 52) //copy cards down the chain till moved last card
{
deck[i] = deck[i + card_index];
deck[i + card_index].empty_slot = false;
card_index++;
}
else
{
deck[i].empty_slot = true;
}
}
}
void get_hand(Card deck[], Card hand[])
{
int i;
for (i = 0; i < 5; i++)
{
hand[i] = deck[i];
deck[i].empty_slot = true;
}
}
int main()
{
int i, j, card_count = 0;
Card deck[52], hand[5];
for (j = 0; j < 4; j++)
{
for (i = 2; i < 15; i++)
{
deck[card_count++] = (Card) {j, i, false};
}
}
get_hand(deck, hand);
print_deck(deck);
move_deck(deck);
print_deck(deck);
return 0;
}
ee ee ee ee ee 07 08 09 010 011 012 013 014 12 13 14 15 16 17 18 19 110 111 112 113 114 22 23 24 25 26 27 28 29 210 211 212 213 214 32 33 34 35 36 37 38 39 310 311 312 313 314
all good i have my first 5 slots marked as empty (ee) However on the second call of print_deck i get this output:
07 09 011 013 12 14 16 18 110 112 114 23 25 27 29 211 213 32 34 36 38 310 312 314 ee ee 218490 ee ee 327670 ee 00 ee 3276728 327670 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
for some reason its printing out every other card or im moving every other card and then either filling the rest with junk or ee
many thanks]
coop