Merge pull request #18 from svdo/master

Objective-C version of Tennis kata
This commit is contained in:
Emily Bache 2013-05-31 23:35:50 -07:00
commit b31a5436cc
15 changed files with 829 additions and 1 deletions

View File

@ -6,7 +6,7 @@ This is a collection of starting files for when practicing refactoring.
Contents so far:
- Tennis Kata in Java, Python and C++.
- Tennis Kata in Java, Python, Objective-C and C++.
- Gilded Rose Kata in C++, C#, Java, Python, Smalltalk, C and Ruby. (a C# version together with a starting project is already available on github [here](https://github.com/NotMyself/GildedRose))
- Yahtzee Kata in C#, Java, C++ and Python (the C#, C++ and Java versions are also available in Jon Jagger's Cyberdojo, see [his blog post](http://jonjagger.blogspot.co.uk/2012/05/yahtzee-cyber-dojo-refactoring-in-java.html))

3
Tennis/objc/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
Tennis.xcodeproj/project.xcworkspace/xcuserdata/*
Tennis.xcodeproj/xcuserdata/*

View File

@ -0,0 +1,305 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
CACF63F14142F5803F786695 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CACF69F4ABB8AC23884C74C3 /* Foundation.framework */; };
CACF6414A388566B43A9BCB5 /* TennisGame.m in Sources */ = {isa = PBXBuildFile; fileRef = CACF649AAFA398A61A600CB7 /* TennisGame.m */; };
CACF66A873EB3C4C2A1B7D78 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CACF6C52F7301D6A288FECCA /* UIKit.framework */; };
CACF6871FE27A26189B71C29 /* TennisGame1.m in Sources */ = {isa = PBXBuildFile; fileRef = CACF6BF242B4F5F91193BEC7 /* TennisGame1.m */; };
CACF68FFD67D0173EDF11BF4 /* TennisTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CACF6D897D4F49BCD1B41947 /* TennisTests.m */; };
CACF6C5876E4687F7A65AF54 /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CACF6A13AA71A254E309FB03 /* SenTestingKit.framework */; };
CACF6E1993921B2EC1995F0E /* TennisGame2.m in Sources */ = {isa = PBXBuildFile; fileRef = CACF64EDAB086A5F18E393EE /* TennisGame2.m */; };
CACF6F3B5CEDAF9DFE1B8A64 /* TennisGame3.m in Sources */ = {isa = PBXBuildFile; fileRef = CACF6BAD45E82D978466F92B /* TennisGame3.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
CACF61291A7012C7769FCE61 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
CACF6419833B61F7E3D89075 /* TennisGame3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TennisGame3.h; sourceTree = "<group>"; };
CACF649AAFA398A61A600CB7 /* TennisGame.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TennisGame.m; sourceTree = "<group>"; };
CACF64EDAB086A5F18E393EE /* TennisGame2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TennisGame2.m; sourceTree = "<group>"; };
CACF665E8EA24EE215E5498C /* TennisTests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TennisTests.octest; sourceTree = BUILT_PRODUCTS_DIR; };
CACF6771858FAF6F6F4650A6 /* TennisGame2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TennisGame2.h; sourceTree = "<group>"; };
CACF69F4ABB8AC23884C74C3 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
CACF6A13AA71A254E309FB03 /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; };
CACF6A4205D54CCF3AEA2B65 /* TennisTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TennisTests.h; sourceTree = "<group>"; };
CACF6A964A59A72222255B2C /* TennisGame.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TennisGame.h; sourceTree = "<group>"; };
CACF6B7F36643E0787BABC39 /* TennisGame1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TennisGame1.h; sourceTree = "<group>"; };
CACF6BAD45E82D978466F92B /* TennisGame3.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TennisGame3.m; sourceTree = "<group>"; };
CACF6BF242B4F5F91193BEC7 /* TennisGame1.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TennisGame1.m; sourceTree = "<group>"; };
CACF6C52F7301D6A288FECCA /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
CACF6D897D4F49BCD1B41947 /* TennisTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TennisTests.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
CACF6A7D79290F13325B461F /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
CACF6C5876E4687F7A65AF54 /* SenTestingKit.framework in Frameworks */,
CACF66A873EB3C4C2A1B7D78 /* UIKit.framework in Frameworks */,
CACF63F14142F5803F786695 /* Foundation.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
CACF60033C37E3B5C11CC734 /* Frameworks */ = {
isa = PBXGroup;
children = (
CACF6C52F7301D6A288FECCA /* UIKit.framework */,
CACF69F4ABB8AC23884C74C3 /* Foundation.framework */,
CACF61291A7012C7769FCE61 /* CoreGraphics.framework */,
CACF6A13AA71A254E309FB03 /* SenTestingKit.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
CACF652B01231CADE96100D1 = {
isa = PBXGroup;
children = (
CACF67CF0393B97F4DC41E0B /* Tennis */,
CACF65D28A08CBB9A2C540F5 /* TennisTests */,
CACF6E7D48293F4D43E7A6DC /* Products */,
CACF60033C37E3B5C11CC734 /* Frameworks */,
);
sourceTree = "<group>";
};
CACF65D28A08CBB9A2C540F5 /* TennisTests */ = {
isa = PBXGroup;
children = (
CACF6A4205D54CCF3AEA2B65 /* TennisTests.h */,
CACF6D897D4F49BCD1B41947 /* TennisTests.m */,
);
path = TennisTests;
sourceTree = "<group>";
};
CACF67CF0393B97F4DC41E0B /* Tennis */ = {
isa = PBXGroup;
children = (
CACF6BF242B4F5F91193BEC7 /* TennisGame1.m */,
CACF6B7F36643E0787BABC39 /* TennisGame1.h */,
CACF64EDAB086A5F18E393EE /* TennisGame2.m */,
CACF6771858FAF6F6F4650A6 /* TennisGame2.h */,
CACF6BAD45E82D978466F92B /* TennisGame3.m */,
CACF6419833B61F7E3D89075 /* TennisGame3.h */,
CACF649AAFA398A61A600CB7 /* TennisGame.m */,
CACF6A964A59A72222255B2C /* TennisGame.h */,
);
path = Tennis;
sourceTree = "<group>";
};
CACF6E7D48293F4D43E7A6DC /* Products */ = {
isa = PBXGroup;
children = (
CACF665E8EA24EE215E5498C /* TennisTests.octest */,
);
name = Products;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
CACF6DE0B90C4D92A2B3D5E6 /* TennisTests */ = {
isa = PBXNativeTarget;
buildConfigurationList = CACF68FD0CC2343E15B3AA80 /* Build configuration list for PBXNativeTarget "TennisTests" */;
buildPhases = (
CACF6FE60766CDED5F49C391 /* Sources */,
CACF6A7D79290F13325B461F /* Frameworks */,
CACF6814909F4B826FA2E0CA /* Resources */,
CACF6F4E118C6D42BF7A89A2 /* ShellScript */,
);
buildRules = (
);
dependencies = (
);
name = TennisTests;
productName = TennisTests;
productReference = CACF665E8EA24EE215E5498C /* TennisTests.octest */;
productType = "com.apple.product-type.bundle";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
CACF6DA3161729BC17499E03 /* Project object */ = {
isa = PBXProject;
attributes = {
ORGANIZATIONNAME = "Stefan van den Oord";
};
buildConfigurationList = CACF6959B8F6F7EC1647AAB0 /* Build configuration list for PBXProject "Tennis" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = CACF652B01231CADE96100D1;
productRefGroup = CACF6E7D48293F4D43E7A6DC /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
CACF6DE0B90C4D92A2B3D5E6 /* TennisTests */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
CACF6814909F4B826FA2E0CA /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXShellScriptBuildPhase section */
CACF6F4E118C6D42BF7A89A2 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "# Run the unit tests in this test bundle.\n\"${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests\"\n";
};
/* End PBXShellScriptBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
CACF6FE60766CDED5F49C391 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CACF68FFD67D0173EDF11BF4 /* TennisTests.m in Sources */,
CACF6F3B5CEDAF9DFE1B8A64 /* TennisGame3.m in Sources */,
CACF6E1993921B2EC1995F0E /* TennisGame2.m in Sources */,
CACF6871FE27A26189B71C29 /* TennisGame1.m in Sources */,
CACF6414A388566B43A9BCB5 /* TennisGame.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
CACF601C65BCC0FBE3D880E3 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "";
FRAMEWORK_SEARCH_PATHS = (
"\"$(SDKROOT)/Developer/Library/Frameworks\"",
"\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "";
WRAPPER_EXTENSION = octest;
};
name = Release;
};
CACF6452D3F349731DB85BE9 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 6.1;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
name = Release;
};
CACF6A641D1372E5C5478EB2 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "";
FRAMEWORK_SEARCH_PATHS = (
"\"$(SDKROOT)/Developer/Library/Frameworks\"",
"\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "";
WRAPPER_EXTENSION = octest;
};
name = Debug;
};
CACF6FD238549A86B0925D65 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 6.1;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
CACF68FD0CC2343E15B3AA80 /* Build configuration list for PBXNativeTarget "TennisTests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
CACF601C65BCC0FBE3D880E3 /* Release */,
CACF6A641D1372E5C5478EB2 /* Debug */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
CACF6959B8F6F7EC1647AAB0 /* Build configuration list for PBXProject "Tennis" */ = {
isa = XCConfigurationList;
buildConfigurations = (
CACF6452D3F349731DB85BE9 /* Release */,
CACF6FD238549A86B0925D65 /* Debug */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = CACF6DA3161729BC17499E03 /* Project object */;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:Tennis.xcodeproj">
</FileRef>
</Workspace>

View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CACF6DE0B90C4D92A2B3D5E6"
BuildableName = "TennisTests.octest"
BlueprintName = "TennisTests"
ReferencedContainer = "container:Tennis.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CACF6DE0B90C4D92A2B3D5E6"
BuildableName = "TennisTests.octest"
BlueprintName = "TennisTests"
ReferencedContainer = "container:Tennis.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -0,0 +1,9 @@
#import <Foundation/Foundation.h>
@interface TennisGame : NSObject
- (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2;
- (void)wonPoint:(NSString *)playerName;
- (NSString *)score;
@end

View File

@ -0,0 +1,10 @@
#import "TennisGame.h"
@implementation TennisGame
- (id)initWithPlayer1:(NSString *)player1 player2:(NSString *)player2 { return [super init]; }
- (void)wonPoint:(NSString *)playerName {}
- (NSString *)score { return nil; }
@end

View File

@ -0,0 +1,5 @@
#import <Foundation/Foundation.h>
#import "TennisGame.h"
@interface TennisGame1 : TennisGame
@end

View File

@ -0,0 +1,92 @@
#import "TennisGame1.h"
@interface TennisGame1 ()
@property(nonatomic, copy) NSString *player1;
@property(nonatomic, copy) NSString *player2;
@end
@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"])
score1 += 1;
else
score2 += 1;
}
- (NSString *)score {
NSString *score = @"";
int tempScore=0;
if (score1 == score2)
{
switch (score1)
{
case 0:
score = @"Love-All";
break;
case 1:
score = @"Fifteen-All";
break;
case 2:
score = @"Thirty-All";
break;
case 3:
score = @"Forty-All";
break;
default:
score = @"Deuce";
break;
}
}
else if (score1>=4 || score2>=4)
{
int minusResult = score1-score2;
if (minusResult==1) score = @"Advantage player1";
else if (minusResult ==-1) score = @"Advantage player2";
else if (minusResult>=2) score = @"Win for player1";
else score = @"Win for player2";
}
else
{
for (int i=1; i<3; i++)
{
if (i==1) tempScore = score1;
else { score = [NSString stringWithFormat:@"%@-", score]; tempScore = score2; }
switch(tempScore)
{
case 0:
score = [NSString stringWithFormat:@"%@Love", score];
break;
case 1:
score = [NSString stringWithFormat:@"%@Fifteen", score];
break;
case 2:
score = [NSString stringWithFormat:@"%@Thirty", score];
break;
case 3:
score = [NSString stringWithFormat:@"%@Forty", score];
break;
}
}
}
return score;
}
@end

View File

@ -0,0 +1,5 @@
#import <Foundation/Foundation.h>
#import "TennisGame.h"
@interface TennisGame2 : TennisGame
@end

View File

@ -0,0 +1,147 @@
#import "TennisGame2.h"
@interface TennisGame2 ()
@property(nonatomic, copy) NSString *player2Name;
@property(nonatomic, copy) NSString *player1Name;
@end
@implementation TennisGame2 {
int P1point;
NSString *P1res;
int P2point;
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)
{
if (P1point==0)
score = @"Love";
if (P1point==1)
score = @"Fifteen";
if (P1point==2)
score = @"Thirty";
if (P1point==3)
score = @"Forty";
score = [NSString stringWithFormat:@"%@-All", score];
}
if (P1point==P2point && P1point>3)
score = @"Deuce";
if (P1point > 0 && P2point==0)
{
if (P1point==1)
P1res = @"Fifteen";
if (P1point==2)
P1res = @"Thirty";
if (P1point==3)
P1res = @"Forty";
P2res = @"Love";
score = [NSString stringWithFormat:@"%@-%@", P1res, P2res];
}
if (P2point > 0 && P1point==0)
{
if (P2point==1)
P2res = @"Fifteen";
if (P2point==2)
P2res = @"Thirty";
if (P2point==3)
P2res = @"Forty";
P1res = @"Love";
score = [NSString stringWithFormat:@"%@-%@", P1res, P2res];
}
if (P1point>P2point && P1point < 4)
{
if (P1point==2)
P1res=@"Thirty";
if (P1point==3)
P1res=@"Forty";
if (P2point==1)
P2res=@"Fifteen";
if (P2point==2)
P2res=@"Thirty";
score = [NSString stringWithFormat:@"%@-%@", P1res, P2res];
}
if (P2point>P1point && P2point < 4)
{
if (P2point==2)
P2res=@"Thirty";
if (P2point==3)
P2res=@"Forty";
if (P1point==1)
P1res=@"Fifteen";
if (P1point==2)
P1res=@"Thirty";
score = [NSString stringWithFormat:@"%@-%@", P1res, P2res];
}
if (P1point > P2point && P2point >= 3)
{
score = @"Advantage player1";
}
if (P2point > P1point && P1point >= 3)
{
score = @"Advantage player2";
}
if (P1point>=4 && P2point>=0 && (P1point-P2point)>=2)
{
score = @"Win for player1";
}
if (P2point>=4 && P1point>=0 && (P2point-P1point)>=2)
{
score = @"Win for player2";
}
return score;
}
-(void)setP1Score:(int)number {
for (int i = 0; i < number; i++)
{
[self P1Score];
}
}
- (void)setP2Score:(int)number {
for (int i = 0; i < number; i++)
{
[self P2Score];
}
}
- (void)P1Score {
P1point++;
}
- (void)P2Score {
P2point++;
}
- (void)wonPoint:(NSString *)playerName {
if ([playerName isEqualToString:@"player1"])
[self P1Score];
else
[self P2Score];
}
@end

View File

@ -0,0 +1,5 @@
#import <Foundation/Foundation.h>
#import "TennisGame.h"
@interface TennisGame3 : TennisGame
@end

View File

@ -0,0 +1,46 @@
#import "TennisGame3.h"
@interface TennisGame3 ()
@end
@implementation TennisGame3 {
int p1;
int p2;
NSString *p1N;
NSString *p2N;
}
- (id)initWithPlayer1:(NSString *)ap1N player2:(NSString *)ap2N {
self = [super initWithPlayer1:ap1N player2:ap2N];
if (self) {
p1N = ap1N;
p2N = ap2N;
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

View File

@ -0,0 +1,5 @@
#import <SenTestingKit/SenTestingKit.h>
@interface TennisTests : SenTestCase
- (id)initWithInvocation:(NSInvocation *)invocation scores:(NSArray *)scores;
@end

View File

@ -0,0 +1,121 @@
#import <SenTestingKit/SenTestingKit.h>
#import "TennisTests.h"
#import "TennisGame1.h"
#import "TennisGame2.h"
#import "TennisGame3.h"
@interface TennisTests()
+ (NSArray*)parameters;
@end
@implementation TennisTests (Parametrized)
+ (id)defaultTestSuite {
SenTestSuite *testSuite = [[SenTestSuite alloc] initWithName:NSStringFromClass(self)];
NSArray *allScores = [self parameters];
for (NSArray *scores in allScores) {
[self addTestWithScores:scores toTestSuite:testSuite];
}
return testSuite;
}
+ (void)addTestWithScores:(NSArray *)scores toTestSuite:(SenTestSuite *)testSuite {
NSArray *testInvocations = [self testInvocations];
for (NSInvocation *testInvocation in testInvocations) {
SenTestCase *test = [[TennisTests alloc] initWithInvocation:testInvocation scores:scores];
[testSuite addTest:test];
}
}
@end
@implementation TennisTests {
int player1Score;
int player2Score;
NSString *expectedScore;
}
+ (NSArray*)parameters {
return @[
@[ @0, @0, @"Love-All"],
@[ @1, @1, @"Fifteen-All"],
@[ @2, @2, @"Thirty-All"],
@[ @3, @3, @"Forty-All"],
@[ @4, @4, @"Deuce"],
@[ @1, @0, @"Fifteen-Love"],
@[ @0, @1, @"Love-Fifteen"],
@[ @2, @0, @"Thirty-Love"],
@[ @0, @2, @"Love-Thirty"],
@[ @3, @0, @"Forty-Love"],
@[ @0, @3, @"Love-Forty"],
@[ @4, @0, @"Win for player1"],
@[ @0, @4, @"Win for player2"],
@[ @2, @1, @"Thirty-Fifteen"],
@[ @1, @2, @"Fifteen-Thirty"],
@[ @3, @1, @"Forty-Fifteen"],
@[ @1, @3, @"Fifteen-Forty"],
@[ @4, @1, @"Win for player1"],
@[ @1, @4, @"Win for player2"],
@[ @3, @2, @"Forty-Thirty"],
@[ @2, @3, @"Thirty-Forty"],
@[ @4, @2, @"Win for player1"],
@[ @2, @4, @"Win for player2"],
@[ @4, @3, @"Advantage player1"],
@[ @3, @4, @"Advantage player2"],
@[ @5, @4, @"Advantage player1"],
@[ @4, @5, @"Advantage player2"],
@[ @15, @14, @"Advantage player1"],
@[ @14, @15, @"Advantage player2"],
@[ @6, @4, @"Win for player1"],
@[ @4, @6, @"Win for player2"],
@[ @16, @14, @"Win for player1"],
@[ @14, @16, @"Win for player2"]
];
}
- (id)initWithInvocation:(NSInvocation *)invocation scores:(NSArray *)scores {
self = [super initWithInvocation:invocation];
if (self) {
player1Score = [scores[0] intValue];
player2Score = [scores[1] intValue];
expectedScore = scores[2];
}
return self;
}
- (NSString *)name {
NSString *parametersDescription = [NSString stringWithFormat:@" (%d,%d,%@)]", player1Score, player2Score, expectedScore];
return [[super name] stringByReplacingOccurrencesOfString:@"]" withString:parametersDescription];
}
- (void)checkAllScoresForGame:(TennisGame *)game {
int highestScore = MAX(player1Score, player2Score);
for (int i = 0; i < highestScore; i++) {
if (i < player1Score)
[game wonPoint:@"player1"];
if (i < player2Score)
[game wonPoint:@"player2"];
}
STAssertEqualObjects([game score], expectedScore, @"");
}
- (void)testAllScoresTennisGame1 {
TennisGame1 * game = [[TennisGame1 alloc] initWithPlayer1:@"player1" player2:@"player2"];
[self checkAllScoresForGame:game];
}
- (void)testAllScoresTennisGame2 {
TennisGame2 * game = [[TennisGame2 alloc] initWithPlayer1:@"player1" player2:@"player2"];
[self checkAllScoresForGame:game];
}
- (void)testAllScoresTennisGame3 {
TennisGame3 * game = [[TennisGame3 alloc] initWithPlayer1:@"player1" player2:@"player2"];
[self checkAllScoresForGame:game];
}
@end