在编写代码的过程中,把各项功能分开写出来,互不影响,这样可以方便的调用,在出现错误是,也可以很容易的找出错误,对编写代码有很好的帮助。
以数字游戏为例,用户输入4个不同的数字 ,与系统产生的随机4个数对比 如果同位置一样则显示几A位置不同但是有此数字则显示几B 都不同则0A0B 都猜对则游戏成功,6次失败则游戏失败
首先产生随机数
//
// Radom.h
// ShuZiYouXi
//
.
//
#import <UIKit/UIKit.h>
@interface Radom : NSObject
+(NSArray *)randomArray;
@end
//
// Radom.m
// ShuZiYouXi
//
#import "Radom.h"
@implementation Radom
+(NSArray *)randomArray
{
NSMutableArray *startArray = [[NSMutableArray alloc]initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
NSMutableArray *resultArray = [[NSMutableArray alloc]initWithCapacity:0];
for (int i = 0; i < 4; i ++)
{
int k = arc4random()%startArray.count;
resultArray[i] = startArray[k];
startArray[k] = [startArray lastObject];//为了更好的乱序,交换下位置
[startArray removeLastObject];
}
return resultArray;
}//生成随机的4个数
@end
对比随机数和自己输入的数是否一样
//
// Compare.h
// ShuZiYouXi
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface Compare : NSObject //建立功能类是建立NSObject即可
+(NSString *)compare_info :(NSArray *)arr_random_numbers :(NSArray *)user_filled_numbers;
@end
//
// Compare.m
// ShuZiYouXi
//
//
#import "Compare.h"
#import "Radom.h"
@implementation Compare
+(NSString *)compare_info :(NSArray *)arr_random_numbers :(NSArray *)user_filled_numbers
{
int a_count = 0;
int b_count = 0;
for (int j = 0; j < user_filled_numbers.count ; j++)
{
for (int m = 0; m < arr_random_numbers.count; m++)
{
if ([[user_filled_numbers objectAtIndex:j]isEqualToString:
[arr_random_numbers objectAtIndex:m]])
{
if (j == m)
a_count++;
else
b_count++;
}
}
}
NSString *result_info = [NSString stringWithFormat:@"%dA%dB",a_count,b_count];
return result_info;
}
//arr_random_numbers和user_filled_numbers 是传进来的数 可以进行任意的更改 ,此方法与上一部分产生的随机数
没有直接的关联
@end
控制游戏的次数
//
// Compare.h
// ShuZiYouXi
//
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface Compare : NSObject
+(NSString *)compare_info :(NSArray *)arr_random_numbers :(NSArray *)user_filled_numbers;
@end
//
// NumberOfGame.m
// ShuZiYouXi
//
//
#import "NumberOfGame.h"
#import "Compare.h"
#import "Radom.h"
@implementation NumberOfGame
//- (void)viewDidLoad {
// [super viewDidLoad];
// self.view.backgroundColor = [UIColor whiteColor];
//
//}
+(NSString *)compare_result :(NSString *)comare_result :(int)count_number
{
NSString *label_text;
if (count_number < 6 )
{
if ([comare_result isEqualToString:@"4A0B"])
label_text = @"恭喜您猜对了!";
else
label_text = @"很遗憾,请再猜一次!";
}
else if (count_number == 6)
{
if ([comare_result isEqualToString:@"4A0B"])
label_text = @"恭喜您猜对了!";
else
label_text = @"对不起,游戏失败!";
}
return label_text;
}
@end
三个功能相互没有关联,单独写出来,要是有错误的地方会一目了然
测试用例
//
// ShuZiYouXiTests.m
// ShuZiYouXiTests
//
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
@interface ShuZiYouXiTests : XCTestCase
@end
@implementation ShuZiYouXiTests
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample {
// This is an example of a functional test case.
XCTAssert(YES, @"Pass");
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}
@end
测试功能时建个testcase 把测试的代码写进去测试即可