MKoD - D Programming Language

Basic Switch...Case...Default example in D - code-name switchcaseMM.d

Basic Newbie Stuff Switch...Case...Default example:

// ** switchcaseMM2.d **
// switch...case...default example in D
// 12.Oct.04 Compiled and Tested with dmd v0.102
// 28.Feb.05 Compiled and Tested with dmd v0.114
// 27.Jul.06 Lasted compiled and tested with dmd v0.163
//
// To Compile: C:\dmd\MKoD_ex>..\bin\dmd switchcaseMM2.d
private import std.stdio;
private import std.random;
private import std.date;

int main()
{
    enum : ubyte { Black=0, Red=1, Blue=2, Green=3, Brown=4, Yellow=5, Orange=6, Purple=7 }
    
    uint[ 8 ] ubytCtr = 0U; 
    ubyte     ubytRN  = 0U;
    int       ix      = 0;
     
    writefln( "Your M & M (Mars & Mars) candy drops are being made...\n" );
    
    for( ;; )
    {
        // Wanted a better range for gathering a random numbers, and so decided to use 
        // the range from 0 to 255 dividing it by 32, instead of just using 0 to 7. 
        ubytRN = getRandomNumber( 0, 255 ) / 32U;
        
        switch ( ubytRN )
        {
           case Black:  { if ( ubytCtr[ Black  ] < 13 ) { writef( "Black,  " ); ubytCtr[ Black  ]++; } else continue; break; }
           case Red:    { if ( ubytCtr[ Red    ] < 13 ) { writef( "Red,    " ); ubytCtr[ Red    ]++; } else continue; break; }
           case Blue:   { if ( ubytCtr[ Blue   ] < 13 ) { writef( "Blue,   " ); ubytCtr[ Blue   ]++; } else continue; break; }
           case Green:  { if ( ubytCtr[ Green  ] < 13 ) { writef( "Green,  " ); ubytCtr[ Green  ]++; } else continue; break; }
           case Brown:  { if ( ubytCtr[ Brown  ] < 13 ) { writef( "Brown,  " ); ubytCtr[ Brown  ]++; } else continue; break; }
           case Yellow: { if ( ubytCtr[ Yellow ] < 13 ) { writef( "Yellow, " ); ubytCtr[ Yellow ]++; } else continue; break; }
           case Orange: { if ( ubytCtr[ Orange ] < 13 ) { writef( "Orange, " ); ubytCtr[ Orange ]++; } else continue; break; }
           case Purple: { if ( ubytCtr[ Purple ] < 13 ) { writef( "Purple, " ); ubytCtr[ Purple ]++; } else continue; break; }
           default : continue;
        }
        
        ix++;
        if ( ix % 10 == 0 ) writefln();
        if ( ix == 56 ) break;
    }
    
    writefln( "\n" );
    writefln( "Your bag of 56 M & M's has the following amount in each color:" );
    writefln( "Black=%2d, Red   =%2d, Blue  =%2d, Green =%2d", 
               ubytCtr[ Black ], ubytCtr[ Red ], ubytCtr[ Blue ], ubytCtr[ Green ] );
    writefln( "Brown=%2d, Yellow=%2d, Orange=%2d, Purple=%2d",
               ubytCtr[ Brown ], ubytCtr[ Yellow ], ubytCtr[ Orange ], ubytCtr[ Purple ] );
         
    return 0;
    
} // end int main()

/+
 ' Source        : uint getRandomNumber( in uint = 0x0000, in uint = 0xFFFFFFFF )
 ' Author        : David L. 'SpottedTiger' Davis
 ' Date Created  : 12.Oct.04 Compiled and Tested with dmd v0.102
 ' Modified Date : 28.Feb.05 Compiled and Tested with dmd v0.114
 '               : 26.Jul.06 Fixed an error that appeared if getRandomNumber() 
 '               :           was called using both the default values...I only
 '               :           tested this function with ranges. Compiled and
 '               :           tested with dmd v0.163.  
 ' Requires      : std.random and std.date
 ' -----------------------------------------------------------------------------
 ' By default, returns a unsigned number between 0 to 4,294,967,295. 
 +/
uint getRandomNumber( in uint uiLowNum = 0x0000, in uint uiHighNum = 0xFFFFFFFF )
{
    d_time sysTime;
    uint   uiRandom;
    static uint uiMixNum; 
    
    if ( uiHighNum <= 0 )
        return uiLowNum;
        
    if ( uiLowNum == uiHighNum || uiLowNum > uiHighNum )
        return uiLowNum;
    
    sysTime = msFromTime( getUTCtime() );
    uiMixNum += sysTime << 5;
    
    rand_seed( uiMixNum, ( sysTime * SecFromTime( sysTime ) ) - uiMixNum );
    
    if (uiLowNum == 0x0 && uiHighNum == 0xFFFFFFFF)
        uiRandom = ( rand() % uiHighNum );
    else
        uiRandom = ( rand() % ( uiHighNum - uiLowNum + 1 ) + uiLowNum );
    
    //writefln( "uiLowNum=%06d, uiRandom=%06d, uiHighNum=%06d", 
    //           uiLowNum, uiRandom, uiHighNum );

    return uiRandom;
    
} // end uint getRandomNumber( in uint = 0x0000, in uint = 0xFFFFFFFF )
C:\dmd\MKOD_ex>..\bin\dmd switchcaseMM2.d
C:\dmd\bin\..\..\dm\bin\link.exe switchcaseMM2,,,user32+kernel32/noi;

C:\dmd\MKOD_ex>switchcaseMM2
Your M & M (Mars & Mars) candy drops are being made...

Orange, Green,  Brown,  Purple, Red,    Black,  Blue,   Purple, Green,  Orange,
Orange, Brown,  Blue,   Blue,   Yellow, Red,    Green,  Blue,   Red,    Orange,
Purple, Black,  Brown,  Brown,  Black,  Yellow, Blue,   Brown,  Green,  Purple,
Purple, Purple, Red,    Purple, Orange, Orange, Black,  Orange, Green,  Red,
Brown,  Purple, Brown,  Blue,   Blue,   Red,    Purple, Black,  Blue,   Red,
Green,  Black,  Yellow, Brown,  Green,  Orange,

Your bag of 56 M & M's has the following amount in each color:
Black= 6, Red   = 7, Blue  = 8, Green = 7
Brown= 8, Yellow= 3, Orange= 8, Purple= 9

C:\dmd\MKoD_ex>switchcaseMM2
Your M & M (Mars & Mars) candy drops are being made...

Orange, Black,  Orange, Blue,   Red,    Brown,  Black,  Red,    Green,  Red,
Green,  Brown,  Orange, Black,  Red,    Brown,  Red,    Brown,  Green,  Red,
Green,  Yellow, Yellow, Green,  Blue,   Brown,  Green,  Blue,   Orange, Blue,
Purple, Brown,  Black,  Black,  Brown,  Purple, Green,  Blue,   Red,    Red,
Yellow, Orange, Green,  Blue,   Orange, Yellow, Blue,   Yellow, Purple, Purple,
Brown,  Yellow, Black,  Blue,   Black,  Orange,

Your bag of 56 M & M's has the following amount in each color:
Black= 7, Red   = 8, Blue  = 8, Green = 8
Brown= 8, Yellow= 6, Orange= 7, Purple= 4

C:\dmd\MKoD_ex>
Mars: fourth Rock from the Sun.