Dice game with rules and three dice I'm trying to make a dice game with three dice and with rules.


It costs 10 credits to roll the dice.
If you get 6 6 6 you win 90 credits.
If you get 5 5 5, 4 4 4, 3 3 3, 2 2 2, 1 1 1, you win 40 credits.
If two of the dice have the same number you win 0 credits (because it costs 10 to roll).


Here's what I've done so far:

#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

int main(void) {

    srand((int)time(NULL));

    int diceRoll1, diceRoll2, diceRoll3;
    char yn;

    do {
        printf("\nDo you want to play? (y/n): ");
        scanf_s("%c", &amp;yn);
        getchar();

        if (yn == 'n') {
            printf("Coward!");
            getchar();
            return 0;
        }

        diceRoll1 = (rand() % 6) + 1;
        diceRoll2 = (rand() % 6) + 1;
        diceRoll3 = (rand() % 6) + 1;

        printf("Your dices are %d %d %d \n", diceRoll1, diceRoll2, diceRoll3);

        if (diceRoll1 == 6 &amp;&amp; diceRoll2 == 6 &amp;&amp; diceRoll3 == 6) {
            printf("Congratulations! You won 90 credits.\n");
        }

        if (diceRoll1 == 5 &amp;&amp; diceRoll2 == 5 &amp;&amp; diceRoll3 == 5) {
            printf("Congratulations! You won 40 credits.\n");
        }

        if (diceRoll1 == 4 &amp;&amp; diceRoll2 == 4 &amp;&amp; diceRoll3 == 4) {
            printf("Congratulations! You won 40 credits.\n");
        }

        if (diceRoll1 == 3 &amp;&amp; diceRoll2 == 3 &amp;&amp; diceRoll3 == 3) {
            printf("Congratulations! You won 40 credits.\n");
        }

        if (diceRoll1 == 2 &amp;&amp; diceRoll2 == 2 &amp;&amp; diceRoll3 == 2) {
            printf("Congratulations! You won 40 credits.\n");
        }

        if (diceRoll1 == 1 &amp;&amp; diceRoll2 == 1 &amp;&amp; diceRoll3 == 1) {
            printf("Congratulations! You won 40 credits.\n");
        }

    } while (yn != 'n');


    getchar();
    return 0;

}



I have not implemented all of the rules and the ones I have done can probably be done in a better way. I don't have much experience, so I wonder what the best way is to have all the rules in the game and so the code looks good.
Reducing the amount of code in any program is good because that reduces the possible errors or bugs in the code. Providing general functions that can be reused is a good habit to get into.

DRY Code

There is a programming principle called the Don't Repeat Yourself Principle sometimes referred to as DRY code. If you find yourself repeating the same code multiple times it is better to encapsulate it in a function. If it is possible to loop through the code that can reduce repetition as well.

As shown in another answer all the if statements of the form 

        if (diceRoll1 == **x** &amp;&amp; diceRoll2 == diceRoll1 &amp;&amp; diceRoll3 == diceRoll1) {
            printf("Congratulations! You won **YY** credits.\n");
        }


Can be reduced to a simple function that can be reused.

This code can also be simplified, although it is not absolutely necessary

    diceRoll1 = (rand() % 6) + 1;
    diceRoll2 = (rand() % 6) + 1;
    diceRoll3 = (rand() % 6) + 1;


Can be 

static constexpr int dieFaces = 6;      // Allows the number of faces of the dice to be changed in one place
                                        // if this is used in a board game such as D &amp; D

int rollOneDie()
{
    return (rand() % dieFaces) + 1;
}

void rollTheDice(int &amp;die1, int &amp;die2, int &amp;die3)
{
    die1 = rollOneDie();
    die2 = rollOneDie();
    die3 = rollOneDie();
}


Any time I see variables with the names var1, var2 and var3 I see the potential for using an array instead of separate variables. The entire program might be simplified if instead of 3 separate variables diceRoll1, diceRoll2 and diceRoll3 there was one array of dice. This would also allow the program to be easily changed so that more or less dice are used.

static constexpr int diceCount = 3;
static constexpr int highScore = 90;
static constexpr int normalScore = 40;
static constexpr int lowScore = 10;
static constexpr int noScore = 0;

void rollTheDice2(int dice[])
{
    for (int i = 0; i &lt; diceCount; i++)
    {
        dice[i] = rollOneDie();
    }
}

bool allTheDiceEqualThisNumber(int dice[],int faceValue)
{
    for (int i = 0; i &lt; diceCount; i++)
    {
        if (dice[i] != faceValue)
        {
            return false;
        }
    }
    return true;
}

int earnCredit(int dice[])
{
    if (allTheDiceEqualThisNumber(dice, dieFaces))
    {
        return highScore;
    }
    else if (allTheDiceEqualThisNumber(dice, 1))
    {
        return lowScore;
    }
    else
    {
        for (int i = 2; i &lt; dieFaces - 1; i++) {
            if (allTheDiceEqualThisNumber(dice, i)) {
                return normalScore;
            }
        }
    }
    return noScore;
}

void showDice(int dice[])
{
    printf("Your dices are ");
    for (int i = 0; i &lt; diceCount; i++)
    {
        printf("%d ", dice[i]);
    }
    printf("\n");
}

void playGame()
{
    int diceRolls[diceCount];

    rollTheDice2(diceRolls);

    showDice(diceRolls);

    int newCredits = earnCredit(diceRolls);
    if (newCredits &gt; 0)
    {
        printf("Congratulations! You won 90 credits.\n");
    }
}


Complexity

The function main() is too complex (does too much). As programs grow in size the use of main() should be limited to calling functions that parse the command line, calling functions that set up for processing, calling functions that execute the desired function of the program, and calling functions to clean up after the main portion of the program.

There is also a programming principle called the Single Responsibility Principle that applies here. The Single Responsibility Principle states:


  that every module, class, or function should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by that module, class or function.


int main(void)
{
    srand((int)time(NULL));

    char yn;

    do {
        printf("\nDo you want to play? (y/n): ");
        scanf_s("%c", &amp;yn);
        getchar();

        if (yn == 'n') {
            printf("Coward!");
            getchar();
            return 0;
        }

        playGame();

    } while (yn != 'n');


    getchar();
    return 0;
}


Magic Numbers

There are Magic Numbers in the main() function (6, 90, 40, 10), it might be better to create symbolic constants for them to make the code more readble and easier to maintain. These numbers may be used in many places and being able to change them by editing only one line makes maintenance easier.

Numeric constants in code are sometimes referred to as Magic Numbers, because there is no obvious meaning for them. There is a discussion of this on stackoverflow.

The possible symbolic constants are show in the examples for DRY Code

C versus C++

This question was originally tagged with C rather than C++. The strict version of C can't compile #include &lt;iostream&gt;, it uses #include &lt;stdio.h&gt; instead. Also input in C++ generally prefers

std::cin &gt;&gt; variableName;


over scanf, scanf_s or getchar() and 

std::cout &lt;&lt; "Your score was " &lt;&lt; variableName &lt;&lt; "\n";


over printf().

C++ also has a newer random number generator with better distribution using #include &lt;random&gt;.