// Dice Checker
// Take two dice worth of input from a user
// add them together and check them against
// a target number.
// Report back success or failure.

// Lecture demo by Marc Chee, September 2020
// (cs1511@cse.unsw.edu.au)

#include <stdio.h>

#define TARGET_VALUE 7

#define MIN_VALUE 1
#define MAX_VALUE 6

int main(void) {
    // take input from user about the two dice
    int dieOne;
    int dieTwo;
    
    // Ask for and read input about dieOne
    printf("Please type in the value of the first die: ");
    scanf("%d", &dieOne);
    
    // Check whether this input is in the valid range
    if (dieOne < MIN_VALUE || dieOne > MAX_VALUE) {
        // die roll was invalid
        printf(
            "Die One, %d was outside the range of %d to %d. ", 
            dieOne, MIN_VALUE, MAX_VALUE 
        );
        
        // Code for rejecting input and exiting
        //printf("Program will exit now.\n");
        //return 1;
        
        printf("Let me correct your value.\n");
        
        /*
        // Clamping Value Correction
        if (dieOne < MIN_VALUE) {
            dieOne = MIN_VALUE;
        } else if (dieOne > MAX_VALUE) {
            dieOne = MAX_VALUE;
        }
        */
        
        // Modulus Value Correction
        dieOne = (dieOne % MAX_VALUE);
        if (dieOne <= 0) {
            // dieOne was negative, make it positive
            dieOne = dieOne + MAX_VALUE;
        }
    } 

    // Ask for and read input about dieTwo
    printf("Please type in the value of the second die: ");
    scanf("%d", &dieTwo);
    
    // DieTwo hasn't been checked for valid input
    // See if you can replicate the behaviour of your chosen solution
    // for dieOne here!
    
    // Calculate and report Total
    int total = dieOne + dieTwo;
    printf("Total rolled is: %d\n", total);
        
    // Check against the secret target value
    if (total >= TARGET_VALUE) {
        // success
        printf("Dice check was successful!\n");
    } else {
        // failure
        printf("Dice check failed . . .\n");
    }
    
    return 0;
}

Resource created Tuesday 22 September 2020, 01:32:34 PM.

file: diceCheck.c


Back to top

COMP1511 20T3 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G