UI, 猜数字游戏的实现
来源:互联网 发布:sql查询语句 编辑:程序博客网 时间:2024/05/19 14:00
游戏规则:
每次随机给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
- UI, 猜数字游戏的实现
- UI中简单的猜数字游戏
- 猜数字游戏的实现
- 猜数字游戏的实现
- 猜数字游戏的实现
- 猜数字游戏实现
- 实现猜数字游戏
- 用java实现的猜数字游戏
- 实现控制台的猜数字游戏。
- JAVA实现简单的猜数字游戏
- UI, 关灯游戏的实现
- struts2实现猜数字游戏
- 随机数实现猜数字游戏
- python实现猜数字游戏
- Go实现猜数字游戏
- 猜数字的游戏
- erlang实现的经典的猜数字游戏
- 女朋友用Python实现的猜数字游戏:)
- RC4加密算法实践
- 如何关闭窗体、线程、应用程序?
- C++ 下的GDI+绘图初始配置
- [POJ 1980] Unit Fraction Partition Dfs
- scala第78讲:Type与Class实战详解
- UI, 猜数字游戏的实现
- 14. 调整数组顺序使奇数位于偶数前面
- 设置一个简单的安卓启动页面
- 欢迎使用CSDN-markdown编辑器
- 代理作用之一代理传值:正向传值
- Android View(一)
- 一个大牛的acm历程(看着就要颤抖)
- 编写简单字符设备驱动
- 大犇心得