博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 小游戏项目——数字速算升级版
阅读量:7301 次
发布时间:2019-06-30

本文共 7698 字,大约阅读时间需要 25 分钟。

级别: ★☆☆☆☆

标签:「iOS」「小游戏项目」「数字速算」
作者:
审校:

前言:最近公司部门在组织团建,需要准备准备两个团建小游戏, 分别是“数字速算升级版”和。

小编琢磨了一下,发现这个两个小项目很适合入门iOS,故这篇文章诞生了。 本篇将介绍 iOS 小游戏项目——数字速算升级版。 希望通过这篇文章,能够帮助对iOS感兴趣的同学快速入门iOS。

效果图如下:

一、项目需求

  1. UI层面: 8个Label,3个Button。

图解:

  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(微信公众号)

推荐文章:

转载地址:http://jsinm.baihongyu.com/

你可能感兴趣的文章
NSTimer解除循环引用
查看>>
WPF 插拔触摸设备触摸失效
查看>>
MongoDB工具最新进展
查看>>
android图像处理系列之七--图片涂鸦,水印-图片叠加(转载)
查看>>
扩展Asterisk1.8.7的Dialplan Applications
查看>>
超异类的“无用类”网站导航网站 - theuselessweb.com
查看>>
[置顶] Android仿人人客户端(v5.7.1)——应用主界面之左侧面板UI实现
查看>>
情况动态规划CodeForces Round #179 (296B) - Yaroslav and Two Strings
查看>>
配置Erlang shell的工作路径
查看>>
Unity Built-in Shader详解二
查看>>
[置顶] 使用sping AOP 操作日志管理
查看>>
QRadioButton类中Toggled()信号的使用方法
查看>>
权限管理之基于ACL的实现:自定义JSTL函数实现即时认证
查看>>
svn删除项目目录
查看>>
机器学习 —— 概率图模型(推理:连续时间模型)
查看>>
ODI Studio拓扑结构的创建与配置(Oracle)
查看>>
form、iframe实现异步上传文件
查看>>
获取Web.config配置节
查看>>
膝盖中了一箭之康复篇-第十个月暨4月份目标总结
查看>>
【swift学习笔记】一.页面转跳的条件判断和传值
查看>>