[QiShare team] [QiShare team] [QiShare team] [QiShare team

Preface: company department is organizing group to build recently, need to prepare prepare two group to build small game, be respectively “digital quick calculate upgrade version” and “your words I guess upgrade version”.

After a bit of thinking, I found that these two small projects are very suitable for getting started with iOS, so this article was born. This article will introduce iOS small game project — digital quick calculation upgrade version. I hope this article can help students who are interested in iOS get a quick start on iOS.

The renderings are as follows:

I. Project requirements

  1. UI level: 8 labels and 3 buttons.

Illustration:

  1. Logical level:

    • Click the question/Start button to randomly generate three numbers (two digits or less) and two operators (add, subtract, multiply).
    • Set a timer from0Start the timer until the game ends and check how long the game lasts.
    • Click create to refresh the topic.
    • Click on the result to calculate the result.
    • Click the Start button, a popup window appears, click OK, start timing.
  2. Problem probability:

    • The ones digit is more likely than the two digit.
    • Multiplication occurs less often than addition and subtraction.

Second, implementation ideas

  1. The UI level:
  • Method 1: Storyboard (drag controls, add constraints).
  • Method two: Pure code.

In the project, I chose the storyboard. When developing independently, storyboards are more efficient.

@property (weak, nonatomic) IBOutlet UILabel *factorLabel1; / /! < digital label1@property (weak, nonatomic) IBOutlet UILabel *factorLabel2; / /! < digital label2@property (weak, nonatomic) IBOutlet UILabel *factorLabel3; / /! < numeric Label3 @property (weak, nonatomic) IBOutlet UILabel *operatorLabel1; / /! < operator label1@property (weak, nonatomic) IBOutlet UILabel *operatorLabel2; / /! < operator label2@property (weak, nonatomic) IBOutlet UILabel *resultLabel; / /! < result Label @property (weak, nonatomic) IBOutlet UILabel *recordingLabel; / /! < timer label@property (weak, nonatomic) IBOutlet UIButton *questionButton; / /! < button@property (weak, nonatomic) IBOutlet UIButton *resultButton; / /! < result button@property (weak, nonatomic) IBOutlet UIButton *startButton; / /! The start ButtonCopy the code
  1. Business logic:
  • Attributes to save:
@property (nonatomic, strong) NSTimer *timer; / /! The < timerCopy the code
  • Start button business logic:
- (IBAction)startButtonClicked (UIButton *) Sender {NSString *message = [NSString stringWithFormat:@" do you want %@? ", sender.currentTitle]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle: @ "cancel" style: UIAlertActionStyleCancel handler: nil]; UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:sender.currentTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { sender.selected = !sender.selected; self.resultButton.enabled = !self.resultButton.enabled; if (sender.selected) { [self resetElements]; [self startTimer]; } else { [self stopTimer]; } }]; [alertController addAction:cancelAction]; [alertController addAction:confirmAction]; [self.navigationController presentViewController:alertController animated:YES completion:nil]; }Copy the code
  • Button business logic:
- (IBAction)questionButtonClicked:(id)sender { _questionButton.enabled = NO; _resultButton.enabled = YES; [self setQuestion]; if (_speechManager) { _recordingLabel.text = @""; _recordingLabel.layer.borderWidth = .0; [_speechManager startRecordingWithResponse:^(NSString * _Nonnull formatString) { self.recordingLabel.text = [formatString componentsSeparatedByString:@" "].lastObject; }]; }}Copy the code
  • Result button business logic:
- (IBAction)resultButtonClicked:(id)sender {
    
    _questionButton.enabled = YES;
    _resultButton.enabled = NO;
    
    _resultLabel.text = @([self calculate]).stringValue;
    
    if (_speechManager) {
        [_speechManager stopRecording];
        
        _recordingLabel.layer.borderWidth = 1.0;
        if ([_recordingLabel.text isEqualToString:_resultLabel.text]) {
            _recordingLabel.layer.borderColor = [UIColor greenColor].CGColor;
        } else {
            _recordingLabel.layer.borderColor = [UIColor redColor].CGColor;
        }
    }
}
Copy the code
  • Business logic:
- (void)setQuestion { _resultLabel.text = @""; _factorLabel1.text = [self generateFactor]; _factorLabel2.text = [self generateFactor]; _factorLabel3.text = [self generateFactor]; _operatorLabel1.text = [self generateOperator]; _operatorLabel2.text = [self generateOperator]; } / /! GenerateFactor {NSUInteger r = arc4Random () % 10; NSUInteger max = r < 4? 10: r < 7? 20: r < 9? 50:100; NSUInteger factor = arc4random() % max; return @(factor).stringValue; } / /! GenerateOperator - (NSString *)generateOperator {NSUInteger r = arc4random() % 5; NSString *operator = r < 2? @"+": r < 4? @ "-" : @ "x"; return operator; }Copy the code
  • Calculation method Business logic:
