// Dice Checking app version 2
// Marc Chee June 2019

// This app will help a user with dice rolling
// It will ask for the size of the dice
// Then it will take two dice worth of input,
// verifying for correctness.
// If incorrect, it will use modulus to correct
// the range.

// Then it will add the two dice together
// and check them against the secret number
// reporting back success, tie or failure.

#include <stdio.h>

#define SECRET_NUMBER 7

int main (void) {
    int diceSize;
    int die1;
    int die2;
    int total;
    
    // Ask the user how large the dice are
    printf("Please enter the number of sides on your dice: ");
    scanf("%d", &diceSize);
    
    // read two dice values from the user
    printf("Please enter the first die roll: ");
    scanf("%d", &die1);
    
    if (die1 < 1 || diceSize < die1) {
        // die1 is outside the legal range
        printf("The first die roll, %d is outside the legal range 1-%d.\n", die1, diceSize); 
        
        // Use mod to convert any number into a 1-size
        die1 = die1 % diceSize;
        if (die1 == 0) {
            die1 = diceSize;
        }
    }    
    
/*    
    // This code was used to clamp values between 1 and diceSize
    // It was replaced by modulus code
    
    // clamp values between 1-size
    if (die1 < 1) {
        die1 = 1;
    }
    if (die1 > diceSize) {
        die1 = diceSize;
    }
*/
    
    printf("Please enter the second die roll: ");
    scanf("%d", &die2);

    if (die2 < 1 || diceSize < die2) {
        // die2 is outside the legal range
        printf("The second die roll, %d is outside the legal range 1-%d.\n", die2, diceSize); 
        
        // Use mod to convert any number into a 1-size
        die2 = die2 % diceSize;
        if (die2 == 0) {
            die2 = diceSize;
        }
    }
    
    // total the dice
    total = die1 + die2;
    printf("You rolled %d\n", total);
    
    // Check the total against the target
    if (total > SECRET_NUMBER) {
        // success
        printf("Dice check succeeded.\n");
    } else if (total == SECRET_NUMBER) {
        // tie
        printf("Dice check tied.\n");
    } else {
        // fail
        printf("Dice check failed.\n");
    }
    
    return 0;
}

Resource created Tuesday 11 June 2019, 04:30:06 PM.

file: diceCheck2.c


Back to top

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