UI, 猜数字游戏的实现
2015-08-08 21:42
316 查看
游戏规则:
每次随机给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
每次随机给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
相关文章推荐
- UITableView
- 【Android UI】ViewPage
- leetcode - Unique Paths II
- UIStepper
- UIScrollView 按页面滚动视图 UIPageControl
- POJ2478 Farey Sequence(欧拉函数,打表)
- Pixel Width of the text in a UILabel
- UITableView省市区字典
- UI05_Control(UIStepper加减音量/UIBlurEffect毛玻璃/UISlider滑动/UISegmentedControl小模块)
- [leetcode] Implement Queue using Stacks
- UIButton的一些常用知识点
- [leetcode 234]Implement Queue using Stacks
- UVA 10420 List of Conquests
- UIViewController的生命周期
- UIViewController
- opencv开发之MFC的.SetWindowTextA() + OnTimer(UINT_PTR nIDEvent) +cvNamedWindow 在MFC上显示
- UI中简单的猜数字游戏
- UI, 关灯游戏的实现
- Longest Consecutive Sequence
- android自定义UI模板图文详解