【非凡程序员】 OC第十六节课 初识IOS开发四 (九宫格游戏)
【非凡程序员】 OC第十六节课 初识IOS开发四 (九宫格游戏)
七月流火zsp 发表于3年前
【非凡程序员】 OC第十六节课 初识IOS开发四 (九宫格游戏)
  • 发表于 3年前
  • 阅读 19
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 非凡程序员,感谢 黄杨超 IOS指导。总结:张岁平学习笔记

九宫格游戏:先是定义一个二维数组,写出正确的九宫格,然后随机的交换横行的两组,在随机隐藏几个数据。让用户输入数据,再进行判断是否正确

ViewController.h文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)DaWan:(id)sender;

@property (weak, nonatomic) IBOutlet UITextField *ShengFu;
@property (weak, nonatomic) IBOutlet UITextField *Number9;
@property (weak, nonatomic) IBOutlet UITextField *Number8;
@property (weak, nonatomic) IBOutlet UITextField *Number7;
@property (weak, nonatomic) IBOutlet UITextField *Number6;
@property (weak, nonatomic) IBOutlet UITextField *Number5;
@property (weak, nonatomic) IBOutlet UITextField *Number4;
@property (weak, nonatomic) IBOutlet UITextField *Number3;
@property (weak, nonatomic) IBOutlet UITextField *Number2;
@property (weak, nonatomic) IBOutlet UITextField *Number1;
@property (nonatomic,strong) NSMutableArray *finish; //定义全局变量
- (IBAction)Begin:(id)sender;

@end

ViewController.m文件:

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)Begin:(id)sender {
   
    _ShengFu.text=@"祝你好运";

    NSArray *a1=@[@[@"1",@"2",@"3"],[@[@"3",@"1",@"2"],@[@[@"2",@"3",@"1"]];    

    NSMutableArray * a2 = [NSMutableArray arrayWithArray:a1];

    NSArray * temp = @[];
    int number1=arc4random()%3;
    int number2=arc4random()%3;
    

  //交换横行随机两组

    temp=a2[number1];
    a2[number1]=a2[number2];
    a2[number2]=temp;
    _finish=a2;

    NSArray *array = @[_Number1, _Number2,_Number3,_Number4,_Number5, _Number6,_Number7, _Number8,_Number9];
   
    int flag=0;
    for (int i=0; i < [a2 count]; i++)
    {
        for (int j=0; j < [a2[i] count]; j++)
        {
            UITextField *t=array[flag];  //定义 UITextField类型的t,把a2的值一次给t

            t.text=a2[i][j];
            flag++;
        }
    }
    for (int n=0; n<7; n++)
    {
        int sui=arc4random()%9; 
        UITextField *s=array[sui];
        s.text=@"";  //随机隐藏不输出几个数据
    }
   
    }


- (IBAction)DaWan:(id)sender {        //判断用户输入是否正确
   
    int flag3=0;
   
   NSArray *array = @[@[_Number1, _Number2,_Number3],@[_Number4,_Number5, _Number6],@[_Number7, _Number8,_Number9]];
    for( int i = 0; i <= 2; i++ )
    {
        for( int j = 0; j <= 2; j++ )
        {
            UITextField * text = array[i][j];
            if (![text.text isEqualToString: _finish[i][j]])
            {
                flag3 = 1;
                break;
            }
           
        }
    }

    if (flag3==1)
    {
        _ShengFu.text=@"你输了";
        flag3=0 ;
    }
    else
    {
        _ShengFu.text=@"你赢了";
    }
   
}

@end

标签: 非凡程序员
共有 人打赏支持
粉丝 10
博文 29
码字总数 11273
×
七月流火zsp
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: