Last active
February 9, 2016 03:14
-
-
Save paulw11/9307d1126b143c949400 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// main.m | |
// ReindeerGamesObjC | |
// | |
// Created by Paul Wilkinson on 9/02/2016. | |
// Copyright © 2016 Paul Wilkinson. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import "Reindeer.h" | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
Reindeer *comet=[[Reindeer alloc] initWithSpeed:14 endurance:10 restTime:127]; | |
Reindeer *dancer=[[Reindeer alloc] initWithSpeed:16 endurance:11 restTime:162]; | |
int time=2503; | |
int cometDistance=[comet distanceOverTime:time]; | |
int dancerDistance=[dancer distanceOverTime:time]; | |
NSString *winner=@"Dancer"; | |
if (cometDistance > dancerDistance) { | |
winner=@"Comet"; | |
} else if (cometDistance==dancerDistance) { | |
winner=@"dead heat"; | |
} | |
NSLog(@"After %d seconds, Comet distance=%dkm, Dancer distance=%dkm. The winner was: %@",time,cometDistance,dancerDistance,winner); | |
} | |
return 0; | |
} |
This file contains hidden or 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
// | |
// Reindeer.h | |
// ReindeerGamesObjC | |
// | |
// Created by Paul Wilkinson on 9/02/2016. | |
// Copyright © 2016 Paul Wilkinson. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface Reindeer : NSObject | |
@property int endurance; | |
@property int speed; | |
@property int restTime; | |
-(id)initWithSpeed:(int)speed endurance:(int)endurance restTime:(int)restTime; | |
-(int)distanceOverTime:(int)duration; | |
@end |
This file contains hidden or 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
// | |
// Reindeer.m | |
// ReindeerGamesObjC | |
// | |
// Created by Paul Wilkinson on 9/02/2016. | |
// Copyright © 2016 Paul Wilkinson. All rights reserved. | |
// | |
#import "Reindeer.h" | |
@implementation Reindeer | |
-(id)initWithSpeed:(int)speed endurance:(int)endurance restTime:(int)restTime { | |
if (self=[super init]) { | |
self.speed=speed; | |
self.endurance=endurance; | |
self.restTime=restTime; | |
} | |
return self; | |
} | |
-(int) distanceOverTime:(int)duration { | |
int phase=0; | |
int distanceTravelled=0; | |
for (int i=1;i<duration;i++){ | |
if (phase < self.endurance) { | |
distanceTravelled+=self.speed; | |
} else if (phase > self.restTime+self.endurance) { | |
phase = -1; | |
} | |
phase++; | |
} | |
return distanceTravelled; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment