To replace our hardcoded values, we need to use the following steps:
Open the
Battlefield.m
file.Add a new method called
fuzzyValue
, as shown in the following code:-(float) fuzzyValue: (NSString *) value { if ([value isEqualToString:@"Very near"]) { return (float) (arg4random() % 40) + 40.0f; } else if ([value isEqualToString:@"Quite near"]) { result = (float) (arc4random() % 30) + 70.0f; } else { result = (float) (arc4random() % 50) + 150.0f; } }
Using the following code, update the hardcoded values with the values from the
fuzzyValue
method:if ([ship checkDistanceToShip:_pirateShip] < [self fuzzyValue:@"Near"]) { if ([ship checkDistanceToShip:_pirateShip] < [self fuzzyValue:@"Very near"]) { if ([ship checkDistanceToShip:_pirateShip] < [self fuzzyValue:@"Quite near"]) {
Run the example. If we were to insert logging to see what the values actually are, we would see the following output: