Order Now
C Programming Task on Dice Game

C Programming Task on Dice Game

  • 2nd Jun, 2022
  • 23:29 PM
#include //preprocessor command to include header file stdio.h
#include //preprocessor command to include header file stdlib.h
#include //preprocessor command to include header file time.h

int RULES=1; // initialize global variable RULES=1
int GAME=2;// initialize global variable GAME=2
int EXIT=3;// initialize global variable EXIT=3
int ROLLS=4;// initialize global variable ROLLS=4

int displayGameMenu();// function declaration function name : displayGameMenu
void displayRandomDice();// function declaration function name : displayRandomDice
int rollDie();// function declaration function name : rollDie

void gameRules();// function declaration function name : gameRules, return type : void, parameter : no parameters passed
void clearScreen();// function declaration function name : clearScreen, return type : void, parameter : no parameters passed

int main()// main function with return type int

    int play=1;//initialize play=1
    srand(time(0));//call seed function
    while (play==1)//check whether play is equal to 1 or not
        int disp=displayGameMenu();// call displayGameMenu function
        switch(disp)//switch case
        case 1:
            gameRules();//call gameRules function
        case 2:
            clearScreen();//call clearScreen function
            displayRandomDice();//call displayRandomDice function
        case 3:
            printf("\nThank you for playing!");
            play=0;// set play=0
            printf("\nIncorrect option, hit enter and try again");
            char enter;

            fflush(stdin);//flush the input

    return 0;// return type of main function that is int

void displayRandomDice()
    int numberOfRolls;//declare numberOfRolls
    int firstDie;//declare firstDie
    int secondDie;//declare secondDie
    int thirdDie;//declare thirdDie
    int fourthDie;//declare fourthDie
    int fifthDie;//declare fifthDie
    for(int i=0;i     {
        firstDie=rollDie();//call function rollDie() & store it's value in firstDie
        secondDie=rollDie();//call function rollDie() & store it's value in secondDie
        thirdDie=rollDie();//call function rollDie() & store it's value in thirdDie
        fourthDie=rollDie();//call function rollDie() & store it's value in fourthDie
        fifthDie=rollDie();//call function rollDie() & store it's value in fifthDie
        printf("|         |          |          |       |        |\n");
        printf("|     %d   |     %d    |     %d    |   %d   |    %d   |\n",firstDie,secondDie,thirdDie,fourthDie,fifthDie);
        printf("|         |          |          |       |        |\n");

int displayGameMenu()
    int sheet=0;//initialize sheet =0
        printf("\n%d. Display game rules",RULES);
        printf("\n%d. Start the game of yahtzee",GAME);
        printf("\n%d. Exit\n",EXIT);
        scanf("%d",&sheet);//get the user input
    }while(sheet<0 || sheet>4);//check whether user enter b/w 1 - 3 or not, if it's not repeat the loop.
    return sheet;//return value of sheet

int rollDie()
    int dieValue=0;//initialize dieValue =0
    dieValue=1+(rand()%6);//generate random number between 1 and 6
    return dieValue;//return dievalue
void gameRules()// function definition of gameRules()
    // print the rules of the game using a series of printf() statements with \n used for new lines
    printf("\nRULES OF THE CAME:");
    printf("\n\t1. The scorecard used for Yahtlee is composed of an upper section and a lower section.");
    printf("\n\t2. A total of 13 scoring combinations are divided amongst the sections.");
    printf("\n\t3. The upper action consists of boxes that are scored by summing the value of tho dice notching the faces of t box.");
    printf("\n\t4. If a player rolls four 3's, then the score placed in the 5's box is the sum of the dice which is 12.");
    printf("\n\t5. Once a player has chosen to score a box, it may not be changed and the combination is no longer in play for uture rounds.");
    printf("\n\t6. If the sun of the scores in the upper section is greater than or equal to 63, then 35 more points are added to the players overall score as a bonus. The lower section contains a number of poker like combinations.");

void clearScreen()// function definition of clearScreen()
    printf("\n          Hit To Continue!        ");// this statement is used to display the text on the screen
    char c;// declaring a variable of type character
    scanf("%c",&c);// storing the input given by user in c
    system("cls");// calling function system with "cls" passed as argument as it clears the screen

Share this post

assignment helpassignment helperassignment expertsassignment writing services