自己写的..所以可能没什么逻辑性...可能特别水...
环境为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