[某鸥实训记][objective-c][第二天][作业]黑白棋+关灯游戏

自己写的..所以可能没什么逻辑性...可能特别水...

环境为ios SDK8.0 选择的Simulator是iPhone6

创建ios SingleViewApplication..然后再ViewController.m中的代码就是全部了

  1 //
  2 //  ViewController.m
  3 //  黑白棋0908
  4 //
  5 //  Created by ******* on 15/9/8.
  6 //  Copyright (c) 2015年 *******. All rights reserved.
  7 //
  8
  9 #import "ViewController.h"
 10
 11 @interface ViewController ()
 12 @property NSInteger stepCounter;
 13 @property UIButton *flagButton;
 14 @end
 15
 16 @implementation ViewController
 17
 18
 19
 20 - (void)viewDidLoad {
 21     [super viewDidLoad];
 22     _flagButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 23     _flagButton.frame = CGRectMake(17, 35, 30, 30);
 24     _flagButton.backgroundColor = [UIColor blackColor];
 25     [_flagButton.layer setCornerRadius:15];
 26     [self.view addSubview:_flagButton];
 27     _stepCounter = 0;
 28     for(int i=0; i<8;i++){
 29         for(int j=0;j<8;j++){
 30             self.view.backgroundColor = [UIColor grayColor];
 31             UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 32             button.backgroundColor = [UIColor darkGrayColor];
 33             [button.layer setCornerRadius:20.5];
 34             button.frame = CGRectMake(14+44*i, 30+44*(j+1), 41, 41);//
 35             [button addTarget:self action:@selector(heibai:) forControlEvents:UIControlEventTouchUpInside];
 36             button.tag = 10000+1000*(j+1)+10*(i+1);
 37             UIButton *tempButton;
 38             tempButton = (UIButton *)[self.view viewWithTag:14040];
 39             tempButton.backgroundColor = [UIColor blackColor];
 40             [tempButton setEnabled:NO];
 41             tempButton = (UIButton *)[self.view viewWithTag:15050];
 42             tempButton.backgroundColor = [UIColor blackColor];
 43             [tempButton setEnabled:NO];
 44             tempButton = (UIButton *)[self.view viewWithTag:14050];
 45             tempButton.backgroundColor = [UIColor whiteColor];
 46             [tempButton setEnabled:NO];
 47             tempButton = (UIButton *)[self.view viewWithTag:15040];
 48             tempButton.backgroundColor = [UIColor whiteColor];
 49             [tempButton setEnabled:NO];
 50             [self.view addSubview:button];
 51         }
 52     }
 53 }
 54
 55
 56
 57
 58 - (void)heibai:(UIButton *)sender{
 59     NSInteger changeNum = 0;
 60     NSInteger side = 0;
 61     NSInteger tag = sender.tag;
 62     UIButton *tempButton;
 63     NSInteger breakFlag[8] = {0};
 64     UIColor *color;
 65     UIColor *inverseColor;
 66
 67     if(_stepCounter%2==0){
 68         color = [UIColor blackColor];
 69         inverseColor = [UIColor whiteColor];
 70     }else{
 71         color = [UIColor whiteColor];
 72         inverseColor = [UIColor blackColor];
 73     }
 74     //右
 75     tempButton = (UIButton *)[self.view viewWithTag:tag+10];
 76     if(tempButton.enabled)
 77         breakFlag[0]++;
 78     if(tempButton.backgroundColor == color)
 79         breakFlag[0]++;
 80     if(tempButton.backgroundColor == inverseColor)
 81         side ++;
 82     //右下
 83     tempButton = (UIButton *)[self.view viewWithTag:tag+1010];
 84     if(tempButton.enabled)
 85         breakFlag[1]++;
 86     if(tempButton.backgroundColor == color)
 87         breakFlag[1]++;
 88     if(tempButton.backgroundColor == inverseColor)
 89         side ++;
 90     //下
 91     tempButton = (UIButton *)[self.view viewWithTag:tag+1000];
 92     if(tempButton.enabled)
 93         breakFlag[2]++;
 94     if(tempButton.backgroundColor == color)
 95         breakFlag[2]++;
 96     if(tempButton.backgroundColor == inverseColor)
 97         side ++;
 98     //左下
 99     tempButton = (UIButton *)[self.view viewWithTag:tag+990];
100     if(tempButton.enabled)
101         breakFlag[3]++;
102     if(tempButton.backgroundColor == color)
103         breakFlag[3]++;
104     if(tempButton.backgroundColor == inverseColor)
105         side ++;
106     //左
107     tempButton = (UIButton *)[self.view viewWithTag:tag-10];
108     if(tempButton.enabled)
109         breakFlag[4]++;
110     if(tempButton.backgroundColor == color)
111         breakFlag[4]++;
112     if(tempButton.backgroundColor == inverseColor)
113         side ++;
114     //左上
115     tempButton = (UIButton *)[self.view viewWithTag:tag-1010];
116     if(tempButton.enabled)
117         breakFlag[5]++;
118     if(tempButton.backgroundColor == color)
119         breakFlag[5]++;
120     if(tempButton.backgroundColor == inverseColor)
121         side ++;
122     //上
123     tempButton = (UIButton *)[self.view viewWithTag:tag-1000];
124     if(tempButton.enabled)
125         breakFlag[6]++;
126     if(tempButton.backgroundColor == color)
127         breakFlag[6]++;
128     if(tempButton.backgroundColor == inverseColor)
129         side ++;
130     //右上
131     tempButton = (UIButton *)[self.view viewWithTag:tag-990];
132     if(tempButton.enabled)
133         breakFlag[7]++;
134     if(tempButton.backgroundColor == color)
135         breakFlag[7]++;
136     if(tempButton.backgroundColor == inverseColor)
137         side ++;
138     //如果边上什么子都没有就退出
139     if(0 == side)
140         return;
141
142
143     //判断能不能吃子
144     for(int a=0;a<7;a++){
145         NSInteger temp = breakFlag[a];
146         NSLog(@"%li,%li.%li",(long)temp,(long)a,(long)_stepCounter);
147     }
148     changeNum += [self judge:sender :breakFlag];
149
150     if(changeNum==0)
151         return;
152     sender.backgroundColor = color;
153     [sender setEnabled:NO];
154     _stepCounter++;
155
156
157     //修改提示标记
158     if(_stepCounter%2==0){
159         _flagButton.backgroundColor = [UIColor blackColor];
160     }else{
161          _flagButton.backgroundColor = [UIColor whiteColor];
162     }
163
164     //计算是否胜利?..
165     NSInteger whiteNum = 0;
166     NSInteger blackNum = 0;
167     for(int i=0; i<8;i++){
168         for(int j=0;j<8;j++){
169             UIButton *button;
170             button = (UIButton *)[self.view viewWithTag:10000+1000*(j+1)+10*(i+1)];
171             if(button.backgroundColor == [UIColor whiteColor])
172                 whiteNum++;
173             if(button.backgroundColor == [UIColor blackColor])
174                 blackNum++;
175         }
176     }
177
178
179
180     if(whiteNum == 0){
181         NSLog(@"Black Win");
182         UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"结果" message:@"BLACK WIN" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
183         [alter show];
184         [self viewDidLoad];
185     }else if(blackNum == 0){
186         NSLog(@"White Win");
187         UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"结果" message:@"WHITE WIN" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
188         [alter show];
189         [self viewDidLoad];
190
191
192     }
193
194 }
195
196
197 - (NSInteger)judge:(UIButton *)sender :(NSInteger[]) breakFlag{
198     NSInteger changeNum = 0;
199     NSInteger tag = sender.tag;
200     UIButton *tempButton;
201     UIColor *color;
202
203     if(_stepCounter%2==0){
204         color = [UIColor blackColor];
205     }else{
206         color = [UIColor whiteColor];
207     }
208
209     for(int a=0;a<8;a++){
210         NSInteger temp = breakFlag[a];
211         NSLog(@"%li,%li.%li",(long)temp,(long)a,(long)_stepCounter);
212     }
213     for (int i=0; i<6; i++) {
214         //右
215         tempButton = (UIButton *)[self.view viewWithTag:tag+20+i*10];
216         if(tempButton.enabled)
217             breakFlag[0]++;
218         if (tempButton.backgroundColor == color) {
219             if(breakFlag[0]<=0){
220                 changeNum += [self change:_stepCounter%2 :i+1 :tag :10 :0];
221             }
222         }
223
224         //右下
225         tempButton = (UIButton *)[self.view viewWithTag:tag+2020+i*1010];
226         if(tempButton.enabled)
227             breakFlag[1]++;
228         if (tempButton.backgroundColor == color) {
229             if(breakFlag[1]<=0){
230                 changeNum += [self change:_stepCounter%2 :i+1 :tag :1010 :0];
231             }
232         }
233
234         //下
235         tempButton = (UIButton *)[self.view viewWithTag:tag+2000+i*1000];
236         if(tempButton.enabled)
237             breakFlag[2]++;
238         if (tempButton.backgroundColor == color) {
239             if(breakFlag[2]<=0){
240                 changeNum += [self change:_stepCounter%2 :i+1 :tag :1000 :0];
241             }
242         }
243
244         //左下
245         tempButton = (UIButton *)[self.view viewWithTag:tag+1980+i*990];
246         if(tempButton.enabled)
247             breakFlag[3]++;
248         if (tempButton.backgroundColor == color) {
249             if(breakFlag[3]<=0){
250                 changeNum += [self change:_stepCounter%2 :i+1 :tag :990 :0];
251             }
252         }
253
254         //左
255         tempButton = (UIButton *)[self.view viewWithTag:tag-20-i*10];
256         if(tempButton.enabled)
257             breakFlag[4]++;
258         if (tempButton.backgroundColor == color) {
259             if(breakFlag[4]<=0){
260                 changeNum += [self change:_stepCounter%2 :i+1 :tag :10 :1];
261             }
262         }
263
264         //左上
265         tempButton = (UIButton *)[self.view viewWithTag:tag-2020-i*1010];
266         if(tempButton.enabled)
267             breakFlag[5]++;
268         if (tempButton.backgroundColor == color) {
269             if(breakFlag[5]<=0){
270                 changeNum += [self change:_stepCounter%2 :i+1 :tag :1010 :1];
271             }
272
273         }
274
275         //上
276         tempButton = (UIButton *)[self.view viewWithTag:tag-2000-i*1000];
277         if(tempButton.enabled)
278             breakFlag[6]++;
279         if (tempButton.backgroundColor == color) {
280             if(breakFlag[6]<=0){
281                 changeNum += [self change:_stepCounter%2 :i+1 :tag :1000 :1];
282             }
283         }
284
285         //右上
286         tempButton = (UIButton *)[self.view viewWithTag:tag-1980-i*990];
287         if(tempButton.enabled)
288             breakFlag[7]++;
289         if (tempButton.backgroundColor == color) {
290             if(breakFlag[7]<=0){
291                 changeNum += [self change:_stepCounter%2 :i+1 :tag :990 :1];
292             }
293         }
294     }
295     return changeNum;
296 };
297
298
299 - (NSInteger)change:(NSInteger)color :(NSInteger)time :(NSInteger)tag :(NSInteger)step :(NSInteger)type{
300     //颜色0黑1白 type0+1-
301     NSInteger changeNum = 0;
302     UIButton *tempButton;
303     for(NSInteger i=0;i<time;i++){
304         if(type==0){
305             tempButton = (UIButton *)[self.view viewWithTag:tag+step+i*step];
306         }else{
307             tempButton = (UIButton *)[self.view viewWithTag:tag-step-i*step];
308         }
309         if(color==0){
310             if(tempButton.backgroundColor == [UIColor whiteColor]){
311                 tempButton.backgroundColor = [UIColor blackColor];
312                 changeNum++;
313 //                NSInteger newBreak[8] = {0};
314  //               [self judge:tempButton :newBreak];
315             }
316         }else{
317             if(tempButton.backgroundColor == [UIColor blackColor]){
318                 tempButton.backgroundColor = [UIColor whiteColor];
319                 changeNum++;
320 //                NSInteger newBreak[8] = {0};
321 //                [self judge:tempButton :newBreak];
322             }
323         }
324     }
325     return changeNum;
326 }
327
328 - (void)didReceiveMemoryWarning {
329     [super didReceiveMemoryWarning];
330     // Dispose of any resources that can be recreated.
331 }
332
333 @end

