Created
April 23, 2013 09:06
-
-
Save C4Examples/5441999 to your computer and use it in GitHub Desktop.
Advanced Examples probability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// C4WorkSpace.m | |
// Examples | |
// | |
// Created by Greg Debicki. | |
// | |
@implementation C4WorkSpace { | |
C4Shape *circle; | |
CGFloat red, blue, grey; | |
NSInteger rc, bc, gc; | |
C4Label *rl, *bl, *gl; | |
} | |
-(void)setup { | |
// percent chance of each color being chosen (total must equal 100) | |
red = 30.0f; | |
blue = 55.0f; | |
grey = 15.0f; | |
[self setupCircle]; | |
[self setupLabels]; | |
[self runMethod:@"changeColors" afterDelay:1.0f]; | |
[self.canvas addShape:circle]; | |
} | |
-(void) changeColors { | |
[self weightedProbability]; | |
[self updateLabels]; | |
circle.animationDuration = 0.0f; | |
circle.strokeEnd = 0.0f; | |
circle.animationDuration = 1.3f; | |
circle.strokeEnd = 1.0f; | |
[self runMethod:@"changeColors" afterDelay:1.5f]; | |
} | |
-(void)weightedProbability { | |
CGFloat chance = [C4Math randomInt:100]; | |
if (chance <= red) { | |
circle.fillColor = C4RED; | |
rc++; | |
} | |
if (chance > red && chance <= red + blue){ | |
circle.fillColor = C4BLUE; | |
bc++; | |
} | |
if (chance > red + blue && chance <= red + blue + grey){ | |
circle.fillColor = C4GREY; | |
gc++; | |
} | |
} | |
-(void) setupCircle { | |
circle = [C4Shape ellipse:CGRectMake(0, 0, 200, 200)]; | |
circle.center = self.canvas.center; | |
circle.animationOptions = LINEAR; | |
circle.strokeColor = [UIColor blackColor]; | |
circle.rotation -= PI/2; | |
circle.lineWidth = 10.0f; | |
circle.fillColor = [UIColor clearColor]; | |
} | |
-(void) setupLabels { | |
[self updateLabels]; | |
NSString *rs = [NSString stringWithFormat:@"Red has been chosen %3i times", rc]; | |
NSString *bs = [NSString stringWithFormat:@"Blue has been chosen %3i times", bc]; | |
NSString *gs = [NSString stringWithFormat:@"Grey has been chosen %3i times", gc]; | |
rl = [C4Label labelWithText:rs]; | |
bl = [C4Label labelWithText:bs]; | |
gl = [C4Label labelWithText:gs]; | |
CGPoint labelCenter = self.canvas.center; | |
labelCenter.y += 150; | |
rl.center = labelCenter; | |
labelCenter.y += 30; | |
bl.center = labelCenter; | |
labelCenter.y += 30; | |
gl.center = labelCenter; | |
[self.canvas addObjects:@[rl, bl, gl]]; | |
} | |
-(void) updateLabels { | |
NSString *rs = [NSString stringWithFormat:@"Red has been chosen %i times", rc]; | |
NSString *bs = [NSString stringWithFormat:@"Blue has been chosen %i times", bc]; | |
NSString *gs = [NSString stringWithFormat:@"Grey has been chosen %i times", gc]; | |
rl.text = rs; | |
bl.text = bs; | |
gl.text = gs; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment