user3449971 user3449971 - 1 year ago 93
iOS Question

How to block score to not go below zero?

I'm making a word game but I can't figure out how to make zero the final point in the basic scoring I've created. The user must not exceed a minus in his or her score, thus the lowest score can only be zero...

- (IBAction)btncheck:(id)sender {
NSString *answer = [_textbox.text stringByReplacingOccurrencesOfString:@" " withString:@""]
if([answer isEqualToString:@""]){
if ([answer isEqualToString:@"q"]) {
// String is correct, resign keyboard
_keyboard.hidden = YES;
_textXclear.hidden = YES;
//Perfect button
[_closeone setHidden:NO];
[_wrongone setHidden:YES];
score = score +100;
[scoreLabel setText:[NSString stringWithFormat:@"score: %d", score]];
coins = coins +5;
[coinsLabel setText:[NSString stringWithFormat:@"%d", coins]];
else {
[_wrongone setHidden:NO];
score = score -5 ;
[scoreLabel setText:[NSString stringWithFormat:@"score: %d", score]];
closeonechange.text = @"Correct!";


Oh and if the score becomes zero the user must only receive 1 coin.

How will I do this?

Answer Source

Just use MAX

score = MAX(0, score - 5);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download