- (NSInteger)calculate {
    
    NSUInteger factor1 = _factorLabel1.text.integerValue;
    NSUInteger factor2 = _factorLabel2.text.integerValue;
    NSUInteger factor3 = _factorLabel3.text.integerValue;
    
    NSString *operator1 = _operatorLabel1.text;
    NSString *operator2 = _operatorLabel2.text;
    
    NSInteger result = [self calculateWithOperator:operator1 leftFactor:factor1 rightFactor:factor2];
    if ([operator2 isEqualToString:@"×"]) {
        result = [self calculateWithOperator:operator2 leftFactor:factor2 rightFactor:factor3];
        result = [self calculateWithOperator:operator1 leftFactor:factor1 rightFactor:result];
    } else {
        result = [self calculateWithOperator:operator2 leftFactor:result rightFactor:factor3];
    }
    
    return result;
}

- (NSUInteger)calculateWithOperator:(NSString *)operator leftFactor:(NSUInteger)leftFactor rightFactor:(NSUInteger)rightFactor  {
    
    NSInteger result = leftFactor;
    
    if ([operator isEqualToString:@"+"]) {
        result += rightFactor;
    } else if ([operator isEqualToString:@"-"]) {
        result -= rightFactor;
    } else {
        result *= rightFactor;
    }
    
    return result;
}
Copy the code
  • Reset element logic:
- (void)resetElements {
    
    _factorLabel1.text = @"0";
    _factorLabel2.text = @"0";
    _factorLabel3.text = @"0";
    
    _operatorLabel1.text = @"+";
    _operatorLabel2.text = @"+";
    
    _resultLabel.text = @"0";
    _recordingLabel.text = @"0";
    
    _questionButton.enabled = YES;
    _resultButton.enabled = YES;
}
Copy the code
  • Timer business logic:
- (void)startTimer { [self stopTimer]; _timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @ the selector (countUp) the userInfo: nil repeats: YES]; } - (void)stopTimer { [_timer invalidate]; _timer = nil; } - (void)countUp { NSInteger count = _recordingLabel.text.integerValue; _recordingLabel.text = @(++count).stringValue; }Copy the code

3. Difficulty: difficulty probability

  • Number generation probability algorithm:
The random number Digital range The probability of
0,1,2,3 0 ~ 9 40%
4,5,6 0 ~ 20 30%
7, 8 0 ~ 50 20%
9 0 ~ 100 10%
/ /! GenerateFactor {NSUInteger r = arc4Random () % 10; NSUInteger max = r < 4? 10: r < 7? 20: r < 9? 50:100; NSUInteger factor = arc4random() % max; return @(factor).stringValue; }Copy the code
  • Operator generation probability algorithm:
The random number The operator The probability of
0, 1 + 40%
2, 3 40%
4 x 20%
/ /! GenerateOperator - (NSString *)generateOperator {NSUInteger r = arc4random() % 5; NSString *operator = r < 2? @"+": r < 4? @ "-" : @ "x"; return operator; }Copy the code
  • Calculation algorithm:
- (NSInteger)calculate { NSUInteger factor1 = _factorLabel1.text.integerValue; NSUInteger factor2 = _factorLabel2.text.integerValue; NSUInteger factor3 = _factorLabel3.text.integerValue; NSString *operator1 = _operatorLabel1.text; NSString *operator2 = _operatorLabel2.text; NSInteger result = [self calculateWithOperator:operator1 leftFactor:factor1 rightFactor:factor2]; If ([operator2 isEqualToString: @ "x"]) {result = [self calculateWithOperator: operator2 leftFactor: factor2 rightFactor:factor3]; result = [self calculateWithOperator:operator1 leftFactor:factor1 rightFactor:result]; } else { result = [self calculateWithOperator:operator2 leftFactor:result rightFactor:factor3]; } return result; } - (NSUInteger)calculateWithOperator:(NSString *)operator leftFactor:(NSUInteger)leftFactor rightFactor:(NSUInteger)rightFactor { NSInteger result = leftFactor; if ([operator isEqualToString:@"+"]) { result += rightFactor; } else if ([operator isEqualToString:@"-"]) { result -= rightFactor; } else { result *= rightFactor; } return result; }Copy the code

Finally, project source: game source


QiShare(Simple book) QiShare(digging gold) QiShare(Zhihu) QiShare(GitHub) QiShare(CocoaChina) QiShare(StackOverflow) QiShare(wechat public account)

Write high quality Objective-C code for iOS (7)