int dice_roll_with_average_scoring(struct dice dice, struct rnd *rnd) { assert(dice_is_valid(dice)); assert(rnd); int die_scores[dice.count]; dice_roll(dice, rnd, die_scores); int const high_roll = dice.sides; int const low_roll = 1; int const low_plus_high = high_roll + low_roll; int const low_average = low_plus_high / 2; int const high_average = low_average + (low_plus_high % 2); int score = 0; for (int i = 0; i < dice.count; ++i) { if (die_scores[i] == low_roll) { score += low_average; } else if (die_scores[i] == high_roll) { score += high_average; } else { score += die_scores[i]; } } return score; }
game(FILE *fp) { char yes[5]; int i; int dice1 = dice_roll(fp); int dice2 = dice_roll(fp); int point = dice1 + dice2; int cont = 1; printf("\nYou have rolled %d + %d = %d\n",dice1,dice2,point); if(point == 7 || point == 11) { printf("You Win!\n"); } else if(point == 2 || point == 3 || point == 12) { printf("You Lose!\n"); } else { printf("The point is %d\n",point); while(cont == 1) { dice1 = dice_roll(fp); dice2 = dice_roll(fp); printf("\nYou have rolled %d + %d = %d\n",dice1,dice2,(dice1 + dice2)); if((dice1+dice2) == 7) { printf("You Lose!\n"); cont = 0; } else if((dice1+dice2) == point) { printf("You Win!\n"); cont = 0; } else { printf("Still in the game\n"); } } } printf("\nWould you like to play again?\n"); scanf("%s",yes); for(i = 0; yes[i]; i++) { tolower(yes[i]); } if(strcmp(yes,"yes") == 0) { game(fp); } }
static void dice_roll_test(void) { struct rnd *always_one = rnd_alloc_fake_fixed(0); struct rnd *always_two = rnd_alloc_fake_fixed(1); int score; score = dice_roll(dice_make(0, 1), always_one, NULL); assert(0 == score); score = dice_roll(dice_make(3, 1), always_two, NULL); assert(3 == score); score = dice_roll(dice_make_plus(1, 1, 1), always_one, NULL); assert(2 == score); score = dice_roll(dice_make_plus(5, 1, -1), always_two, NULL); assert(4 == score); score = dice_roll(dice_make_plus_times(5, 1, 0, 10), always_one, NULL); assert(50 == score); score = dice_roll(dice_make_plus(0, 4, 1), always_two, NULL); assert(1 == score); rnd_free(always_one); rnd_free(always_two); }
char* generate_word(const unsigned max_word_length) { unsigned i, word_length = rand() % max_word_length + MIN_WORD_LENGHT, is_consonant = dice_roll(2); char* word = calloc(word_length + 2, sizeof(char)); // 2 = space + null for (i = 0; i < word_length; ++i) { if ((!is_consonant && dice_roll(5)) || is_consonant || 1 == word_length) { word[i] = get_vowel(); is_consonant = 0; continue; } word[i] = get_consonant(); is_consonant = 1; } word[word_length] = ' '; return word; }
int dice_roll_and_drop_lowest(struct dice dice, struct rnd *rnd) { assert(dice_is_valid(dice)); assert(rnd); int die_scores[dice.count]; dice_roll(dice, rnd, die_scores); qsort(die_scores, (size_t)dice.count, sizeof die_scores[0], compare_die_scores); int score = 0; for (int i = 1; i < dice.count; ++i) { score += die_scores[i]; } return score; }
int dice_roll_and_adjust_upwards(struct dice dice, struct rnd *rnd) { assert(dice_is_valid(dice)); assert(rnd); int die_scores[dice.count]; dice_roll(dice, rnd, die_scores); int score = 0; for (int i = 0; i < dice.count; ++i) { if (die_scores[i] < 6) { score += die_scores[i] + 1; } else { score += die_scores[i]; } } return score; }
static void generate_monetary_treasure(struct treasure *treasure, struct rnd *rnd) { int multiple_rolls = 0; int max_score = 20; do { int score = dice_roll(dice_make(1, max_score), rnd, NULL); if (score <= 2) { generate_monetary_treasure_1_to_2_copper_and_silver(treasure, rnd); --multiple_rolls; } else if (score <= 5) { generate_monetary_treasure_3_to_5_electrum(treasure, rnd); --multiple_rolls; } else if (score <= 10) { generate_monetary_treasure_6_to_10_gold(treasure, rnd); --multiple_rolls; } else if (score <= 12) { generate_monetary_treasure_11_to_12_platinum(treasure, rnd); --multiple_rolls; } else if (score <= 15) { generate_monetary_treasure_13_to_15_gems(treasure, rnd); --multiple_rolls; } else if (score <= 17) { generate_monetary_treasure_16_to_17_jewelry(treasure, rnd); --multiple_rolls; } else if (score <= 18) { max_score = 17; multiple_rolls = 2; } else if (score <= 19) { max_score = 17; multiple_rolls = 3; } else { generate_monetary_treasure_1_to_2_copper_and_silver(treasure, rnd); generate_monetary_treasure_3_to_5_electrum(treasure, rnd); generate_monetary_treasure_6_to_10_gold(treasure, rnd); generate_monetary_treasure_11_to_12_platinum(treasure, rnd); generate_monetary_treasure_13_to_15_gems(treasure, rnd); generate_monetary_treasure_16_to_17_jewelry(treasure, rnd); } } while (multiple_rolls > 0); }
/** * Determine the damage of a spell attack which ignores monster hp * (i.e. bolts and balls, including arrows/boulders/storms/etc.) * * \param spell is the attack type * \param race is the monster race of the attacker * \param dam_aspect is the damage calc required (min, avg, max, random) */ static int nonhp_dam(const struct monster_spell *spell, const struct monster_race *race, aspect dam_aspect) { int dam = 0; struct effect *effect = spell->effect; /* Set the reference race for calculations */ ref_race = race; /* Now add the damage for each effect */ while (effect) { random_value rand; if (effect->dice) { dice_roll(effect->dice, &rand); dam += randcalc(rand, 0, dam_aspect); } effect = effect->next; } ref_race = NULL; return dam; }
/** * Describe an object's effect, if any. */ static bool describe_effect(textblock *tb, const struct object *obj, bool only_artifacts, bool subjective) { char desc[200]; struct effect *effect = NULL; bool aimed = false; int min_time, max_time, failure_chance; /* Sometimes we only print artifact activation info */ if (only_artifacts && !obj->artifact) return false; if (obj_known_effect(obj, &effect, &aimed, &min_time, &max_time, &failure_chance) == false) return false; /* Effect not known, mouth platitudes */ if (!effect && object_effect(obj)) { if (aimed) textblock_append(tb, "It can be aimed.\n"); else if (tval_is_edible(obj)) textblock_append(tb, "It can be eaten.\n"); else if (tval_is_potion(obj)) textblock_append(tb, "It can be drunk.\n"); else if (tval_is_scroll(obj)) textblock_append(tb, "It can be read.\n"); else textblock_append(tb, "It can be activated.\n"); return true; } /* Activations get a special message */ if (obj->activation && obj->activation->desc) { textblock_append(tb, "When activated, it "); textblock_append(tb, obj->activation->desc); } else { int random_choices = 0; /* Get descriptions for all the effects */ effect = object_effect(obj); if (!effect_desc(effect)) return false; if (aimed) textblock_append(tb, "When aimed, it "); else if (tval_is_edible(obj)) textblock_append(tb, "When eaten, it "); else if (tval_is_potion(obj)) textblock_append(tb, "When quaffed, it "); else if (tval_is_scroll(obj)) textblock_append(tb, "When read, it "); else textblock_append(tb, "When activated, it "); /* Print a colourised description */ while (effect) { char *next_char = desc; int roll = 0; random_value value = { 0, 0, 0, 0 }; char dice_string[20]; int boost, level = obj->kind->level; /* Get the level */ if (obj->artifact) level = obj->artifact->level; else if (obj->ego) level = obj->ego->level; /* Get the boost */ boost = MAX(player->state.skills[SKILL_DEVICE] - level, 0); if (effect->dice != NULL) roll = dice_roll(effect->dice, &value); /* Deal with special random effect */ if (effect->index == EF_RANDOM) random_choices = roll + 1; /* Get the possible dice strings */ if (value.dice && value.base) strnfmt(dice_string, sizeof(dice_string), "%d+%dd%d", value.base, value.dice, value.sides); else if (value.dice) strnfmt(dice_string, sizeof(dice_string), "%dd%d", value.dice, value.sides); else strnfmt(dice_string, sizeof(dice_string), "%d", value.base); /* Check all the possible types of description format */ switch (base_descs[effect->index].efinfo_flag) { /* Healing sometimes has a minimum percentage */ case EFINFO_HEAL: { char min_string[50]; if (value.m_bonus) strnfmt(min_string, sizeof(min_string), " (or %d%%, whichever is greater)", value.m_bonus); else strnfmt(min_string, sizeof(min_string), ""); strnfmt(desc, sizeof(desc), effect_desc(effect), dice_string, min_string); break; } /* Nourishment is just a flat amount */ case EFINFO_CONST: { strnfmt(desc, sizeof(desc), effect_desc(effect), value.base/2); break; } case EFINFO_CURE: { strnfmt(desc, sizeof(desc), effect_desc(effect), timed_idx_to_desc(effect->params[0])); break; } case EFINFO_TIMED: { strnfmt(desc, sizeof(desc), effect_desc(effect), timed_idx_to_desc(effect->params[0]), dice_string); break; } case EFINFO_STAT: { strnfmt(desc, sizeof(desc), effect_desc(effect), mod_flags[effect->params[0]].name); break; } case EFINFO_SEEN: { strnfmt(desc, sizeof(desc), effect_desc(effect), gf_desc(effect->params[0])); break; } case EFINFO_SUMM: { strnfmt(desc, sizeof(desc), effect_desc(effect), summon_desc(effect->params[0])); break; } /* Only currently used for the player, but can handle monsters */ case EFINFO_TELE: { if (effect->params[0]) strnfmt(desc, sizeof(desc), effect_desc(effect), "a monster", value.base); else strnfmt(desc, sizeof(desc), effect_desc(effect), "you", value.base); break; } case EFINFO_QUAKE: { strnfmt(desc, sizeof(desc), effect_desc(effect), effect->params[1]); break; } case EFINFO_LIGHT: { strnfmt(desc, sizeof(desc), effect_desc(effect), dice_string, effect->params[1]); break; } /* Object generated balls are elemental */ case EFINFO_BALL: { strnfmt(desc, sizeof(desc), effect_desc(effect), elements[effect->params[0]].name, effect->params[1], dice_string); if (boost) my_strcat(desc, format(", which your device skill increases by %d per cent", boost), sizeof(desc)); break; } /* Bolts that inflict status */ case EFINFO_BOLT: { strnfmt(desc, sizeof(desc), effect_desc(effect), gf_desc(effect->params[0])); break; } /* Bolts and beams that damage */ case EFINFO_BOLTD: { strnfmt(desc, sizeof(desc), effect_desc(effect), gf_desc(effect->params[0]), dice_string); if (boost) my_strcat(desc, format(", which your device skill increases by %d per cent", boost), sizeof(desc)); break; } case EFINFO_TOUCH: { strnfmt(desc, sizeof(desc), effect_desc(effect), gf_desc(effect->params[0])); break; } case EFINFO_NONE: { strnfmt(desc, sizeof(desc), effect_desc(effect)); break; } default: { msg("Bad effect description passed to describe_effect(). Please report this bug."); return false; } } do { if (isdigit((unsigned char) *next_char)) textblock_append_c(tb, COLOUR_L_GREEN, "%c", *next_char); else textblock_append(tb, "%c", *next_char); } while (*next_char++); /* Random choices need special treatment - note that this code * assumes that RANDOM and the random choices will be the last * effect in the object/activation description */ if (random_choices >= 1) { if (effect->index == EF_RANDOM) ; else if (random_choices > 2) textblock_append(tb, ", "); else if (random_choices == 2) textblock_append(tb, " or "); random_choices--; } else if (effect->next) { if (effect->next->next && (effect->next->index != EF_RANDOM)) textblock_append(tb, ", "); else textblock_append(tb, " and "); } effect = effect->next; } } textblock_append(tb, ".\n"); if (min_time || max_time) { /* Sometimes adjust for player speed */ int multiplier = turn_energy(player->state.speed); if (!subjective) multiplier = 10; textblock_append(tb, "Takes "); /* Correct for player speed */ min_time = (min_time * multiplier) / 10; max_time = (max_time * multiplier) / 10; textblock_append_c(tb, COLOUR_L_GREEN, "%d", min_time); if (min_time != max_time) { textblock_append(tb, " to "); textblock_append_c(tb, COLOUR_L_GREEN, "%d", max_time); } textblock_append(tb, " turns to recharge"); if (subjective && player->state.speed != 110) textblock_append(tb, " at your current speed"); textblock_append(tb, ".\n"); } if (failure_chance > 0) { textblock_append(tb, "Your chance of success is %d.%d%%\n", (1000 - failure_chance) / 10, (1000 - failure_chance) % 10); } return true; }
int roll(char const *dice_string, struct rnd *rnd) { return dice_roll(dice_parse(dice_string), rnd, NULL); }
/** * Describe an object's effect, if any. */ static bool describe_effect(textblock *tb, const struct object *obj, bool only_artifacts, bool subjective) { char desc[200]; struct effect *e; int effect = 0; bool aimed = FALSE; int min_time, max_time, failure_chance; /* Sometimes we only print artifact activation info */ if (only_artifacts && !obj->artifact) return FALSE; if (obj_known_effect(obj, &effect, &aimed, &min_time, &max_time, &failure_chance) == FALSE) return FALSE; /* We don't know much */ if (effect == OBJ_KNOWN_PRESENT) { if (aimed) textblock_append(tb, "It can be aimed.\n"); else if (tval_is_edible(obj)) textblock_append(tb, "It can be eaten.\n"); else if (tval_is_potion(obj)) textblock_append(tb, "It can be drunk.\n"); else if (tval_is_scroll(obj)) textblock_append(tb, "It can be read.\n"); else textblock_append(tb, "It can be activated.\n"); return TRUE; } /* Obtain the description */ e = obj->effect; if (!effect_desc(e)) return FALSE; if (aimed) textblock_append(tb, "When aimed, it "); else if (tval_is_edible(obj)) textblock_append(tb, "When eaten, it "); else if (tval_is_potion(obj)) textblock_append(tb, "When quaffed, it "); else if (tval_is_scroll(obj)) textblock_append(tb, "When read, it "); else textblock_append(tb, "When activated, it "); /* Print a colourised description */ while (e) { char *next_char = desc; random_value value = { 0, 0, 0, 0 }; char dice_string[20]; if (e->dice != NULL) (void) dice_roll(e->dice, &value); /* Get the possible dice strings */ if (value.dice && value.base) strnfmt(dice_string, sizeof(dice_string), "%d+%dd%d", value.base, value.dice, value.sides); else if (value.dice) strnfmt(dice_string, sizeof(dice_string), "%dd%d", value.dice, value.sides); else strnfmt(dice_string, sizeof(dice_string), "%d", value.base); /* Check all the possible types of description format */ switch (base_descs[e->index].efinfo_flag) { /* Healing sometimes has a minimum percentage */ case EFINFO_HEAL: { char min_string[50]; if (value.m_bonus) strnfmt(min_string, sizeof(min_string), " (or %d%%, whichever is greater)", value.m_bonus); else strnfmt(min_string, sizeof(min_string), ""); strnfmt(desc, sizeof(desc), effect_desc(e), dice_string, min_string); break; } /* Nourishment is just a flat amount */ case EFINFO_FEED: { strnfmt(desc, sizeof(desc), effect_desc(e), value.base); break; } case EFINFO_CURE: { strnfmt(desc, sizeof(desc), effect_desc(e), timed_idx_to_desc(e->params[0])); break; } case EFINFO_TIMED: { strnfmt(desc, sizeof(desc), effect_desc(e), timed_idx_to_desc(e->params[0]), dice_string); break; } case EFINFO_STAT: { strnfmt(desc, sizeof(desc), effect_desc(e), mod_flags[e->params[0]].name); break; } case EFINFO_SEEN: { strnfmt(desc, sizeof(desc), effect_desc(e), gf_desc(e->params[0])); break; } case EFINFO_SUMM: { strnfmt(desc, sizeof(desc), effect_desc(e), summon_desc(e->params[0])); break; } /* Only currently used for the player, but can handle monsters */ case EFINFO_TELE: { if (e->params[0]) strnfmt(desc, sizeof(desc), effect_desc(e), "a monster", value.base); else strnfmt(desc, sizeof(desc), effect_desc(e), "you", value.base); break; } case EFINFO_QUAKE: { strnfmt(desc, sizeof(desc), effect_desc(e), e->params[1]); break; } case EFINFO_LIGHT: { strnfmt(desc, sizeof(desc), effect_desc(e), dice_string, e->params[1]); break; } /* Object generated balls are elemental */ case EFINFO_BALL: { strnfmt(desc, sizeof(desc), effect_desc(e), elements[e->params[0]].name, e->params[1], dice_string); break; } /* Bolts that inflict status */ case EFINFO_BOLT: { strnfmt(desc, sizeof(desc), effect_desc(e), gf_desc(e->params[0])); break; } /* Bolts and beams that damage */ case EFINFO_BOLTD: { strnfmt(desc, sizeof(desc), effect_desc(e), gf_desc(e->params[0]), dice_string); break; } case EFINFO_TOUCH: { strnfmt(desc, sizeof(desc), effect_desc(e), gf_desc(e->params[0])); break; } default:strnfmt(desc, sizeof(desc), effect_desc(e)); break; } do { if (isdigit((unsigned char) *next_char)) textblock_append_c(tb, COLOUR_L_GREEN, "%c", *next_char); else textblock_append(tb, "%c", *next_char); } while (*next_char++); if (e->next) { if (e->next->next) textblock_append(tb, ", "); else textblock_append(tb, " and "); } e = e->next; } textblock_append(tb, ".\n"); if (min_time || max_time) { /* Sometimes adjust for player speed */ int multiplier = turn_energy(player->state.speed); if (!subjective) multiplier = 10; textblock_append(tb, "Takes "); /* Correct for player speed */ min_time *= multiplier / 10; max_time *= multiplier / 10; textblock_append_c(tb, COLOUR_L_GREEN, "%d", min_time); if (min_time != max_time) { textblock_append(tb, " to "); textblock_append_c(tb, COLOUR_L_GREEN, "%d", max_time); } textblock_append(tb, " turns to recharge"); if (subjective && player->state.speed != 110) textblock_append(tb, " at your current speed"); textblock_append(tb, ".\n"); } if (failure_chance > 0) { textblock_append(tb, "Your chance of success is %d.%d%%\n", (1000 - failure_chance) / 10, (1000 - failure_chance) % 10); } return TRUE; }
int main (int argc, char *argv[]) { SDL_Surface *screen; printf ("Initializing SDL.\n"); /* Initializes Audio and the CDROM, add SDL_INIT_VIDEO for Video */ if (SDL_Init(SDL_INIT_VIDEO)< 0) { printf("Could not initialize SDL:%s\n", SDL_GetError()); SDL_Quit(); return 1; } if(TTF_Init() != 0) { printf("Could not initialize TTF:%s\n", SDL_GetError()); return 1; } printf("Video initialized correctly\n"); atexit( SDL_Quit ); printf("SCREENW : %d, SCREENH : %d\n", SCREENW, SCREENW); screen = SDL_SetVideoMode( SCREENW, SCREENH, 16, SDL_HWSURFACE ); SDL_WM_SetCaption( "421", 0 ); if( screen == NULL ) { printf( "Can't set video mode: %s\n", SDL_GetError( ) ); return EXIT_FAILURE; } // Part of the bitmap that we want to draw SDL_Rect bgsource; bgsource.x = 0; bgsource.y = 0; bgsource.w = 1000; bgsource.h = 903; // Part of the screen we want to draw the sprite to SDL_Rect bgdestination; bgdestination.x = 0; bgdestination.y = 0; bgdestination.w = 1000; bgdestination.h = 903; SDL_Surface *background; background = (SDL_Surface*) IMG_Load("pixs/background.png"); background = SDL_DisplayFormat(background); SDL_BlitSurface(background, &bgsource, screen, &bgdestination); SDL_Flip(screen); // Part of the bitmap that we want to draw SDL_Rect d1source; d1source.x = 0; d1source.y = 0; d1source.w = 189; d1source.h = 199; // Part of the screen we want to draw the sprite to SDL_Rect d1destination; d1destination.x = 0; d1destination.y = 0; d1destination.w = 189; d1destination.h = 199; SDL_Surface *dice; dice = (SDL_Surface*) IMG_Load("pixs/dices.png"); dice = SDL_DisplayFormat(dice); Uint32 colorkey = SDL_MapRGB(dice->format, 0, 255, 0); SDL_SetColorKey(dice, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey ); bool quit = false; SDL_Event event; struct dice *d1, *d2, *d3; d1 = NULL; d2 = NULL; d3 = NULL; hands *ah; ah = hands_new(); ah = hands_init(ah); hands_print(ah); while(quit == false) { while( SDL_PollEvent( &event ) ) { int x = 0; int y = 0; if(event.type == SDL_MOUSEBUTTONDOWN) { x = event.motion.x; y = event.motion.y; printf("Down (%d, %d)!!!! \n", x, y); //board_click(bd, x/24, y/24); d1 = dice_new(490, 240); d2 = dice_new(340, 340); d3 = dice_new(650, 440); if(!d1->rolling) d1 = dice_roll(d1); if(!d2->rolling) d2 = dice_roll(d2); if(!d3->rolling) d3 = dice_roll(d3); } // User press key if( event.type == SDL_KEYDOWN ) { Uint8 *keystates = SDL_GetKeyState( NULL ); // User press ESCAPE if( keystates[ SDLK_ESCAPE ] ) { quit = true; } } // User press X if( event.type == SDL_QUIT ) { quit = true; } } if(d1 && d2 && d3) if(d1->rolling || d2->rolling || d3->rolling) { SDL_BlitSurface(background, &bgsource, screen, &bgdestination); d1 = dice_check_dices_collisions(d1, d2, d3); d2 = dice_check_dices_collisions(d2, d3, d1); d3 = dice_check_dices_collisions(d3, d1, d2); } if(d1) { if(d1->rolling) d1 = dice_update(d1); d1source.x = d1->imgoffset; d1destination.x = d1->position->x; d1destination.y = d1->position->y; SDL_BlitSurface(dice, &d1source, screen, &d1destination); } if(d2) { if(d2->rolling) d2 = dice_update(d2); d1source.x = d2->imgoffset; d1destination.x = d2->position->x; d1destination.y = d2->position->y; SDL_BlitSurface(dice, &d1source, screen, &d1destination); } if(d3) { if(d3->rolling) d3 = dice_update(d3); d1source.x = d3->imgoffset; d1destination.x = d3->position->x; d1destination.y = d3->position->y; SDL_BlitSurface(dice, &d1source, screen, &d1destination); } SDL_Flip(screen); } return 0; }