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** && diceRoll2 == diceRoll1 && 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 & D
int rollOneDie()
{
return (rand() % dieFaces) + 1;
}
void rollTheDice(int &die1, int &die2, int &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 < diceCount; i++)
{
dice[i] = rollOneDie();
}
}
bool allTheDiceEqualThisNumber(int dice[],int faceValue)
{
for (int i = 0; i < 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 < dieFaces - 1; i++) {
if (allTheDiceEqualThisNumber(dice, i)) {
return normalScore;
}
}
}
return noScore;
}
void showDice(int dice[])
{
printf("Your dices are ");
for (int i = 0; i < diceCount; i++)
{
printf("%d ", dice[i]);
}
printf("\n");
}
void playGame()
{
int diceRolls[diceCount];
rollTheDice2(diceRolls);
showDice(diceRolls);
int newCredits = earnCredit(diceRolls);
if (newCredits > 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", &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 <iostream>
, it uses #include <stdio.h>
instead. Also input in C++
generally prefers
std::cin >> variableName;
over scanf
, scanf_s
or getchar()
and
std::cout << "Your score was " << variableName << "\n";
over printf().
C++ also has a newer random number generator with better distribution using #include <random>
.