From 99a58611258ba61139f8f10c0ead94814e5feae5 Mon Sep 17 00:00:00 2001 From: Stefan van den Oord Date: Fri, 31 May 2013 19:41:01 +0200 Subject: [PATCH] Also ported TennisGame3. Furthermore pushed instance vars down to better match spirit of Java implementation --- Tennis/objc/.idea/workspace.xml | 95 +++++++++++++++++--------------- Tennis/objc/Tennis/TennisGame.h | 8 +-- Tennis/objc/Tennis/TennisGame.m | 14 +---- Tennis/objc/Tennis/TennisGame1.h | 2 + Tennis/objc/Tennis/TennisGame1.m | 17 +++++- Tennis/objc/Tennis/TennisGame2.m | 16 ++++++ Tennis/objc/Tennis/TennisGame3.m | 43 ++++++++++++++- 7 files changed, 128 insertions(+), 67 deletions(-) diff --git a/Tennis/objc/.idea/workspace.xml b/Tennis/objc/.idea/workspace.xml index 597337eb..2aef8bcb 100644 --- a/Tennis/objc/.idea/workspace.xml +++ b/Tennis/objc/.idea/workspace.xml @@ -2,7 +2,12 @@ + + + + + @@ -14,11 +19,11 @@ - - + + - + - + - + @@ -59,7 +64,7 @@ - + @@ -67,10 +72,10 @@ - + - + @@ -97,7 +102,7 @@ - + @@ -106,7 +111,7 @@ - + @@ -115,7 +120,7 @@ - + @@ -124,7 +129,7 @@ - + @@ -148,16 +153,16 @@ @@ -398,9 +403,30 @@ - + - + + + + + + + + + + + + + + + + + + + + + + @@ -412,53 +438,32 @@ - + - - - - - - - - - - - - - - - - - - - - - - + - + - + - + - + - + diff --git a/Tennis/objc/Tennis/TennisGame.h b/Tennis/objc/Tennis/TennisGame.h index 38a5ceed..bca6994b 100644 --- a/Tennis/objc/Tennis/TennisGame.h +++ b/Tennis/objc/Tennis/TennisGame.h @@ -9,13 +9,7 @@ #import -@interface TennisGame : NSObject { - int score1; - int score2; -} - -@property(nonatomic, copy) NSString *player1; -@property(nonatomic, copy) NSString *player2; +@interface TennisGame : NSObject - (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2; - (void)wonPoint:(NSString *)playerName; diff --git a/Tennis/objc/Tennis/TennisGame.m b/Tennis/objc/Tennis/TennisGame.m index 64d1e82a..7a94623f 100644 --- a/Tennis/objc/Tennis/TennisGame.m +++ b/Tennis/objc/Tennis/TennisGame.m @@ -12,19 +12,7 @@ @implementation TennisGame - -- (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2 { - self = [super init]; - if (self) { - self.player1 = player1; - self.player2 = player2; - score1 = 0; - score2 = 0; - } - - return self; -} - +- (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2 { return [super init]; } - (void)wonPoint:(NSString *)playerName {} - (NSString *)score { return nil; } diff --git a/Tennis/objc/Tennis/TennisGame1.h b/Tennis/objc/Tennis/TennisGame1.h index d3dd979f..43cc7e31 100644 --- a/Tennis/objc/Tennis/TennisGame1.h +++ b/Tennis/objc/Tennis/TennisGame1.h @@ -11,4 +11,6 @@ @interface TennisGame1 : TennisGame +@property(nonatomic, copy) NSString *player1; +@property(nonatomic, copy) NSString *player2; @end \ No newline at end of file diff --git a/Tennis/objc/Tennis/TennisGame1.m b/Tennis/objc/Tennis/TennisGame1.m index 5f53e117..74965708 100644 --- a/Tennis/objc/Tennis/TennisGame1.m +++ b/Tennis/objc/Tennis/TennisGame1.m @@ -9,7 +9,22 @@ #import "TennisGame1.h" -@implementation TennisGame1 +@implementation TennisGame1 { + int score1; + int score2; +} + +- (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2 { + self = [super init]; + if (self) { + self.player1 = player1; + self.player2 = player2; + score1 = 0; + score2 = 0; + } + + return self; +} - (void)wonPoint:(NSString *)playerName { if ([playerName isEqualToString:@"player1"]) diff --git a/Tennis/objc/Tennis/TennisGame2.m b/Tennis/objc/Tennis/TennisGame2.m index e08b02ef..66112b94 100644 --- a/Tennis/objc/Tennis/TennisGame2.m +++ b/Tennis/objc/Tennis/TennisGame2.m @@ -9,6 +9,11 @@ #import "TennisGame2.h" +@interface TennisGame2 () +@property(nonatomic, copy) NSString *player2Name; +@property(nonatomic, copy) NSString *player1Name; +@end + @implementation TennisGame2 { int P1point; NSString *P1res; @@ -16,6 +21,17 @@ NSString *P2res; } +- (id)initWithPlayer1:(NSString *)player1Name player2:(NSString *)player2Name { + self = [super initWithPlayer1:player1Name player2:player2Name]; + if (self) { + self.player1Name = player1Name; + self.player2Name = player2Name; + } + + return self; +} + + - (NSString *)score { NSString *score = @""; if (P1point == P2point && P1point < 4) diff --git a/Tennis/objc/Tennis/TennisGame3.m b/Tennis/objc/Tennis/TennisGame3.m index b2a8a869..bf12e6c2 100644 --- a/Tennis/objc/Tennis/TennisGame3.m +++ b/Tennis/objc/Tennis/TennisGame3.m @@ -9,6 +9,47 @@ #import "TennisGame3.h" -@implementation TennisGame3 +@interface TennisGame3 () +@end + +@implementation TennisGame3 { + int p1; + int p2; + NSString *p1N; + NSString *p2N; +} + +- (id)initWithPlayer1:(NSString *)p1N player2:(NSString *)p2N { + self = [super initWithPlayer1:p1N player2:p2N]; + if (self) { + self->p1N = p1N; + self->p2N = p2N; + p1 = 0; + p2 = 0; + } + return self; +} + +- (NSString *)score { + NSString *s; + if (p1 < 4 && p2 < 4) { + NSArray* p = @[@"Love", @"Fifteen", @"Thirty", @"Forty"]; + s = p[p1]; + return (p1 == p2) ? [NSString stringWithFormat:@"%@-All",s] : [NSString stringWithFormat:@"%@-%@",s,p[p2]]; + } else { + if (p1 == p2) + return @"Deuce"; + s = p1 > p2 ? p1N : p2N; + return ((p1-p2)*(p1-p2) == 1) ? [NSString stringWithFormat:@"Advantage %@",s] : [NSString stringWithFormat:@"Win for %@",s]; + } +} + +- (void)wonPoint:(NSString *)playerName { + if ([playerName isEqualToString:@"player1"]) + p1 += 1; + else + p2 += 1; +} + @end \ No newline at end of file