1

I am working on a game of Yahtzee and one part of the game is the user can choose which dice out of 5 they wish to re-roll. I don't really know how to approach this besides writing a ton of if if-else statements, but there has to be a more efficient way to re-roll the specific die/ dice. I wrote out a snippet of what I am trying to accomplish, its not exactly like this in my actual code, but hopefully it is enough to answer the question :)

#include<stdio.h>

int main(void)
{
    int die1 = 0, die2 = 0, die3 = 0, die4 = 0, die5 = 0;
    int *ptr_die1 = &die1, *ptr_die2 = &die2, *ptr_die3 = &die3, *ptr_die4 = &die4, *ptr_die5 = &die5;
    int choice = 0;
    int die[5] = { 0 };


    for (int i = 0; i < 5; i++)
    {
        die[i] = rand() % 6 + 1;
    }



    printf("Die[1] = %d\n", die[0]);
    printf("Die[2] = %d\n", die[1]);
    printf("Die[3] = %d\n", die[2]);
    printf("Die[4] = %d\n", die[3]);
    printf("Die[5] = %d\n", die[4]);


    choice = printf("Please select which die to reroll\n");
    scanf("%d", &choice); 

    printf("%d\n", choice);

    for (int i = 0; i < 5; i++)
    {
        die[choice-1] = rand() % 6 + 1;
    }

    printf("Die[1] = %d\n", die[0]);
    printf("Die[2] = %d\n", die[1]);
    printf("Die[3] = %d\n", die[2]);
    printf("Die[4] = %d\n", die[3]);
    printf("Die[5] = %d\n", die[4]);

    return 0;
}

after this I am really lost on how to change the die because the user could want to change just 1, or all 5 or any combination in between...

sarah campolt
  • 148
  • 2
  • 13
  • The user's input gives you the index into the array right? So just use that to find which dice value to change. – kaylum Mar 10 '17 at 01:35
  • But what if the user inputs 2 and 5? thats what is confusing me, if they want to change 2 or more.. and how would my scanf change for inputting more than 1 number – sarah campolt Mar 10 '17 at 01:36
  • Well, that's what loops are for. Read input, change value, read next input, change value, etc – kaylum Mar 10 '17 at 01:37
  • Have you heard of arrays? Do you know how to use them yet? Hmmm; yes, there's `int die[5];`. Why do you have 5 `dieN` variables and 5 `ptr_dieN` variables? Apart from making your code longer, what is the benefit they provide? – Jonathan Leffler Mar 10 '17 at 01:43
  • @JonathanLeffler I am just learning arrays, I'm still figuring them out – sarah campolt Mar 10 '17 at 01:44
  • OK; we all go through a learning phase — some of us did ours longer ago than others. I think that the number-suffixed variables are just going to confuse you. You should keep the array `int die[5];`, and use a loop to set the values (instead of 5 systematically different assignments), and so on. About the only thing to watch is that users think in terms of dice 1 through dice 5, but the array indexes go from 0..4. – Jonathan Leffler Mar 10 '17 at 01:47
  • Okay so I figured out how to change one die :D, is there a way to do it when there could be anywhere between 1 - 5? @JonathanLeffler I changed my current to code to what I'm experimenting with, I know printing could be preformed in a loop, but this was just for simplicity sake :) – sarah campolt Mar 10 '17 at 02:01
  • in C, when compiling, always enable all the warnings, then fix those warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -pedantic` I also use: `-Wconversion -std=gnu11` ) – user3629249 Mar 10 '17 at 17:55
  • the posted code is missing `#include `, needed for the `rand()` function. Also before calling `rand()` the function: `srand()` needs to be called (only once) to seed the `rand()` function. Similar to: `srand( time(NULL) );` which will require also having the statement: `#include ` – user3629249 Mar 10 '17 at 17:59

4 Answers4

0

You could have the user input a comma-separatd list of die, instead of a single integer, which it looks like you're doing now. Then just parse the input, check that you have between 1 and 5 valid integers less than 6, and index into each die.

Or you could do like kaylum suggested and loop until the user inputs a special string indicating they're done, or prompt for 1, 2, ... 5 and ask for a yes or no answer to each.

Cameron
  • 84
  • 1
  • 11
0

Just use an array of int values to represent the set of dice:

#define DICE_COUNT 6

void rollDice(int* diceArray, size_t diceIndex) {

    assert( 0 <= diceIndex && diceIndex < DICE_COUNT );

    diceArray[ diceIndex ] = rand() % 6 + 1;
}

int main(int argc, char* argv[]) {

    // Seed the RNG:
    srand( (unsigned)time(&t) );

    int dice[DICE_COUNT];

    for(size_t i = 0; i < DICE_COUNT; i++) {

        rollDice( dice, i );
    }

    while( true ) {

        printf("Please select which die to reroll. Enter -2 to quit. (%d to %d inclusive)", 1, DICE_COUNT);
        int selection = -1;
        scanf("%d", &selection);
        if( selection == -2 ) break;

        if( 1 <= selection && selection <= DICE_COUNT ) {

            selection--; // convert from 1-6 to 0-5.

            rollDice( dice, selection );
        }
    }

    return EXIT_SUCCESS;
}
Dai
  • 141,631
  • 28
  • 261
  • 374
0

You have way too many variables that are not really needed.

int main(int argc, char **argv) 
{
  int i;
  int choice;
  int dices[5];
  srand(time(NULL));

  while(1){
    for (i = 0; i < 5; i++)
      dices[i] = rand() % 6 + 1;

    choice = printf("Please select which die to reroll (or enter 0 to quit)");
    scanf("%d", &choice);
    if (choice == 0)   // end the game
       break;    
    if (choice < 1 || choice > 5 ){  // make sure that input is valid
      fprintf(stderr, "error, input should be between 1 to 5 (inclusive)\n");
      return -1;
    }
    printf("dice shows: %d", dices[choice-1]);
  }
  return 0;
}

You need to ask the user for the ending it, e.g. "Enter 0 to end the game". Otherwise it would be an infinite loop.

Arash
  • 1,950
  • 14
  • 17
0

I don't see any if..else statements in your code above. I will say, in this chunk of code:

for (int i = 0; i < 5; i++)
{
    die[choice-1] = rand() % 6 + 1;
}

You don't need the for loop. You are not using the index, and rand() should work the first time through. I know rand() isn't the best written function, but if you seed it first, it should give you a pseudo-random number.

#include <time.h>
...
/* initialize random seed: */
srand ( time(NULL) );
...
die[choice-1] = rand() % 6 + 1;

Hope this was helpful, if you are still even working on that project!

Ryan
  • 436
  • 5
  • 15