gpt4 book ai didi

ios - 连续找到5个

转载 作者:行者123 更新时间:2023-11-29 10:33:54 24 4
gpt4 key购买 nike

背景:我正在构建一个类似 connect 4 的游戏。我大部分都在工作,我现在唯一坚持的部分是确定是否连续有 5 个相同的颜色(左、右、上、下和对角线)

问题:

我怎样才能让代码循环,看看是否有 5 block 连续的颜色在任何方向上都相同。

注意 - 每一轮都是通过将棋盘上的一个棋子移动到一个新位置来进行的,然后有 3 个新棋子上场。这意味着它必须在转牌后以及在 3 个新棋子被随机放置到棋盘上之后检查是否匹配 5。

谢谢!

到目前为止,我的游戏代码是..

View Controller (.m)

#import "ViewController.h"
#import "BoardCell.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (strong,nonatomic) NSArray *imageNames;
@property (strong,nonatomic) NSMutableArray *board;
@property NSInteger lastMove;
#define BOARDWIDTH 9
#define BOARDHEIGHT 9

@end

static int moves[]={-BOARDWIDTH,-1,1,BOARDWIDTH};
bool preSelect;
BoardCell *startCell;
@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
preSelect = NO;
self.imageNames = @[@"marble_red",@"marble_blue",@"marble_purple",@"marble_orange",@"marble_green"];

self.board = [NSMutableArray new];

for (int y=0; y < BOARDWIDTH; y++) {
for (int x = 0; x < BOARDHEIGHT; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(5+ 35 * x, 100 + 35 * y, 30, 30);
button.tag = y*BOARDWIDTH+x;
//[button setTitle:[NSString stringWithFormat:@"%ld", button.tag] forState:UIControlStateNormal];
button.selected = NO;
[button.layer setCornerRadius:15];
[button setBackgroundColor:[UIColor colorWithWhite:.7 alpha:.5]];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: button];
[self.board addObject:[[BoardCell alloc] initWithButton:button]];
}
}

self.lastMove=arc4random_uniform(BOARDWIDTH*BOARDHEIGHT);
UIButton *button=(UIButton *)[self.view viewWithTag:self.lastMove];
[button setBackgroundImage:[UIImage imageNamed:@"greensquare"] forState:UIControlStateNormal];

[self addRandoms:3];


}

