UI, 猜数字游戏的实现

本文介绍了一款基于iOS平台的数字猜谜游戏的开发过程,包括使用Swift语言实现随机数生成、用户输入验证、反馈提示及游戏结束逻辑。重点讨论了如何利用Swift的特性优化用户体验,以及如何通过简单的条件判断实现游戏逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

游戏规则:

每次随机给lable赋值一个数, 然后再向输入框输入0-10的数, 判断是否和lable里的数字相同, 相同的话会弹出提示框, 总共有三次机会


#import "RootViewController.h"

//遵守协议<UIAlertViewDelegate>

@interface RootViewController ()<UIAlertViewDelegate> {

    //定义几个全局变量

    UILabel *lable;

    UITextField *textField;

    UIButton *button;

    NSInteger random;

    NSInteger count;

}


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.891 blue:0.636 alpha:1.000];

    

    //创建一个lable

    lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 175, 175)];

    lable.backgroundColor = [UIColor colorWithRed:1.000 green:0.217 blue:0.858 alpha:1.000];

    lable.font = [UIFont boldSystemFontOfSize:150];

    lable.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:lable];

    [lable release];

    

    //创建一个textField

    textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 300, 275, 50)];

    textField.placeholder = @"请输入0-10的数";

    textField.borderStyle = UITextBorderStyleRoundedRect;

    textField.keyboardType = UIKeyboardTypeNumberPad;

    [self.view addSubview:textField];

    [textField release];

    

    //创建一个button

    button = [UIButton buttonWithType:UIButtonTypeSystem];

    [button setTitle:@"确认" forState:UIControlStateNormal];

    button.frame = CGRectMake(50, 400, 275, 50);

    button.titleLabel.font = [UIFont boldSystemFontOfSize:40];

    [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    [self reset];

    

}


//重置方法(把一些数据重置, 便于游戏重新开始)


- (void)reset {

    

    count = 3;

    lable.text = @"?";

    random = arc4random() % 11;

    textField.text = nil;

    [textField resignFirstResponder];//

    

    

}


//button的关联方法

- (void)pressButton:()button {

    //将随机数装化为字符串

    NSString *title = [NSString stringWithFormat:@"%ld", random];

    //textField.text转化为整型值

    NSInteger num = [textField.text integerValue];

    //判断是否输入了数字, 如果没有弹出提示框

    if ([textField.text length] == 0) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"在输入框中输入0-10的数" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];

        [alertView show];

        [alertView release];

    }

    

    //判断输入数字是否在0-10之间

    if (num > 10 || num < 0) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@", 请输入0-10的数哦" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];

        [alertView show];

        [alertView release];

    }

    

    //对输入的数与随机数进行大小的判断, 生成提示语

    NSString *str = [NSString stringWithFormat:@"你输入的数字%@", num > random ? @"太大了" : @"太小了"];

    

    //判断是否用完了三次机会

    if (count == 0) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"很遗憾" message:@"您的机会用完了" delegate:self cancelButtonTitle:@"再试一次吧" otherButtonTitles: nil];

        [alertView show];

        [alertView release];

        textField.text = title;

        

    }else {

        //判断是否猜对了

        if ([title isEqualToString:textField.text]) {

            //猜对之后的弹框

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"恭喜你" message:@"猜对了" delegate:self cancelButtonTitle:@"再来一次吧" otherButtonTitles: nil];

            [alertView show];

            [alertView release];

            textField.text = title;

        } else {

            //猜错的弹框

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"很遗憾" message:str delegate:nil cancelButtonTitle:@"再来一次吧" otherButtonTitles: nil];

            [alertView show];

            [alertView release];

            //每猜一次, count - 1

            count--;

            

        }


    }

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



#pragma mark - UIAlertViewDelegate


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    [self reset];

}



@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值