oc 实现五子棋游戏
来源:互联网 发布:淘宝店怎么弄淘客 编辑:程序博客网 时间:2024/05/20 23:57
思路:第一步,绘制棋盘
第二步,将每一个交点设置为一个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;}
运行结果:
0 0
- oc 实现五子棋游戏
- Gomoku五子棋OC实现
- C++实现五子棋游戏
- 自定义View实现五子棋游戏
- C# 控制台实现的五子棋游戏
- js实现的简单五子棋游戏
- Android五子棋游戏设计与实现
- 单机版五子棋游戏项目的实现
- js+canvas实现简单的五子棋游戏
- JAVA实现简单控制台五子棋游戏
- 原生JS+Canvas实现五子棋游戏实例
- 五子棋游戏
- 五子棋游戏
- 五子棋游戏
- 五子棋游戏
- 五子棋游戏
- 五子棋游戏
- Android实现五子棋游戏(一) 游戏基本逻辑
- 数据库的两种CRUD和事务,listview
- 客户端访问https站点(自定义证书)--续
- Fatal error: Class 'GearmanClient' not found解决方法
- iOS算法篇(一)快速排序算法
- 【hibernate进阶】hibernate
- oc 实现五子棋游戏
- 2016计蒜之道初赛第三场----百度的科学计算器【模拟】
- 解决TCP网络传输“粘包”问题
- 丢失DLL文件,并非全都下载相关DLL就能解决的,千万别被百度误人子弟
- 快速搭建Web环境 Angularjs + Express3 + Bootstrap3
- header('Content-type:text/html;charset = utf-8');出现中文乱码
- C++第七次实验--素数和回文
- 实验:C++实验7 项目二
- 2.数据结构--线性表之链表实现