By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 446,190 Members | 926 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

# Black Jack Programming

 P: 2 Hello I'm new to C langauge I had a project about blackjack programming. but I have no IDEA, what to do..;; Could you please help me out to write down this simple program? (Biggest Problem is that I don't know how to get random numbers between 1~10) it will be so much help T-T ================================================== ======= // Lab 6 - blackjack simulation // Written by: // Date: #include void intro(void); int getCard(void); int sum(int total, int cardVal); int first2(void); int bust(int total); int main (void) { intro(); return 0; } // intro: prints the rules of the game to the player // should be called once, at the beginning of the program // input: nothing // return: nothing void intro(void) { printf ("=====Welcome to the 15AG Black Jack Table=====\n\n"); printf ("-I will deal myself 1 card and show the card value to you.\n"); printf (" Then I will deal you 2 cards.\n"); printf (" You can decide to draw 1 more card at a time to add to your total.\n"); printf (" When you decide to stop, I will deal myself 1 card at a time.\n"); printf (" I will stop when my total reaches 17 or higher.\n"); printf (" Whoever has a total closest to 21, without going over, wins.\n"); printf (" If it is a tie, then you win.\n"); printf (" Let's play!\n\n"); printf ("================================================ =================\n\n"); } // sum: adds the new card value to the current total // input: current total and new card value // return: new total int sum(int total, int cardVal) { total = total + cardVal; printf ("The total for the hand is %d\n", total); return total; } // getCard: generates a random number between 1 and 10, inclusive // input: nothing // return: random number int getCard(void) { } // first2: deals the player the first 2 cards and sums up their values // prints each of the card value and the sum // input: nothing // return: the total int first2(void) { } // bust: determines if a player's hand is a bust (over 21) // input: total // return: 1 bust // 0 not bust int bust(int total) { } Jun 4 '07 #1
Share this Question
4 Replies

 100+ P: 147 Take a look here for info on rand() and srand(). Is this meant to be a realistic blackjack sim? There are more than 10 ranks of cards in a normal deck, and choosing a card randomly isn't the same as dealing from a shuffled deck. Jun 4 '07 #2

 P: 2 nope~ It has to show only between 1 ~ 10. (Also I clicked your link, but I have to type in C only^^;; I haven't took C++ classes yet, so if I use C ++ codes my teacher will know that I didn't write it by myself) it has to be done like this// =================================== Welcome to the 15AG Black Jack Table I will deal myself 1 card and show the card value to you. Then I will deal you 2 cards. You can decide to draw 1 more card at a time to add to your total. When you decide to stop, I will deal myself 1 card at a time I will stop when my total reaches 17 or higher Whoever has a total closest to 21, without going over, wins. If it is a tie, then you win Let's play =================================== My first card is 10 Your first card is 2, your second card is 1 The total for the hand is 3 Another card? (y/n): y Your new card is 9 The total for the hand is 12 Another card? (y/n): y Your new card is 10 The total for the hand is 22 Bust! I win!! Play again? (y/n) ========================================= Like above.. Computer just need to ask only Yes Or No. and the User need to answer only Yes or No. (Sounds simple..but ek..X_X) Jun 4 '07 #3

 Expert 100+ P: 844 nope~ It has to show only between 1 ~ 10. (Also I clicked your link, but I have to type in C only^^;; I haven't took C++ classes yet, so if I use C ++ codes my teacher will know that I didn't write it by myself) You can use the modulo operator to limit the range and rand srand are good with just C I think. Expand|Select|Wrap|Line Numbers srand((unsigned)time(0)); randomnumber = rand()%10+1;   Jun 4 '07 #4

 Expert 10K+ P: 11,448 You can use the modulo operator to limit the range and rand srand are good with just C I think. Expand|Select|Wrap|Line Numbers srand((unsigned)time(0)); randomnumber = rand()%10+1;   It's a minor nitpick, but I couldn't refuse: statistically that's not quite correct. Consider this informal 'proof': when you put the numbers 0, 1, 2 ... 2^15-1 in jars numbered 0, 1, 2, ... 9 where the random number % 10 is the jar number, you'd notice that the jars 0, 1, 2, 3, 4, 5, 6, 7 have one number more in them than the other two jars 8 and 9 while each jar should be filled with the same number of numbers. To straigten up this slight bias you should reject any random number greater than RAND_MAX-RAND_MAX%nof_jars and try a next random number for further processing. kind regards, Jos Jun 4 '07 #5

### Post your reply

Sign in to post your reply or Sign up for a free account.