最后注释掉的是递归的判断是否可变色...然而并不太清楚黑白棋的规则...所以注释掉了

后边是那个关灯游戏..

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    for(int i=0; i<5;i++){
        for(int j=0;j<5;j++){
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//            button.backgroundColor = [UIColor blackColor];
            [button setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateSelected];
            button.frame = CGRectMake(55*(i+1), 30+55*(j+1), 50, 50);
            [button addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 100+10*(j+1)+i;
            [self.view addSubview:button];
        }
    }
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)change:(UIButton *)sender{
    NSInteger num = sender.tag;
    UIButton *button = (UIButton *)[self.view viewWithTag:num];
    button.selected = !button.selected;
    NSInteger num2 = num-10;
    button = (UIButton *)[self.view viewWithTag:num2];
    button.selected = !button.selected;
    num2 = num+1;
    button = (UIButton *)[self.view viewWithTag:num2];
    button.selected = !button.selected;
    num2 = num-1;
    button = (UIButton *)[self.view viewWithTag:num2];
    button.selected = !button.selected;
    num2 = num+10;
    button = (UIButton *)[self.view viewWithTag:num2];
    button.selected = !button.selected;

}

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

@end

使用的两张图片就是一张开灯一张关灯的...

时间: 2024-10-24 11:30:27

