//
// DieRoller
//
// Copyright (C) 1992 Contemporary Design Studios. All rights reserved.
//


#import "DieRoller.h"


@implementation DieRoller


//
//rollDie:
//

- (int)rollDie:(int) numSides
{
    return [self randMax:(numSides - 1)] + 1;
}


//
// roll:die:
//

- (int)roll:(int) numRolls die:(int) numSides
{
    int temp = 0;
    int loop;
	
    for (loop = 1 ; loop <= numRolls ; loop++ )
	temp += [self rollDie:numSides];
	
    return temp;
}


//
// rollBest:of:die:
//

- (int) rollBest:(int)numWanted of:(int)numRolls die:(int)numSides
{
    int temp[numRolls];				// Array of rolls
    int loop1;					// First loop control variable
    int loop2;					// Second loop control variable
    int highest;				// Index of highest found roll
    int accumulator = 0;			// Accumulates total best roll
	
    for (loop1 = 1 ; loop1 <= numRolls ; loop1++)	// Fill an array with rolls
	temp[loop1] = [self rollDie:numSides];
	
    for (loop1 = 1 ; loop1 <= numWanted; loop1++) {
	highest = 1;				// Start off as if first is highest
	for (loop2 = 2 ; loop2 <= numRolls ; loop2++)	// Scan array for higher rolls
	    if (temp[loop2] > temp[highest])	// If temp[loop2] is higher, then
		highest = loop2;		//     remember that fact
	accumulator += temp[highest];		// Add highest roll to accumulator
	temp[highest] = 0;			// Clear highest roll so we don't find it again
    }
	
    return accumulator;				// Return what we found
}


@end


//
// End of file.
//