-(void) addRandoms:(NSInteger)randomCount {
for (int i = 0; i < randomCount; i++) {
int space = arc4random_uniform(BOARDWIDTH*BOARDHEIGHT);
BoardCell *cell=self.board[space];
if (!cell.occupied) {
int pic = arc4random_uniform((u_int32_t)self.imageNames.count);
NSString *string = [self.imageNames objectAtIndex:pic];
NSString *highlighted = [NSString stringWithFormat:@"%@_highlighted",string];
NSLog(@"HIGHLIGHTED = %@",highlighted);
[cell.button setBackgroundImage:[UIImage imageNamed:string] forState:UIControlStateNormal];
[cell.button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[cell.button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
cell.button.selected = NO;
cell.occupied=YES;
}
else {
i--;
}

}
}

-(void)buttonPressed:(UIButton *)button
{
NSInteger buttonId=button.tag;
BoardCell *cell=self.board[buttonId];
if (!preSelect) {
if (cell.occupied) {
//cell.button.selected = YES;
[[cell.button layer] setBorderWidth:3.5f];
[cell.button.layer setBorderColor:[[UIColor colorWithWhite:.85 alpha:.7]CGColor]];
cell.button.highlighted = YES;
preSelect = YES;
self.lastMove = buttonId;
startCell = cell;
}else{
cell.button.selected = NO;
cell.button.highlighted = NO;
[cell.button.layer setBorderColor:[[UIColor clearColor]CGColor]];

}
}else{
NSLog(@"SECOND STEP");
if (!cell.occupied) {

BoardCell *startCell=self.board[self.lastMove];
startCell.occupied=NO;
if ([self validMoveFromSquare:self.lastMove toDestination:buttonId]) {
[cell.button setBackgroundImage:[startCell.button backgroundImageForState:UIControlStateNormal]
forState:UIControlStateNormal];
[startCell.button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];

NSLog(@"FROM %ld, TO %ld",(long)self.lastMove,(long)buttonId);
cell.button.selected = NO;
cell.button.highlighted = NO;
startCell.button.selected = NO;
startCell.button.highlighted = NO;
self.lastMove=buttonId;
cell.occupied=YES;
preSelect = NO;
[self addRandoms:3];

}else{
startCell.occupied=YES;
preSelect = NO;
cell.button.selected = NO;
cell.button.highlighted = NO;
startCell.button.selected = NO;
startCell.button.highlighted = NO;
NSLog(@" INVALID FROM %ld, TO %ld",(long)self.lastMove,(long)buttonId);
}
}
preSelect = NO;
cell.button.selected = NO;
cell.button.highlighted = NO;
startCell.button.selected = NO;
startCell.button.highlighted = NO;
[cell.button.layer setBorderColor:[[UIColor clearColor]CGColor]];
[startCell.button.layer setBorderColor:[[UIColor clearColor]CGColor]];


}

}


-(BOOL) validMoveFromSquare:(NSInteger)startSquare toDestination:(NSInteger)destination {

for (int limit=1;limit<10;limit++ ) {
NSMutableIndexSet *visitList=[NSMutableIndexSet new];
if ([self DFSFromStart:startSquare toGoal:destination withLimit:limit andVisitList:visitList]) {
return YES;
}
}

return NO;

}

-(BOOL) DFSFromStart:(NSInteger)start toGoal:(NSInteger)goal withLimit:(NSInteger)limit andVisitList:(NSMutableIndexSet *)visitList {



if (limit >=0) {

if (((BoardCell *)self.board[start]).occupied) {
NSLog(@"Self Board = %@",self.board[start]);
return NO;
}

[visitList addIndex:start];

for (int i=0;i<4;i++) {
NSInteger nextPosition=start+moves[i];
NSLog(@"Next spot = %ld",(long)nextPosition);
if (nextPosition == goal) {
return YES;
}
if (nextPosition >=0 && nextPosition < BOARDWIDTH*BOARDHEIGHT) {
if (![visitList containsIndex:nextPosition]) {
if ([self DFSFromStart:nextPosition toGoal:goal withLimit:limit-1 andVisitList:visitList]) {
return YES;
}
}

}
}
}
return NO;

}

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

BoardCell.m

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface BoardCell : NSObject


@property (weak,nonatomic,readonly) UIButton *button;
@property BOOL occupied;


-(id) initWithButton:(UIButton *)button;

@end

再次感谢!

最佳答案

您可以使用递归算法在八个方向中的每一个方向上工作,直到遇到边界或不同的颜色 -

-(void) clearRunsOfColor:(Colors)color fromPoint:(NSInteger)startPoint {
NSInteger left=[self runFromStart:startPoint-1 ofColor:color inDirection:-1];
NSInteger right=[self runFromStart:startPoint+1 ofColor:color inDirection:1];
BOOL cleared=NO;
if (left+right+1 >4) {
[self clearBoardFromStart:startPoint-1 inDirection:-1 forLength:left];
[self clearBoardFromStart:startPoint+1 inDirection:1 forLength:right];
cleared=YES;
}

NSInteger up=[self runFromStart:startPoint-BOARDWIDTH ofColor:color inDirection:-BOARDWIDTH];
NSInteger down=[self runFromStart:startPoint+BOARDWIDTH ofColor:color inDirection:BOARDWIDTH];
if (up+down+1 > 4) {
[self clearBoardFromStart:startPoint-BOARDWIDTH inDirection:-BOARDWIDTH forLength:up];
[self clearBoardFromStart:startPoint+BOARDWIDTH inDirection:BOARDWIDTH forLength:down];
cleared=YES;
}

NSInteger NW=[self runFromStart:startPoint-BOARDWIDTH-1 ofColor:color inDirection:-BOARDWIDTH-1];
NSInteger SE=[self runFromStart:startPoint+BOARDWIDTH+1 ofColor:color inDirection:+BOARDWIDTH+1];

if (NW+SE+1 > 4) {
[self clearBoardFromStart:startPoint-BOARDWIDTH-1 inDirection:-BOARDWIDTH-1 forLength:NW];
[self clearBoardFromStart:startPoint+BOARDWIDTH+1 inDirection:BOARDWIDTH+1 forLength:SE];
cleared=YES;
}

NSInteger NE=[self runFromStart:startPoint-BOARDWIDTH+1 ofColor:color inDirection:-BOARDWIDTH+1];
NSInteger SW=[self runFromStart:startPoint+BOARDWIDTH-1 ofColor:color inDirection:+BOARDWIDTH-1];

if (NE+SW+1 > 4) {
[self clearBoardFromStart:startPoint-BOARDWIDTH+1 inDirection:-BOARDWIDTH+1 forLength:NE];
[self clearBoardFromStart:startPoint+BOARDWIDTH-1 inDirection:BOARDWIDTH-1 forLength:SW];
cleared=YES;
}

if (cleared) {
[self occupyCell:startPoint withPiece:nil];
}
}

-(void) clearBoardFromStart:(NSInteger)start inDirection:(NSInteger)direction forLength:(NSInteger) length {
NSInteger pos=start;
for (int i=0;i<length;i++) {
[self occupyCell:pos withPiece:nil];
pos+=direction;
}

}

-(NSInteger) runFromStart:(NSInteger)start ofColor:(Colors)color inDirection:(NSInteger)direction {
if ([self inBounds:start]) {
BoardCell *thisCell=self.board[start];
if (thisCell.gamepiece != nil && thisCell.gamepiece.color == color) {
if ([self validDestination:start+direction withMove:(int)direction fromSquare:start]) {
return ([self runFromStart:start+direction ofColor:color inDirection:direction]+1);
}
else {
return 1;
}
}
}
return 0;
}

-(BOOL) inBounds:(NSInteger) position {
if (position >=0 && position < BOARDHEIGHT*BOARDWIDTH) {
return YES;
}
else {
return NO;
}
}

关于ios - 连续找到5个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28075054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com