Ajout début partie 2 (buggé)

This commit is contained in:
rick 2021-03-26 16:53:45 +01:00
parent 7bff779701
commit 8a8af36570
Signed by: Rick
GPG key ID: 2B593F087240EE99
5 changed files with 128 additions and 4 deletions

View file

@ -1,4 +1,5 @@
#import "raindeer.h" #import "raindeer.h"
#import "troupeau.h"
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
int main() int main()
@ -12,6 +13,7 @@ int main()
error:&err]; error:&err];
NSArray *lines = [contentFile componentsSeparatedByString:@"\n"]; NSArray *lines = [contentFile componentsSeparatedByString:@"\n"];
NSMutableArray *listDeer = [[NSMutableArray alloc] init]; NSMutableArray *listDeer = [[NSMutableArray alloc] init];
Troupeau *troupeau = [[Troupeau alloc] init];
for (NSString *tmp in lines) for (NSString *tmp in lines)
{ {
@ -22,10 +24,11 @@ int main()
zatSpeed:[[parsedSpace objectAtIndex:3] integerValue] zatSpeed:[[parsedSpace objectAtIndex:3] integerValue]
boostTime:[[parsedSpace objectAtIndex:6] integerValue] boostTime:[[parsedSpace objectAtIndex:6] integerValue]
LunchTime:[[parsedSpace objectAtIndex:13] integerValue]]; LunchTime:[[parsedSpace objectAtIndex:13] integerValue]];
[listDeer addObject:rd]; [troupeau newDeer:rd];
} }
} }
/*
int ret = 0; int ret = 0;
int tmp = 0; int tmp = 0;
for (Raindeer *rd in listDeer) for (Raindeer *rd in listDeer)
@ -35,7 +38,9 @@ int main()
ret = tmp; ret = tmp;
[rd release]; [rd release];
} }
NSLog(@"le résultat est : %d", ret); */
NSLog(@"le résultat est : %d", [troupeau courseUne:1000]);
NSLog(@"le résultat est : %d", [troupeau courseDeux:1000]);
//finir le main par ça //finir le main par ça
[contentFile release]; [contentFile release];

View file

@ -6,14 +6,21 @@
int speed; int speed;
int boostTime; int boostTime;
int pause; int pause;
int points;
int cooldown;
int hypervitesse;
int distance;
} }
// permet de générer des setters et getter selon des attributs // permet de générer des setters et getter selon des attributs
// ici retain, nonatomic et assign // ici retain, nonatomic et assign
@property (retain, nonatomic) NSString *name; @property (retain, nonatomic) NSString *name;
@property (assign) int speed, boostTime, pause; @property (assign) int speed, boostTime, pause, points, cooldown, hypervitesse, distance;
+ (id) initWithName:(NSString*)name zatSpeed:(int)speed boostTime:(int)timeSpeed LunchTime:(int)pause; + (id) initWithName:(NSString*)name zatSpeed:(int)speed boostTime:(int)timeSpeed LunchTime:(int)pause;
- (int) calculDistance:(int)time; - (int) calculDistance:(int)time;
- (int) updateDistance;
- (void) updatePoints;
@end @end

View file

@ -4,7 +4,7 @@
@implementation Raindeer @implementation Raindeer
// permet de créer les getters et setters à partir de property // permet de créer les getters et setters à partir de property
@synthesize name, speed, boostTime, pause; @synthesize name, speed, boostTime, pause, points, cooldown, hypervitesse, distance;
+ (id) initWithName:(NSString*)name zatSpeed:(int)speed boostTime:(int)boostTime LunchTime:(int)pause + (id) initWithName:(NSString*)name zatSpeed:(int)speed boostTime:(int)boostTime LunchTime:(int)pause
{ {
@ -15,6 +15,10 @@
new.speed = speed; new.speed = speed;
new.boostTime = boostTime; new.boostTime = boostTime;
new.pause = pause; new.pause = pause;
new.points = 0;
new.cooldown = 0;
new.hypervitesse = boostTime;
new.distance = 0;
} }
return new; return new;
} }
@ -31,6 +35,28 @@
return ret; return ret;
} }
- (int) updateDistance
{
if (cooldown > 0)
cooldown--;
if (cooldown == 0)
hypervitesse = boostTime;
else
{
distance += speed;
hypervitesse--;
if (hypervitesse == 0)
cooldown = pause;
}
return distance;
}
- (void) updatePoints
{
points++;
}
- (void) dealloc - (void) dealloc
{ {
[name release]; [name release];

15
jour14/troupeau.h Normal file
View file

@ -0,0 +1,15 @@
#import "raindeer.h"
#import <Foundation/Foundation.h>
@interface Troupeau : NSObject
{
NSMutableArray *troupeau;
NSSortDescriptor *recetteTri;
}
- (id) init;
- (void) newDeer:(Raindeer*)rd;
- (int) courseUne:(int)time;
- (int) courseDeux:(int)time;
@end

71
jour14/troupeau.m Normal file
View file

@ -0,0 +1,71 @@
#import "troupeau.h"
#import "raindeer.h"
@implementation Troupeau
- (id) init
{
self = [super init];
if (self)
{
recetteTri = [[NSSortDescriptor alloc] initWithKey:@"distance"
ascending:YES];
troupeau = [[NSMutableArray alloc] init];
}
return self;
}
- (void) newDeer:(Raindeer*)rd
{
[troupeau addObject:rd];
}
- (int) courseUne:(int)time
{
int ret = 0, tmp = 0;
for (Raindeer *rd in troupeau)
{
tmp = [rd calculDistance:time];
if (tmp > ret)
ret = tmp;
}
return ret;
}
- (int) courseDeux:(int)time
{
int ret = 0;
while (time > 0)
{
for (Raindeer *rd in troupeau)
{
int t = [rd updateDistance];
}
NSArray *tmp = [NSArray arrayWithObject:recetteTri];
NSArray *sortedRank = [troupeau sortedArrayUsingDescriptors:tmp];
[[sortedRank firstObject] updatePoints];
NSLog(@"%d", [[sortedRank firstObject] points]);
NSLog(@"Rennes : %@ pour une distance de %d", [[troupeau firstObject] name], [[troupeau firstObject] distance]);
// copier dans le troupeau le tableau trié
time--;
}
NSSortDescriptor *tt = [[NSSortDescriptor alloc] initWithKey:@"points"
ascending:YES];
NSArray *tmpp = [NSArray arrayWithObject:tt];
NSArray *sortedPonits = [troupeau sortedArrayUsingDescriptors:tmpp];
NSLog(@"%d", [[sortedPonits firstObject] points]);
ret = [[sortedPonits firstObject] points];
return ret;
}
- (void) dealloc
{
for (Raindeer *rd in troupeau)
[rd dealloc];
[troupeau release];
[recetteTri release];
[super dealloc];
}
@end