This can be a tough question. As you become more experienced, you will become more comfortable with more advanced concepts. The problem you are working with is actually quite complex and makes a great learning tool.
The biggest problem is that your current solution does not use object orientation, which makes it difficult to support and / or expand in the future.
Ultimately, the question of the optimal structure of the code can have many answers, and you may not know what is better than the next, until you add more functionality to your program.
I reprogrammed your program, in which I feel that this is a solid final game structure (as opposed to shooting with a shorter intermediate step). Unfortunately, this can be a little leap when you are just starting.
There are two advanced concepts in this solution: object-oriented programming and a selector. Selectors are an incredibly powerful tool that lets you pass the actual instructions on your program using variables. In your case, you can store if statements in your diagnostic objects.
In any case, please feel free to ask any questions about the following:
Vitals.h
#import <Foundation/Foundation.h> @interface Vitals : NSObject { float _pH; int _paCO2; int _hCO3; } - (id) initWithPH:(float)pH paCO2:(int)paCO2 hCO3:(int)hCO3; - (float) pH; - (int) paCO2; - (int) hCO3; @end
Vitals.m
#import "Vitals.h" @implementation Vitals - (id) initWithPH:(float)pH paCO2:(int)paCO2 hCO3:(int)hCO3 { if (self = [super init]) { _pH = pH; _paCO2 = paCO2; _hCO3 = hCO3; } return self; } - (float) pH {return _pH;} - (int) paCO2 {return _paCO2;} - (int) hCO3 {return _hCO3;} @end
Diagnosis.h
Diagnosis.m
#import "Diagnosis.h" @implementation Diagnosis - (id) initWithName:(NSString*)name delegate:(id)delegate test:(SEL)test { if (self = [super init]) { _name = [name retain]; _delegate = delegate; _test = test; } return self; } - (void) dealloc { [_name release]; [super dealloc]; } - (NSString*) name {return _name;} - (BOOL) test:(Vitals*)vitals { return [(NSNumber*)[_delegate performSelector:_test withObject:vitals] boolValue]; } @end
Doctor.h
Doctor.m
#import "Diagnosis.h" #import "Doctor.h" @implementation Doctor - (id) init { if (self = [super init]) { _diagnoses = [[NSMutableArray alloc] init]; } return self; } - (void) dealloc { [_diagnoses release]; [super dealloc]; } - (void) learnDiagnosis:(Diagnosis*)diagnosis { [_diagnoses addObject:diagnosis]; } - (Diagnosis*) diagnose:(Vitals*)vitals { for (Diagnosis* diagnosis in _diagnoses) { if ([diagnosis test:vitals]) return diagnosis; } return 0; } @end
Differential.h
Differential.m
#import "Vitals.h" #import "Diagnosis.h" #import "Doctor.h" #import "Differential.h" @implementation Differential - (NSNumber*) acuteRespiratoryAcidosis:(Vitals*)vitals { return [NSNumber numberWithBool:(([vitals pH] < 7.35) && ([vitals paCO2] > 45) && ([vitals hCO3] >=22 && [vitals hCO3] <=26))]; } - (NSNumber*) partiallyCompensatedResporatoryAcidosis:(Vitals*)vitals { return [NSNumber numberWithBool:(([vitals pH] < 7.35) && ([vitals paCO2] > 45) && ([vitals hCO3] >26))]; } - (void) teach:(Doctor*)doctor { Diagnosis* diagnosis; diagnosis = [[Diagnosis alloc] initWithName:@"Acute Respiratory Acidosis" delegate:self test:@selector(acuteRespiratoryAcidosis:)]; [doctor learnDiagnosis:diagnosis]; [diagnosis release]; diagnosis = [[Diagnosis alloc] initWithName:@"Partially Compensated Respiratory Acidosis" delegate:self test:@selector(partiallyCompensatedResporatoryAcidosis:)]; [doctor learnDiagnosis:diagnosis]; [diagnosis release]; } @end
Sandbox.h
#import <Foundation/Foundation.h> #import "Vitals.h" #import "Diagnosis.h" #import "Doctor.h" #import "Differential.h" void run () { float pH=7.2; int paCO2=47; int hCO3=25; Doctor* doctor = [[Doctor alloc] init]; Differential* differential = [[Differential alloc] init]; [differential teach:doctor]; Vitals* vitals = [[Vitals alloc] initWithPH:pH paCO2:paCO2 hCO3:hCO3]; Diagnosis* diagnosis = [doctor diagnose:vitals]; NSLog(@"%@",[diagnosis name]); [vitals release]; [differential release]; [doctor release]; }