From 8a8af365709122fa63c7e50c746ecba7580dbea3 Mon Sep 17 00:00:00 2001
From: rick <rick@gnous.eu>
Date: Fri, 26 Mar 2021 16:53:45 +0100
Subject: [PATCH] =?UTF-8?q?Ajout=20d=C3=A9but=20partie=202=20(bugg=C3=A9)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
jour14/jour14.m | 9 ++++--
jour14/raindeer.h | 9 +++++-
jour14/raindeer.m | 28 ++++++++++++++++++-
jour14/troupeau.h | 15 ++++++++++
jour14/troupeau.m | 71 +++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 128 insertions(+), 4 deletions(-)
create mode 100644 jour14/troupeau.h
create mode 100644 jour14/troupeau.m
diff --git a/jour14/jour14.m b/jour14/jour14.m
index a2d52c1..3d186ee 100644
--- a/jour14/jour14.m
+++ b/jour14/jour14.m
@@ -1,4 +1,5 @@
#import "raindeer.h"
+#import "troupeau.h"
#import <Foundation/Foundation.h>
int main()
@@ -12,6 +13,7 @@ int main()
error:&err];
NSArray *lines = [contentFile componentsSeparatedByString:@"\n"];
NSMutableArray *listDeer = [[NSMutableArray alloc] init];
+ Troupeau *troupeau = [[Troupeau alloc] init];
for (NSString *tmp in lines)
{
@@ -22,10 +24,11 @@ int main()
zatSpeed:[[parsedSpace objectAtIndex:3] integerValue]
boostTime:[[parsedSpace objectAtIndex:6] integerValue]
LunchTime:[[parsedSpace objectAtIndex:13] integerValue]];
- [listDeer addObject:rd];
+ [troupeau newDeer:rd];
}
}
+ /*
int ret = 0;
int tmp = 0;
for (Raindeer *rd in listDeer)
@@ -35,7 +38,9 @@ int main()
ret = tmp;
[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
[contentFile release];
diff --git a/jour14/raindeer.h b/jour14/raindeer.h
index 103b76c..3833b74 100644
--- a/jour14/raindeer.h
+++ b/jour14/raindeer.h
@@ -6,14 +6,21 @@
int speed;
int boostTime;
int pause;
+ int points;
+
+ int cooldown;
+ int hypervitesse;
+ int distance;
}
// permet de générer des setters et getter selon des attributs
// ici retain, nonatomic et assign
@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;
- (int) calculDistance:(int)time;
+- (int) updateDistance;
+- (void) updatePoints;
@end
diff --git a/jour14/raindeer.m b/jour14/raindeer.m
index 177475f..7de27f8 100644
--- a/jour14/raindeer.m
+++ b/jour14/raindeer.m
@@ -4,7 +4,7 @@
@implementation Raindeer
// 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
{
@@ -15,6 +15,10 @@
new.speed = speed;
new.boostTime = boostTime;
new.pause = pause;
+ new.points = 0;
+ new.cooldown = 0;
+ new.hypervitesse = boostTime;
+ new.distance = 0;
}
return new;
}
@@ -31,6 +35,28 @@
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
{
[name release];
diff --git a/jour14/troupeau.h b/jour14/troupeau.h
new file mode 100644
index 0000000..e25bd9d
--- /dev/null
+++ b/jour14/troupeau.h
@@ -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
diff --git a/jour14/troupeau.m b/jour14/troupeau.m
new file mode 100644
index 0000000..684f09a
--- /dev/null
+++ b/jour14/troupeau.m
@@ -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