0

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

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 2
    The `for` loop will access outside of the array on the first iteration of the loop (`i == 51` and `deck[i+1]`), and never touch element `0` (the last element should be `1`. You should also probably `srand` *outside* the `for` loop. Also, it looks like your `deck[i+1] = temp;` should be `temp = deck[i+1];` if you're attempting a swap. – Alyssa Haroldsen May 06 '16 at 21:26
  • Dupe of http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once/7343909#7343909? – chux - Reinstate Monica May 06 '16 at 21:29
  • 2
    @chux While the seeding is definitely a problem here, the problem doesn't lie only in `srand` and so isn't a duplicate of the one issue. – Alyssa Haroldsen May 06 '16 at 21:32
  • Heeeeyyyy it worked thanks a lot. I didn't know that the swap syntax mattered that much thank you. – jojobuddy13 May 06 '16 at 21:33
  • @jojobuddy13 Considering that fixed it, I've added an answer. – Alyssa Haroldsen May 06 '16 at 21:39
  • your deck makes no sense. why do you have 1's seperate from 11's? ace is 11 or 1, not both. – UpAndAdam May 06 '16 at 22:07

2 Answers2

1

The algorithm that it looks like you're trying to emulate is the modern Fisher Yates shuffle

-- To shuffle an array a of n elements (indices 0..n-1):
for i from n−1 downto 1 do
  j ← random integer such that 0 ≤ j ≤ i
  exchange a[j] and a[i]

This code is yours cleaned up a bit.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static 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 ,
  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
};

void swap (int *a, int *b){
    int temp = *a; 
    *a = *b; 
    *b = temp;
}

int main(void) {
  //You only need to call srand once.
  srand(time(NULL));
  int i = 0;
  int j = 0;
  for(i = 51 ; i > 0; --i){
    j = rand() % i; 
    printf("%d\n",deck[j]);
    swap(&deck[j], &deck[i]);  
  }
  return 0;
}

The important point here is that the i is decreasing over the entire range of numbers ie each number will be swapped with a randomly chosen one.

Harry
  • 11,298
  • 1
  • 29
  • 43
0

Some problems:

  • You should only be calling srand once.
  • The for loop will access outside of the array on the first iteration of the loop (i == 51 and deck[i+1]), and never touch element 0 (the last element should be 1). You can fix this by changing for (i = 51; i > 0; --i) to for (i = 50; i >= 0; --i).
  • The swap is done incorrectly. deck[i+1] = temp; should be temp = deck[i+1];.

A swap generally looks like this:

// to swap a and b
int t;
t = a;
a = b;
b = t;

It's not so much as a "syntax" as a convention of how one swaps two variables. For example, an idiomatic swap looks like a, b = b, a in Python.

Community
  • 1
  • 1
Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35