[某鸥实训记][objective-c][第二天][作业]黑白棋+关灯游戏的相关文章

[某鸥实训记][objective-c][第三天][作业]打地鼠X2

#import "ViewController.h" @interface ViewController () @property (nonatomic,strong) NSTimer *timer; @property (nonatomic,strong) NSTimer *mouseTimer; @property (nonatomic,strong) NSMutableArray *arr; @property (nonatomic,strong) NSMutableArray

[某鸥实训记][objective-c][第二天][个人笔记]

今天学到了几种传值方式...直接粘在课上做的笔记了 不知道叫什么的用变量来传值 FirstViewController *firstVC = [[FirstViewController alloc] init]; firstVC.str = _textField.text; [self.navigationController pushViewController:firstVC animated:YES]; 单例传值 //SingleTon.m static SingleTon *ton =

[某鸥实训记][objective-c][第五天][个人笔记]

TableView+ScrollView 往cell里加label 解决重影的办法 重用cell的时候删除元素 NSArray *subViews = cell.subviews; for (UIView *view in subViews) { [view removeFromSuperview]; } 在if(cell==nil)中去定义子视图,加上tag值.在外边通过tag值获取视图,修改值 封装一个自定义的cell类 //http://www.tuicool.com/articles/b

[某鸥实训记][objective-c][第三天][个人笔记]

