Skip to content

Commit 1ec82a5

Browse files
author
Stefan van den Oord
committed
Successfully ported TennisGame1
1 parent 7e486ce commit 1ec82a5

File tree

5 files changed

+141
-56
lines changed

5 files changed

+141
-56
lines changed

Tennis/objc/.idea/workspace.xml

Lines changed: 35 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tennis/objc/Tennis/TennisGame.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
#import <Foundation/Foundation.h>
1010

1111

12-
@interface TennisGame : NSObject
12+
@interface TennisGame : NSObject {
13+
int score1;
14+
int score2;
15+
}
16+
1317
@property(nonatomic, copy) NSString *player1;
1418
@property(nonatomic, copy) NSString *player2;
1519

1620
- (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2;
17-
18-
- (void)wonPoint:(NSString *)player;
19-
21+
- (void)wonPoint:(NSString *)playerName;
2022
- (NSString *)score;
23+
2124
@end

Tennis/objc/Tennis/TennisGame.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ - (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2 {
1818
if (self) {
1919
self.player1 = player1;
2020
self.player2 = player2;
21+
score1 = 0;
22+
score2 = 0;
2123
}
2224

2325
return self;
2426
}
2527

26-
- (void)wonPoint:(NSString *)player {}
28+
- (void)wonPoint:(NSString *)playerName {}
2729
- (NSString *)score { return nil; }
30+
2831
@end

Tennis/objc/Tennis/TennisGame1.m

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,73 @@
99
#import "TennisGame1.h"
1010

1111

12-
@implementation TennisGame1 {
12+
@implementation TennisGame1
1313

14+
- (void)wonPoint:(NSString *)playerName {
15+
if ([playerName isEqualToString:@"player1"])
16+
score1 += 1;
17+
else
18+
score2 += 1;
1419
}
20+
21+
- (NSString *)score {
22+
NSString *score = @"";
23+
int tempScore=0;
24+
if (score1 == score2)
25+
{
26+
switch (score1)
27+
{
28+
case 0:
29+
score = @"Love-All";
30+
break;
31+
case 1:
32+
score = @"Fifteen-All";
33+
break;
34+
case 2:
35+
score = @"Thirty-All";
36+
break;
37+
case 3:
38+
score = @"Forty-All";
39+
break;
40+
default:
41+
score = @"Deuce";
42+
break;
43+
44+
}
45+
}
46+
else if (score1>=4 || score2>=4)
47+
{
48+
int minusResult = score1-score2;
49+
if (minusResult==1) score = @"Advantage player1";
50+
else if (minusResult ==-1) score = @"Advantage player2";
51+
else if (minusResult>=2) score = @"Win for player1";
52+
else score = @"Win for player2";
53+
}
54+
else
55+
{
56+
for (int i=1; i<3; i++)
57+
{
58+
if (i==1) tempScore = score1;
59+
else { score = [NSString stringWithFormat:@"%@-", score]; tempScore = score2; }
60+
switch(tempScore)
61+
{
62+
case 0:
63+
score = [NSString stringWithFormat:@"%@Love", score];
64+
break;
65+
case 1:
66+
score = [NSString stringWithFormat:@"%@Fifteen", score];
67+
break;
68+
case 2:
69+
score = [NSString stringWithFormat:@"%@Thirty", score];
70+
break;
71+
case 3:
72+
score = [NSString stringWithFormat:@"%@Forty", score];
73+
break;
74+
}
75+
}
76+
}
77+
return score;
78+
}
79+
80+
1581
@end

Tennis/objc/TennisTests/TennisTests.m

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright (c) 2013 Stefan van den Oord. All rights reserved.
77
//
88

9+
#import <SenTestingKit/SenTestingKit.h>
910
#import "TennisTests.h"
1011
#import "TennisGame1.h"
1112
#import "TennisGame2.h"
@@ -75,18 +76,6 @@ + (NSArray*)allScores {
7576
}
7677

7778
- (void)checkAllScoresForGame:(TennisGame *)game {
78-
79-
for (NSArray * score in [TennisTests allScores]) {
80-
81-
player1Score = [score[0] intValue];
82-
player2Score = [score[1] intValue];
83-
expectedScore = score[2];
84-
85-
[self checkScoreForGame:game];
86-
}
87-
}
88-
89-
- (void)checkScoreForGame:(TennisGame *)game {
9079
int highestScore = MAX(player1Score, player2Score);
9180
for (int i = 0; i < highestScore; i++) {
9281
if (i < player1Score)
@@ -98,18 +87,39 @@ - (void)checkScoreForGame:(TennisGame *)game {
9887
}
9988

10089
- (void)testAllScoresTennisGame1 {
101-
TennisGame1 * game = [[TennisGame1 alloc] initWithPlayer1:@"player1" player2:@"player2"];
102-
[self checkAllScoresForGame:game];
90+
for (NSArray * score in [TennisTests allScores]) {
91+
92+
player1Score = [score[0] intValue];
93+
player2Score = [score[1] intValue];
94+
expectedScore = score[2];
95+
96+
TennisGame1 * game = [[TennisGame1 alloc] initWithPlayer1:@"player1" player2:@"player2"];
97+
[self checkAllScoresForGame:game];
98+
}
10399
}
104100

105101
- (void)testAllScoresTennisGame2 {
106-
TennisGame2 * game = [[TennisGame2 alloc] initWithPlayer1:@"player1" player2:@"player2"];
107-
[self checkAllScoresForGame:game];
102+
for (NSArray * score in [TennisTests allScores]) {
103+
104+
player1Score = [score[0] intValue];
105+
player2Score = [score[1] intValue];
106+
expectedScore = score[2];
107+
108+
TennisGame2 * game = [[TennisGame2 alloc] initWithPlayer1:@"player1" player2:@"player2"];
109+
[self checkAllScoresForGame:game];
110+
}
108111
}
109112

110113
- (void)testAllScoresTennisGame3 {
111-
TennisGame3 * game = [[TennisGame3 alloc] initWithPlayer1:@"player1" player2:@"player2"];
112-
[self checkAllScoresForGame:game];
114+
for (NSArray * score in [TennisTests allScores]) {
115+
116+
player1Score = [score[0] intValue];
117+
player2Score = [score[1] intValue];
118+
expectedScore = score[2];
119+
120+
TennisGame3 * game = [[TennisGame3 alloc] initWithPlayer1:@"player1" player2:@"player2"];
121+
[self checkAllScoresForGame:game];
122+
}
113123
}
114124

115125
@end

0 commit comments

Comments
 (0)