iOS 小遊戲專案——數字速算升級版

QiShare發表於2019-02-28

級別: ★☆☆☆☆
標籤:「iOS」「小遊戲專案」「數字速算」
作者: MrLiuQ
審校: QiShare團隊

前言:最近公司部門在組織團建,需要準備準備兩個團建小遊戲, 分別是“數字速算升級版”和“你話我猜升級版”

小編琢磨了一下,發現這個兩個小專案很適合入門iOS,故這篇文章誕生了。 本篇將介紹 iOS 小遊戲專案——數字速算升級版。 希望通過這篇文章,能夠幫助對iOS感興趣的同學快速入門iOS。

效果圖如下:

效果圖

一、專案需求

  1. UI層面: 8個Label,3個Button。

圖解:

UI

  1. 邏輯層面:

    • 點選出題/開始按鈕,隨機生成三個數(兩位數及以下)和兩個運算子(加、減、乘)。
    • 做一個計時器,從0開始計時,直到遊戲結束,檢視遊戲時長。
    • 點選出題,重新整理題目。
    • 點選結果,計算出結果。
    • 點選開始按鈕,出現一個彈窗,點選確定,開始計時。
  2. 難題概率:

    • 個位數出現的概率比兩位數高。
    • 乘法出現的概率比加減法低。

二、實現思路

  1. UI層面:
  • 方式一:storyboard(拖控制元件、加約束)。
  • 方式二:純程式碼。

專案中,我選擇的storyboard。獨立開發時,用storyboard比較高效。

@property (weak, nonatomic) IBOutlet UILabel *factorLabel1;//!< 數字Label1
@property (weak, nonatomic) IBOutlet UILabel *factorLabel2;//!< 數字Label2
@property (weak, nonatomic) IBOutlet UILabel *factorLabel3;//!< 數字Label3
@property (weak, nonatomic) IBOutlet UILabel *operatorLabel1;//!< 運算子Label1
@property (weak, nonatomic) IBOutlet UILabel *operatorLabel2;//!< 運算子Label2
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;//!< 結果Label
@property (weak, nonatomic) IBOutlet UILabel *recordingLabel;//!< 計時Label
@property (weak, nonatomic) IBOutlet UIButton *questionButton;//!< 出題Button
@property (weak, nonatomic) IBOutlet UIButton *resultButton;//!< 結果Button
@property (weak, nonatomic) IBOutlet UIButton *startButton;//!< 開始Button
複製程式碼
  1. 業務邏輯:
  • 所要儲存的屬性:
@property (nonatomic, strong) NSTimer *timer;//!< 計時器
複製程式碼
  • 開始按鈕業務邏輯:
- (IBAction)startButtonClicked:(UIButton *)sender {
    
    NSString *message = [NSString stringWithFormat:@"確定要 %@ 嗎?", sender.currentTitle];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" 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];
}
複製程式碼
  • 出題按鈕業務邏輯:
- (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;
        }];
    }
}
複製程式碼
  • 結果按鈕業務邏輯:
- (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;
        }
    }
}
複製程式碼
  • 出題業務邏輯:
- (void)setQuestion {
    
    _resultLabel.text = @"";
    
    _factorLabel1.text = [self generateFactor];
    _factorLabel2.text = [self generateFactor];
    _factorLabel3.text = [self generateFactor];
    
    _operatorLabel1.text = [self generateOperator];
    _operatorLabel2.text = [self generateOperator];
}

//! 生成數字
- (NSString *)generateFactor {
    
    NSUInteger r = arc4random() % 10;
    NSUInteger max = r < 4? 10: r < 7? 20: r < 9? 50: 100;
    NSUInteger factor = arc4random() % max;
    
    return @(factor).stringValue;
}

//! 生成運算子
- (NSString *)generateOperator {
    
    NSUInteger r = arc4random() % 5;
    NSString *operator = r < 2? @"+": r < 4? @"-": @"×";
    
    return operator;
}
複製程式碼
  • 計算方法業務邏輯:
- (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;
}
複製程式碼
  • 重置元素邏輯:
- (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;
}
複製程式碼
  • 定時器業務邏輯:
- (void)startTimer {
    
    [self stopTimer];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
}

- (void)stopTimer {
    
    [_timer invalidate];
    _timer = nil;
}

- (void)countUp {
    
    NSInteger count = _recordingLabel.text.integerValue;
    _recordingLabel.text = @(++count).stringValue;
}
複製程式碼

三、難點:難題概率

  • 數字生成概率演算法:
隨機數 數字範圍 概率
0,1,2,3 0~9 40%
4,5,6 0~20 30%
7,8 0~50 20%
9 0~100 10%
//! 生成數字
- (NSString *)generateFactor {
    
    NSUInteger r = arc4random() % 10;
    NSUInteger max = r < 4? 10: r < 7? 20: r < 9? 50: 100;
    NSUInteger factor = arc4random() % max;
    
    return @(factor).stringValue;
}
複製程式碼
  • 運算子生成概率演算法:
隨機數 運算子 概率
0,1 + 40%
2,3 - 40%
4 x 20%
//! 生成運算子
- (NSString *)generateOperator {
    
    NSUInteger r = arc4random() % 5;
    NSString *operator = r < 2? @"+": r < 4? @"-": @"×";
    
    return operator;
}
複製程式碼
  • 計算演算法:
- (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;
}
複製程式碼

最後,工程原始碼:遊戲原始碼


關注我們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公眾號)

推薦文章:
iOS 繪製漸變·基礎篇
iOS 繪製漸變·例項篇
iOS 編寫高質量Objective-C程式碼(七)
奇舞週刊

相關文章