int main (int argc, char **argv) { GError *error = NULL; GOptionContext *context = g_option_context_new ("ROLL_FORMAT_STRING\n"); g_option_context_set_summary (context, "Rolls some dice or shows the odds to get a successful roll.\n" "ROLL_FORMAT_STRING may be for example 1d10+2 or 3d6-1."); g_option_context_add_main_entries (context, entries, NULL); if (! g_option_context_parse (context, &argc, &argv, &error)) { g_print ("option parsing failed: %s\n", error->message); exit (1); } if (remaining_args == NULL) { char *help_str = g_option_context_get_help (context, TRUE, NULL); g_print ("%s", help_str); g_free (help_str); exit (2); } char *n_sides_str = NULL; char *modifier_str = NULL; char *roll_format_str = remaining_args[0]; char *n_dice_str = roll_format_str; char *ptr = roll_format_str; do { if (*ptr == 'd' || *ptr == 'D') { *ptr = '\0'; n_sides_str = ++ptr; } else if (*ptr == '+' || *ptr == '-') { modifier_str = ptr; } } while (*ptr++); int n_dice = atoi (n_dice_str); int n_sides = (n_sides_str) ? atoi (n_sides_str) : 0; int modifier = (modifier_str) ? atoi (modifier_str) : 0; int score = 0; // roll result, including modifiers int i; if (is_probability_mode) { /* For a roll result (which is 1-based) to be used as frequency array * index (which is 0-based), the 0 value needs to be taken into * account. It may in fact happen when using a negative modifier */ int max_score = n_dice * n_sides + modifier; int n_freq = max_score + 1; unsigned int *freq = calloc (n_freq, sizeof (unsigned int)); /* Enumerating all the possible rolls is like counting in the * "n_sides" base with "n_dice" digits. */ int n_rolls = pow (n_sides, n_dice); for (i = 0; i < n_rolls; i++) { score = 0; int roll = i; int die_index = n_dice; // for each die while (die_index-- > 0) { // get rightmost die value score += (roll % n_sides) + 1; // right-shift roll /= n_sides; } score += modifier; freq[score]++; } print_freq_table (freq, n_freq, n_rolls); free (freq); } else { /* Roll the dice */ Die *die = die_new (n_sides); for (i = 0; i < n_dice; i++) { score += die_roll (die); } score = MAX (0, score + modifier); printf ("%d\n", score); die_free (die); } return 0; }
int Game(double *bankroll, char *name)//Return winnings { printf("Hello %s ",name); double bet = bet_func(&(*bankroll)); double winnings = 0; int point,choice,double_bet; int result;//1 True 0 False printf("Would you like to bet for yourself or against?\nChoose 1 for yourself or 2 for against\t"); scanf("%d",&choice); while(1) { int dice_val = die_roll(); printf("The total value of the die roll is %d\n",dice_val); switch(dice_val) { case 2: case 3: case 12: if(choice == 1) { printf("You have lost the game.\n"); result = 0; } break; case 7: case 11: if(choice == 1) { printf("You have won the game.\n"); result = 1; } break; default: point = dice_val; printf("You will need to roll again.\n"); if(choice ==1) { printf("Would you like to double your bet?\nYes = 1\tNo = 0\t"); scanf("%d",&double_bet); if(double_bet==1) { (*bankroll)=(double)(*bankroll)-bet; bet= (double)bet *2; } printf("\nThe current bet on the table is %.02lf\n\n",bet); dice_val=0; for(;;) { dice_val=die_roll(); printf("The total value of the die roll is %d\n",dice_val); printf("Your point value is: %d\n",point); if(dice_val==7||dice_val==11||dice_val==point) { break; } } if(dice_val==7||dice_val==11) { result=0; } else if(dice_val==point) { result=1; } break; } } switch(result) { case 1: printf("You have won the following\t$%.2lf\n",bet); winnings+=(bet*2); return winnings; case 0: printf("You have lost the following\t$%.2lf\n",bet); return winnings; } } return winnings; }