- (void)drawBackground:(CGSize)size{
self.gridWidth = (size.width - 2 * kBoardSpace) / self.gridCount;
//1.开启图像上下文
UIGraphicsBeginImageContext(size);
//2.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 0.8f);
//3.1 画16条竖线
for (int i = 0; i <= self.gridCount; i ++) {
CGContextMoveToPoint(ctx, kBoardSpace + i * self.gridWidth , kBoardSpace);
CGContextAddLineToPoint(ctx, kBoardSpace + i * self.gridWidth , kBoardSpace + self.gridCount * self.gridWidth);
}
//3.1 画16条横线
for (int i = 0; i <= self.gridCount; i ++) {
CGContextMoveToPoint(ctx, kBoardSpace, kBoardSpace + i * self.gridWidth );
CGContextAddLineToPoint(ctx, kBoardSpace + self.gridCount * self.gridWidth , kBoardSpace + i * self.gridWidth);
}
CGContextStrokePath(ctx);
//4.获取生成的图片
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
//5.显示生成的图片到imageview
UIImageView * imageView = [[UIImageView alloc]initWithImage:image];
[self addSubview:imageView];
UIGraphicsEndImageContext();
}
//点击棋盘,下棋
- (void)tapBoard:(UITapGestureRecognizer *)tap{
CGPoint point = [tap locationInView:tap.view];
//计算下子的列号行号
NSInteger col = (point.x - kBoardSpace + 0.5 * self.gridWidth) / self.gridWidth;
NSInteger row = (point.y - kBoardSpace + 0.5 * self.gridWidth) / self.gridWidth;
NSString * key = [NSString stringWithFormat:@"%ld-%ld",col,row];
if (![self.chessmanDict.allKeys containsObject:key]) {
UIView * chessman = [self chessman];
chessman.center = CGPointMake(kBoardSpace + col * self.gridWidth, kBoardSpace + row * self.gridWidth);
[self addSubview:chessman];
[self.chessmanDict setValue:chessman forKey:key];
self.lastKey = key;
//检查游戏结果
[self checkResult:col andRow:row andColor:self.isBlack];
self.isBlack = !self.isBlack;
}
}
//判断是否大于等于五个同色相连
- (BOOL)checkResult:(NSInteger)col andRow:(NSInteger)row andColor:(BOOL)isBlack andDirection:(GmkDirection)direction{
if (self.sameChessmanArray.count >= 5) {
return YES;
}
UIColor * currentChessmanColor = [self.chessmanDict[[NSString stringWithFormat:@"%ld-%ld",col,row]] backgroundColor];
[self.sameChessmanArray addObject:self.chessmanDict[self.lastKey]];
switch (direction) {
//水平方向检查结果
case GmkHorizontal:{
//向前遍历
for (NSInteger i = col - 1; i > 0; i --) {
NSString * key = [NSString stringWithFormat:@"%ld-%ld",i,row];
if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor) break;
[self.sameChessmanArray addObject:self.chessmanDict[key]];
}
//向后遍历
for (NSInteger i = col + 1; i < kGridCount; i ++) {
NSString * key = [NSString stringWithFormat:@"%ld-%ld",i,row];
if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor) break;
[self.sameChessmanArray addObject:self.chessmanDict[key]];
}
if (self.sameChessmanArray.count >= 5) {
[self alertResult];
return YES;
}
[self.sameChessmanArray removeAllObjects];
}
break;
case GmkVertical:{
//向前遍历
for (NSInteger i = row - 1; i > 0; i --) {
NSString * key = [NSString stringWithFormat:@"%ld-%ld",col,i];
if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor) break;
[self.sameChessmanArray addObject:self.chessmanDict[key]];
}
//向后遍历
for (NSInteger i = row + 1; i < kGridCount; i ++) {
NSString * key = [NSString stringWithFormat:@"%ld-%ld",col,i];
if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor) break;
[self.sameChessmanArray addObject:self.chessmanDict[key]];
}
if (self.sameChessmanArray.count >= 5) {
[self alertResult];
return YES;
}
[self.sameChessmanArray removeAllObjects];
}
break;
case GmkObliqueDown:{
//向前遍历
NSInteger j = col - 1;
for (NSInteger i = row - 1; i >= 0; i--,j--) {
NSString * key = [NSString stringWithFormat:@"%ld-%ld",j,i];
if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor || j < 0) break;
[self.sameChessmanArray addObject:self.chessmanDict[key]];
}
//向后遍历
j = col + 1;
for (NSInteger i = row + 1 ; i < kGridCount; i++,j++) {
NSString * key = [NSString stringWithFormat:@"%ld-%ld",j,i];
if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor || j > kGridCount) break;
[self.sameChessmanArray addObject:self.chessmanDict[key]];
}
if (self.sameChessmanArray.count >= 5) {
[self alertResult];
return YES;
}
[self.sameChessmanArray removeAllObjects];
}
break;
case GmkObliqueUp:{
//向前遍历
NSInteger j = col + 1;
for (NSInteger i = row - 1; i >= 0; i--,j++) {
NSString * key = [NSString stringWithFormat:@"%ld-%ld",j,i];
if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor || j > kGridCount) break;
[self.sameChessmanArray addObject:self.chessmanDict[key]];
}
//向后遍历
j = col - 1;
for (NSInteger i = row + 1 ; i < kGridCount; i++,j--) {
NSString * key = [NSString stringWithFormat:@"%ld-%ld",j,i];
if (![self.chessmanDict.allKeys containsObject:key] || [self.chessmanDict[key] backgroundColor] != currentChessmanColor || j < 0) break;
[self.sameChessmanArray addObject:self.chessmanDict[key]];
}
if (self.sameChessmanArray.count >= 5) {
[self alertResult];
return YES;
}
[self.sameChessmanArray removeAllObjects];
}
break;
}
return NO;
}
- (void)newGame{
self.isOver = NO;
self.lastKey = nil;
[self.sameChessmanArray removeAllObjects];
self.userInteractionEnabled = YES;
[self.chessmanDict removeAllObjects];
for (UIView * view in self.subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
continue;
}
[view removeFromSuperview];
}
self.isBlack = NO;
}
//撤回至上一步棋
- (void)backOneStep:(UIButton *)sender{
if(self.isOver) return;
if (self.lastKey == nil) {
sender.enabled = NO;
CGFloat width = SCREEN_WIDTH * 0.4 * SCREEN_WIDTH_RATIO;
UIView * tip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 0.6 * width)];
tip.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
tip.layer.cornerRadius = 8.0f;
[self addSubview:tip];
tip.center = CGPointMake(self.width * 0.5, self.height * 0.5);
UILabel * label = [[UILabel alloc]init];
label.text = self.chessmanDict.count > 0 ? @"只能悔一步棋!!!" : @"请先落子!!!";
label.font = [UIFont systemFontOfSize:15];
[label sizeToFit];
label.center = CGPointMake(tip.width * 0.5, tip.height * 0.5);
[tip addSubview:label];
self.userInteractionEnabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.userInteractionEnabled = YES;
sender.enabled = YES;
[tip removeFromSuperview];
});
return;
}
[self.chessmanDict removeObjectForKey:self.lastKey];
[self.subviews.lastObject removeFromSuperview];
self.isBlack = !self.isBlack;
self.lastKey = nil;
}
//改变键盘级别
- (void)changeBoardLevel{
for (UIView * view in self.subviews) {
[view removeFromSuperview];
}
[self newGame];
self.isHighLevel = !self.isHighLevel;
[self drawBackground:self.bounds.size];
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有