Image |
In this project, we are going to create a simple dice game using C programming language.
Table of Content
Introduction
The dice game program codes were written using code::blocks IDE (Integrated Development Environment). code::blocks allow you to write codes and compile them, and allow you see how the programs perform. Our C programming tutorial covers the instructions on how to install and use code::blocks.
The Dice Game program rolls two dice and presents the total. It then asks the user to guess if the next total will be higher, lower or equal. It then rolls two more dice and tells the user how they did.
The game uses a do-while loop, to ensure the user continues to play the game until he/she fills like quitting.
Procedure
Before you start this project please download the code::blocks IDE if you do not have yet, from the link above. After downloading to install the Code::Blocks software.
Then open the software and click on "Empty file" at the top menu bar to open a blank page.
After you open an "Empty file" on code::blocks, save the file with the name dice_game.c
Step 1
First, lets write the code for the variable names for the game. The first batch of code is required if the dice game program will work. the next batch of code starting from the main () defines local variable names for the dice game.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
main()
{
int dice1, dice2;
int total1, total2;
time_t t;
char ans;
int choice;
Step 2
The next code is required to make sure each random number generated by the dice is different. This random number generation generate different numbers at different times. Each time you run the program, a new set of numbers are generated for the dice game
srand(time(&t));
Step 3
The next codes will roll the first dices, and give you the total, it then allows you to make your predictions
do
{
/* This would give you a number between 0 and 5, so the +1 makes it
1 to 6 */
dice1 = (rand() % 5) + 1;
dice2 = (rand() % 5) + 1;
total1 = dice1 + dice2;
printf("First roll of dice was %d and %d, ", dice1, dice2);
printf("for a total of %d.\n\n\n", total1);
do
{
puts("Do you think the next roll will be ");
puts("(H)igher, (L)ower, or (S)ame?\n");
puts("Enter H, L, or S to reflect your guess.");
scanf(" %c", &ans);
ans = toupper(ans);
} while ((ans != 'H') && (ans != 'L') && (ans != 'S'));
Step 4
The following codes will roll the dice after accepting your predictions, and give you a total
//Roll the dice a second time to get your second total
dice1 = (rand() % 5) + 1;
dice2 = (rand() % 5) + 1;
total2 = dice1 + dice2;
//Display the second total to get your second total
printf("\nThe second roll was %d and %d, ", dice1, dice2);
printf("for a total of %d.\n\n\n", total2);
Step 5
The following code will compare the first total of the dice rolled and the second total of the dice rolled, and after comparison it will then determine if the users predictions were wrong or right.
/* Compare the two dice totals against the user's guess and tell them if they
were right or not */
if (ans == 'L')
{
if (total2 < total1)
{
printf("Good job! You were right!\n");
printf("%d is lower than %d\n\n", total2, total1);
}
else
{
printf("Sorry! %d is not lower than %d\n\n", total2, total1);
}
}
else if (ans == 'H')
{
if (total2 > total1)
{
printf("Good job! You were right!\n");
printf("%d is higher than %d\n\n", total2, total1);
}
else
{
printf("Sorry! %d is not higher than %d\n\n", total2, total1);
}
}
else if (ans == 'S')
{
if (total2 == total1)
{
printf("Good job! You are right!\n");
printf("%d is the same as %d\n\n", total2, total1);
}
else
{
printf("Sorry! %d is not the same as %d\n\n", total2, total1);
}
}
Step 6
The next code its placed before the 'while' closing curly bracket loop, to confirm if the player wants to continue with the game or wants to quit. The dice game will then continue if the player click one and end if the player click zero. The return code is then placed before the closing curly bracket of the main()
printf("Click 1 to CONTINUE the game\n");
printf("Click 0 to END the game\n");
scanf(" %d", &choice);
} while (choice != 0);
return (0);
}
Conclusion
This is the end of the program. You can test your program by clicking on the yellow wrench icon with a green play button as shown in the image below:
Image 2 |
Don't forget to check back for more programming projects.