// Upgraded Dice Checker
// Marc Chee 26/2/2019

// This app allows the user to:
// Decide the size of the dice being rolled

// It will then correct any rolls outside the range
// It will total the values of two dice
// It will report back success, tie or failure

#include <stdio.h>

// target value for dice check
#define TARGET_VALUE 7

int main (void) {
    int diceSize;       // number of sides on the dice
    int dieOne;         // value of first die
    int dieTwo;         // value of second die
    int total;          // sum of both dice
    
    // read the user's dice size
    printf("Please enter how many sides are on your dice: ");
    scanf("%d", &diceSize);
    
    // read and possibly correct the first die
    printf("Please enter the value of the first die: ");
    scanf("%d", &dieOne);
    if (dieOne < 1 || dieOne > diceSize) {
        printf("Die roll value: %d is outside of the range 1 - %d.\n", dieOne, diceSize);
        dieOne = dieOne % diceSize;
        if (dieOne == 0) {
            dieOne = diceSize;
        }
    }
    printf ("Your roll is: %d\n", dieOne);

    // read and possibly correct the second die
    printf("Please enter the value of the second die: ");
    scanf("%d", &dieTwo);
    if (dieTwo < 1 || dieTwo > diceSize) {
        printf("Die roll value: %d is outside of the range 1 - %d.\n", dieTwo, diceSize);
        dieTwo = dieTwo % diceSize;
        if (dieTwo == 0) {
            dieTwo = diceSize;
        }
    }
    printf ("Your roll is: %d\n", dieTwo);

    // total the values
    total = dieOne + dieTwo;
    printf("Total roll is: %d\n", total);
    
    // check for success
    if (total > TARGET_VALUE) {
        // success
        printf("Dice Check Succeeded!\n");
    } else if (total == TARGET_VALUE) {
        // tie
        printf("Dice Check Tied.\n");
    } else {
        // fail (don't need an if, because we've checked the other options already)
        printf("Dice Check Failed!\n");
    }

    return 0;
}

Resource created Wednesday 27 February 2019, 01:04:51 AM.

file: diceRollLecture3.c


Back to top

COMP1511 19T1 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G