#import "DoorsViewController.h"
@implementation DoorsViewController
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
- (void)awakeFromNib {
highScore = [prefs integerForKey:@"highScore"];
srandom(time(NULL));
[self setLevel:1 withMessage:nil];
}
- (IBAction)clickDoor:(id)sender {
int correctDoor = random() % 3 + 1;
if(correctDoor == [sender tag])
[self setLevel:level + 1 withMessage:[NSString stringWithFormat:@"You passed! Welcome to level %i!", level + 1]];
else {
[self setLevel:1 withMessage:[NSString stringWithFormat:@"Sorry, you failed! The correct door was door #%i!", correctDoor]];
wrongCount++;
[wrong setText:[NSString stringWithFormat:@"In this session you have gotten %d wrong", wrongCount]];
}
}
- (void)setLevel:(int)newLevel withMessage:(NSString *)message {
level = newLevel;
[self setTitle:[NSString stringWithFormat:@"Level %i", level]];
if (message)
[successLabel setText:message];
if (level > highScore) {
highScore = level;
[scoreLabel setText:[NSString stringWithFormat:@"High Score: %i", highScore]];
[prefs setInteger:highScore forKey:@"highScore"];
}
}
- (IBAction)resetDoor:(id)sender
{
level = 1;
wrongCount = 0;
[self setTitle:[NSString stringWithFormat:@"Level %i", level]];
[wrong setText:[NSString stringWithFormat:@"In this session you have gotten %d wrong", wrongCount]];
}
@end