连续找到5个

时间:2015-01-21 19:17:03

标签: ios objective-c

背景: 我正在建立一个游戏"有点"喜欢连接4。 我大部分都在工作,我现在唯一坚持的部分就是确定一行中是否有5种相同的颜色(左,右,上,下和对角线)

问题:

如何让代码循环并查看在任何方向上连续5个部分是否具有相同的颜色。

注意 - 通过将棋盘上的一个棋子移动到一个新位置并且比起3个新棋子进行游戏来进行每个回合。 这意味着它必须在转弯后检查5的匹配,并且在3个新的棋子随机放置在棋盘上之后。

谢谢!

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

的ViewController(.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

再次感谢你!

2 个答案:

答案 0 :(得分:2)

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

-(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;
    }
}

答案 1 :(得分:0)

以下代码均未经过正确性测试。

您可以像这样更新BoardCell课程:

typedef enum {
  TopLeft,    Top,    TopRight,
  Left,               Right,
  BottomLeft, Bottom, BottomRight,
  NumberOfDirections
} BoardCellAdjacentDirection;

@interface BoardCell
- (void)addAdjacentCell:(BoardCell *)cell forDirection:(BoardCellAdjacentDirection)direction;
- (NSInteger)occupiedCellsInDirection:(BoardCellAdjacentDirection)direction;
@end

@implementation BoardCell {
  NSMutableArray *adjacentCells;
}
- (instancetype)init {
  if (self = [super init]) {
    adjacentCells = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < (NSInteger)NumberOfDirections; i++) {
      [adjacentCells addObject:[NSNull null]];
    }
  }
  return self;
}
- (void)addAdjacentCell:(BoardCell *)cell forDirection:(BoardCellAdjacentDirection)direction {
  [adjacentCells setObject:cell atIndex:(NSInteger)direction];
}
- (NSInteger)occupiedCellsInDirection:(BoardCellAdjacentDirection)direction {
  if (!self.occupied) return 0;
  if (adjacentCells[direction] == [NSNull null]) return 1;
  return [adjacentCells[direction] occupiedCellsInDirection:direction]+1;
}
@end

无论您在何处创建BoardCell个对象,都需要添加相应的相邻单元格。你也可能想要进行一些健全性检查,以确保细胞邻接是一种双向关系(即细胞A与B相邻,B与A正确相邻)。

编辑您可能还想添加一种方法来确定是否已将单元格注入到线条的中心。

typedef enum {
  BackSlash,
  ForwardSlash,
  Vertical,
  Horizontal
} BoardCellAxis;

...

- (NSInteger)occupiedCellsOnAxis:(BoardCellAxis)axis {
  switch (axis) {
  case BackSlash:
    return MAX(0, [self occupiedCellsInDirection:TopLeft]
                  + [self occupiedCellsInDirection:BottomRight]
                  - 1); // Don't count yourself twice.
  case ForwardSlash:
    return MAX(0, [self occupiedCellsInDirection:TopRight]
                  + [self occupiedCellsInDirection:BottomLeft]
                  - 1); // Don't count yourself twice.
  case Vertical:
    return MAX(0, [self occupiedCellsInDirection:Top]
                  + [self occupiedCellsInDirection:Bottom]
                  - 1); // Don't count yourself twice.
  case Horizontal:
    return MAX(0, [self occupiedCellsInDirection:Left]
                  + [self occupiedCellsInDirection:Right]
                  - 1); // Don't count yourself twice.
  }
}