前言:最近公司部門在組織團建,須要準備準備兩個團建小遊戲, 分別是「數字速算升級版」和「你話我猜升級版」。git
小編琢磨了一下,發現這個兩個小項目很適合入門iOS,故這篇文章誕生了。 本篇將介紹 iOS 小遊戲項目——數字速算升級版。 但願經過這篇文章,可以幫助對iOS感興趣的同窗快速入門iOS。github
效果圖以下:算法
圖解: bash
邏輯層面:微信
0
開始計時,直到遊戲結束,查看遊戲時長。難題機率:dom
項目中,我選擇的storyboard。獨立開發時,用storyboard比較高效。ui
@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
複製代碼
@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;
}
複製代碼
最後,工程源碼:遊戲源碼atom
關注咱們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公衆號)spa
推薦文章:
iOS 繪製漸變·基礎篇
iOS 繪製漸變·實例篇
iOS 編寫高質量Objective-C代碼(七)
奇舞週刊