Blackjack Probabilities

108 Views Asked by At

I'm currently thinking about a code question about the C language, its a game called Blackjack, and here is the original question: In practice, one need to play the game a large number of times to get an accurate expected value. Thus, each row of the table should be the results of at least 100,000 experiments. For example, for a particular target points, say 10 points, two cards are drawn first. If the sum of these two cards exceeds 10 points then this experiment is a failure. If the sum is exactly 10 points, then it is a success. If it is less than 10 points, then another card is drawn. If case of neither a failure (more than 10 points) or a success (exactly 10 points), cards are continuously drawn until a conclusive results is obtained. After 100,000 experiments, the probability of getting 10 points should be printed together with the average number of cards of getting 10 points (the third column of the table).

Below is my current code:

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

int main(void) {
  int r1,r2,count,sum,cardsadd,k;
  int aftersum=sum+k;
  unsigned int total,cardsum;
  float percent,cards;
  printf("Points  Probability  #Cards\n");
  for (int points=4; points<=21; points++){
    count = 0;
    total = 0;
    cardsum = 0;
    do{
      r1 = rand()%13 + 1;
      r2 = rand()%13 + 1;
      if(r1>10) r1=10;
      if(r2>10) r2=10;
      sum = r1+r2;
      if(r1==1 && r2==1) sum=12;
      else if ((r1==1 || r2==1) && r1!=r2) sum+=10;
      count++;
      cardsadd=0;
      if(sum==points){
        total++;
        cardsum+=2;
      }
      else if(sum<points){
        while(sum<points){
          do{
              cardsadd+=1;
              k = rand()%13 + 1;
              if(k>10) k=10;
              else if(k==1){
                if(sum<=10) k=11;
              }
          }while(aftersum>points);
          sum+=k;
        }
        total+=1;
        cardsum+=aftersum;
      }
    }while(count<100000);
  percent = (float)total/1000;
  cards = (float)cardsum/100000; 
  printf("  %2d      %5.2lf%%      ",points,percent);
  printf("%.2lf\n",cards);
  }
  return 0;
}

In my code, variable count is the times needed to execute for each cards (4 to 21), total is the correct times when sum of the cards number is successfully equal to the points we want in the beginning (for loop). And cardsum is the total cards we need in 100000 tests, cardsadd is used when the first two cards drawn is less than the point we want, then we will keep drawing until sum of the point is equal to the points in the beginning. I don't have the correct answer yet but I know my code is surely wrong, as I can clearly see that the average cards we need to get 4 points is not 2.00. Hope someone can tell me how I should correct my code to get the answer. If anything is not clearly narrated, I will give a more complete explanation of the parts. Thanks for helping.

1

There are 1 best solutions below

0
lexer31 On

With an ace you have 2 possibles scores (the soft and the hard); You cannot compare "points" with only score in case you have an ace because for example with ace and 5 you can have 6 or 16; You need to modify your program to take this both scores in consideration (in case of an ace);