oc 实现五子棋游戏
2016-06-05 19:23
330 查看
思路:第一步,绘制棋盘
第二步,将每一个交点设置为一个button
第三部,模拟黑白子字走棋,(第一次点击为白子,第二次为黑子)
第三部,记录黑白子坐标,判断五个连城一线的为获胜方
运行结果:
第二步,将每一个交点设置为一个button
第三部,模拟黑白子字走棋,(第一次点击为白子,第二次为黑子)
第三部,记录黑白子坐标,判断五个连城一线的为获胜方
#import "AppDelegate.h" @implementation AppDelegate static int num=0; -(id)init { if (self=[super init]) { _blackChess=[[NSMutableArray alloc]init]; _whiteChess=[[NSMutableArray alloc]init]; _sumPosArray=[[NSMutableArray alloc]init]; _SubArray=[[NSMutableArray alloc]init]; _sumPosForBlackArray=[[NSMutableArray alloc]init]; } return self; } //创建棋盘 -(void)creatChessBoard { for (int i=0; i<8; i++) { for (int j=0; j<8; j++) { UIView*view1=[[UIView alloc]init]; view1.frame=CGRectMake(40*j, 40*i+20, 39, 39); if ((i+j)%2) { view1.backgroundColor=[UIColor brownColor]; } else { view1.backgroundColor=[UIColor brownColor]; } [self.window addSubview:view1]; } } } //放置控制按钮 -(void)creatChess { for (int i=0; i<9; i++) { for (int j=0; j<9; j++) { UIButton* btn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.backgroundColor=[UIColor cyanColor]; //在每个交点上创建一个button, btn.frame=CGRectMake(40*j-10, 40*i+10, 20,20); btn.alpha=0.1f;//把button的透明度设置为0 btn.tag=100+i+j; //为每一个button添加一个事件 [btn addTarget:self action:@selector(pressButon:) forControlEvents:UIControlEventTouchUpInside]; [self.window addSubview:btn]; } } } //放置棋子 -(void)pressButon:(UIButton*)btn { if (num%2==0&&btn.tag) { UIButton*chessBtn=btn; chessBtn.backgroundColor=[UIColor blackColor]; chessBtn.alpha=1.0f; [_blackChess addObject:chessBtn]; [self judgeBlackChess]; btn.tag=0; num++; [self.window addSubview:chessBtn]; } if (num%2&&btn.tag) { UIButton*chessBtn=btn; chessBtn.backgroundColor=[UIColor whiteColor]; chessBtn.alpha=1.0f; [_whiteChess addObject:chessBtn]; [self judgeWhiteChess]; btn.tag=0; num++; [self.window addSubview:chessBtn]; } } //开始判断白棋 -(void)judgeWhiteChess { for (int i=0; i<_whiteChess.count; i++) { UIButton*btn=_whiteChess[_whiteChess.count-1]; int xPos=btn.center.x/40; int yPos=(btn.center.y-20)/40; int sumPos=xPos+yPos; [_sumPosArray addObject:[NSNumber numberWithInt:sumPos]]; break; } if (_sumPosArray.count>=5) { [self subPosArray]; } } -(void)subPosArray { for (int i=_sumPosArray.count-5; i<_sumPosArray.count-1; i++) { [_SubArray addObject:_sumPosArray[i]]; for (int j=_sumPosArray.count-5; j<_sumPosArray.count-i-1 ; j++) { if ([_sumPosArray[j] compare:_sumPosArray[j+1]]==NSOrderedDescending) { [_sumPosArray exchangeObjectAtIndex:j withObjectAtIndex:j+1]; } } } for (int i=_sumPosArray.count-5; i<=_sumPosArray.count-1; i++) { NSLog(@"%d",[_sumPosArray[i]intValue]); } for (int i=_sumPosArray.count-4; i<=_sumPosArray.count-1; i++) { //[_SubArray addObject:_sumPosArray[i]]; static int count1=0; static int count2=0; static int count3=0; int val=[_sumPosArray[i]intValue]-[_sumPosArray[i-1]intValue]; if (val==1) { count1++; if (count1==4) { NSLog(@"白旗赢"); [self winerArlertView]; break; } } else if (val==2) { count2++; if (count2==4) { NSLog(@"白棋赢"); [self winerArlertView]; break; } } else if(val==0) { count3++; if (count3==4) { NSLog(@"白棋赢"); [self winerArlertView]; break; } } } } //开始判断黑棋 -(void)judgeBlackChess { for (int i=0; i<_blackChess.count; i++) { UIButton*btn=_blackChess[_blackChess.count-1]; int xPos=btn.center.x/40; int yPos=(btn.center.y-20)/40; int sumPos=xPos+yPos; [_sumPosForBlackArray addObject:[NSNumber numberWithInt:sumPos]]; break; } if (_sumPosForBlackArray.count>=5) { [self subPosArrayForBalck]; } } -(void)subPosArrayForBalck { for (int i=_sumPosForBlackArray.count-5; i<_sumPosForBlackArray.count-1; i++) { [_SubArray addObject:_sumPosForBlackArray[i]]; for (int j=_sumPosForBlackArray.count-5; j<_sumPosForBlackArray.count-i-1 ; j++) { if ([_sumPosForBlackArray[j] compare:_sumPosForBlackArray[j+1]]==NSOrderedDescending) { [_sumPosForBlackArray exchangeObjectAtIndex:j withObjectAtIndex:j+1]; } } } for (int i=_sumPosForBlackArray.count-5; i<=_sumPosForBlackArray.count-1; i++) { NSLog(@"%d",[_sumPosForBlackArray[i]intValue]); } for (int i=_sumPosForBlackArray.count-4; i<=_sumPosForBlackArray.count-1; i++) { //[_SubArray addObject:_sumPosArray[i]]; static int count1=0; static int count2=0; static int count3=0; int val=[_sumPosForBlackArray[i]intValue]-[_sumPosForBlackArray[i-1]intValue]; if (val==1) { count1++; if (count1==4) { NSLog(@"黑旗赢"); [self winerArlertView]; break; } } else if (val==2) { count2++; if (count2==4) { NSLog(@"黑棋赢"); [self winerArlertView]; break; } } else if(val==0) { count3++; if (count3==4) { NSLog(@"黑棋赢"); [self winerArlertView]; break; } } } } //赢棋弹出提示 -(void)winerArlertView { UIAlertView*alertView=[[UIAlertView alloc] initWithTitle:@"提示" message:@"赢棋" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil]; [alertView show]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. [self creatChessBoard]; [self creatChess]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
运行结果:
相关文章推荐
- 嵌入式c代码规范
- 什么是RESTful
- 【Leetcode】Longest Common Prefix
- 排序算法的 时间复杂度 和 空间复杂度
- 作业:C++作业7
- C语言的流函数
- SDK 第一章_起步
- 计算机专业中经典书籍(程序猿和大学生必读)
- C++实验7-最大公约数和最小公倍数
- Android--color.xml的值
- 【Leetcode】Implement strStr()
- SSH 集合框架应用实例
- 物理扇区和逻辑扇区(相对扇区)的关系
- 【菜鸟学EF】——初识EF
- JAVA基础-----------JVM内存分析之HelloWorld.java
- 【Leetcode】Valid Palindrome
- 【阅读笔记】移动APP测试实战--第一章
- 后缀C和c的区别
- 继承练习题Collection with Inheritance
- CocoaPods 安装和使用