..还是粘的课上做的笔记 //WebView+Image Animation+Timer //UIBarButton UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemSearch target:self action:@selector(search:)]; self.navigationItem.rightBarButtonItem = bar;

[某鸥实训记][objective-c][第四天][个人笔记]

老师上午请假了....然后因为有好多人之前的作业没写出来...所以安排写作业....然后我就划水了一上午.... 下午回来了...带着做了一遍那个打地鼠....... 所以,..今天没啥东西... TableViewController UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 设置什么什么重用标识符 if(cell == n

[某鸥实训记][objective-c][第一天][个人笔记]

UIView,UILabel,UITextField,ViewController..... 然而上面这些都并没太搞懂...... 下面是学到的东西=w= //一边百度一边学的..所以很多东西都来自别的blog..后边会贴出出处 .frame :控件相对于父视图的位置 UITextField.borderStyle :UITextField的样式 .text :控件显示的文本 .backgroundColor :控件的背景颜色(然而设置了这个会让控件的某些属性无效) UIColor :一个颜色的

[某鸥实训记][objective-c][第六天][个人笔记]

- -..抓了个请求什么什么包的网址...然后发了个什么什么请求...   用了个叫paros的工具 ip设置成电脑的..设置下端口...然后把移动设备的HTTP代理设置成一样的就能抓了=w= 抓完了以后..又用了个什么什么包...往刚才抓的网址发了个请求..然后接收了一下Responce.. //PictureURLRequest.h #import <Foundation/Foundation.h> @protocol PictureURLRequestDelegate <NSObj

[某鸥实训记][objective-c][第七天][个人笔记]

在ScrollView里加子界面 ..直接上代码了 self.navigationController.navigationBar.translucent = NO; FirstTableViewController *firstVC = [[FirstTableViewController alloc] init]; SecondTableViewController *secondVC = [[SecondTableViewController alloc] init]; ThirdTabl

小项目实训:曹操外卖第二部分

前面的后台1.INSERT goods VALUES('6','大盘鸡','7.00','0.75','6','6','4','挺辣的','4');/外卖基本信息录入/2.UPDATE goods SET title='烧饼' WHERE goodsld='1';/外卖信息修改/3.UPDATE goods SET discount='0.50' WHERE goodsld='1';/外卖销售折扣设置/4.SELECT invoicecontent FROM orders WHERE invoi