I'm currently working on a blackjack game for my final project in C programming. I have a basic program written that works, but I want to make it more impressive since I have time. I am trying to make my "drawing a card from a deck" part of the program more interesting than just 1 + rand() % 11
twice to pull the two cards.
I want to have a set array of numbers and pull a random number from that. (easy) But never have the same number drawn, in this case, more than 4 times. To do that I want to get a random number between 1 and 52 to get the place marker of the "card" in the array, and move that "card" to the back of the deck and decrease the amount you can get a random number (e.x 1-51 loop 1-50 loop 1-49)
I think my error is in the bubble sort but I may be wrong.
int main(){
int deck[52] = {1,1,1,1,2,2,2,2,3,3,3,3,4,
4,4,4,5,5,5,5,6,6,6,6,7,7, //Array of cards
7,7,8,8,8,8,9,9,9,9,10,10,
10,10,10,10,10,10,10,10,10,
10,11,11,11,11};
for(i=51;i>0;--i){
//for loop to decrease value of i
srand(time(NULL));
crd = rand() % i; //the number - 1 every loop that becomces "crd"
printf("%d\n",deck[crd]);
deck[i+1] = temp; //bouble sort the card to the back of the deck
deck[crd] = deck[i+1];
deck[crd] = temp;
}
}
my out put is as follows 10 6 1 9 6 4 3 0 0 0 0 0 8 5 3 0 4 7 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 3 2 0 0 1 0 0 0 2 0 0 0 0
and my program crashes. I can't figure ou what is wrong with the code. plz help