| /*******************************************/ | /*******************************************/ | ||||
| @implementation SA_DiceBag { | @implementation SA_DiceBag { | ||||
| GKRandomSource *_randomSource; | |||||
| // GKRandomSource *_randomSource; | |||||
| // NSMutableDictionary <NSNumber *, GKRandomDistribution *> *_dice; | |||||
| NSMutableDictionary <NSNumber *, GKRandomDistribution *> *_dice; | |||||
| } | } | ||||
| -(instancetype) init { | -(instancetype) init { | ||||
| self = [super init]; | |||||
| if (!self) return nil; | |||||
| if (!(self = [super init])) | |||||
| return nil; | |||||
| _randomSource = [GKMersenneTwisterRandomSource new]; | |||||
| // _randomSource = [GKMersenneTwisterRandomSource new]; | |||||
| // _dice = [NSMutableDictionary dictionary]; | |||||
| _dice = [NSMutableDictionary dictionary]; | |||||
| return self; | return self; | ||||
| } | } | ||||
| } | } | ||||
| -(NSUInteger) rollDie:(NSUInteger)dieSize { | -(NSUInteger) rollDie:(NSUInteger)dieSize { | ||||
| return [_randomSource nextIntWithUpperBound:dieSize] + 1; | |||||
| // return [[self dieOfSize:dieSize] nextInt]; | |||||
| // return [_randomSource nextIntWithUpperBound:dieSize] + 1; | |||||
| return [[self dieOfSize:dieSize] nextInt]; | |||||
| } | } | ||||
| -(NSArray <NSNumber *> *) rollNumber:(NSUInteger)number | -(NSArray <NSNumber *> *) rollNumber:(NSUInteger)number | ||||
| do { | do { | ||||
| dieRoll = [self rollDie:dieSize]; | dieRoll = [self rollDie:dieSize]; | ||||
| [rollsArray addObject:@(dieRoll)]; | [rollsArray addObject:@(dieRoll)]; | ||||
| } while ((options & SA_DiceRollingExplodingDice) | |||||
| } while ( options & SA_DiceRollingExplodingDice | |||||
| && dieSize > 1 | && dieSize > 1 | ||||
| && dieRoll == dieSize); | && dieRoll == dieSize); | ||||
| } | } | ||||
| #pragma mark - Helper methods | #pragma mark - Helper methods | ||||
| /****************************/ | /****************************/ | ||||
| //-(GKRandomDistribution *) dieOfSize:(NSUInteger) dieSize { | |||||
| // if (_dice[@(dieSize)] == nil) | |||||
| // _dice[@(dieSize)] = [GKRandomDistribution distributionForDieWithSideCount:dieSize]; | |||||
| // | |||||
| // return _dice[@(dieSize)]; | |||||
| //} | |||||
| -(GKRandomDistribution *) dieOfSize:(NSUInteger) dieSize { | |||||
| if (_dice[@(dieSize)] == nil) | |||||
| _dice[@(dieSize)] = [[GKRandomDistribution alloc] initWithRandomSource:[GKMersenneTwisterRandomSource new] | |||||
| lowestValue:1 | |||||
| highestValue:dieSize]; | |||||
| return _dice[@(dieSize)]; | |||||
| } | |||||
| @end | @end |