2013-08-10 1 views
1

내 게임에서 플레이어는 그리드에서 개체 (여기의 타일)를 활성화 할 수 있습니다. 예제 그림에서 모든 빨간 타일이 "활성화"되었다고 상상해보십시오. 플레이어가 빨간색 타일 (예 : 보라색 화살표가있는 타일)을 건 드리면 인접한 모든 빨간색 타일과 빨간색 타일 (녹색 타일)로 묶인 타일에 메시지를 보내고 싶습니다.그리드상의 한 점이 특정 종류의 점으로 둘러싸여있는 경우 알아보기

빨간 타일이나 그리드의 벽에 바인딩 된 타일에도 메시지를 보낼 수 있습니다. (모서리가 아닌 벽이나 전체 빈 공간이 포함됩니다.) 여기에 푸른 타일로 보여줍니다. 빨간 타일이 인접 (노란색 모양)이 아닌 diaganal 일 때도 작동하도록하는 데 신경 쓰지는 않지만, 그 중 하나라도 게임 플레이에 포함 시킬지 확신 할 수 없습니다.

이것은 단순화되었지만 여기에 대한 조언이 필요합니다. 수학의 유형을 알아야합니다.

도움이 될지 모르지만 게임이 Objective-C 및 Cocos2D에 있고 각 타일이 서브 클래 싱 된 개체이고 어떤 배열이 어떤 배열인지 알 수있는 속성이 있습니다.

이 게시물과 관련이있을 것 같지 않습니다 surrounding objects algorithm

enter image description here

+0

,이 거 그것에 대해 생각, 내가 필요한 수 있습니다 : http://www.techuser.net/minecascade.html 당신은 기본적으로 (의 변화)을 찾고 – kidnim

+1

윤곽 추적 알고리즘, Moore 's : http://www.imageprocessingplace.com/downloads_V3/root_downloads/tutorials/contour_tracing_Abeer_George_Ghuneim/moore.html 등고선을 따라하면 내부에있는 타일을 알고 상태를 확인할 수 있습니다. – LearnCocos2D

+0

Steffen - 다시 한 번 구조! 나는 일종의 비슷한 과정을 시작했습니다. 나는 지뢰 찾기 캐스케이드 (Cascade) 알고리즘과 무어 컨투어 (moore contour) 사이의 혼합이라고 생각합니다. 기본적으로 시작 타일을 가져 와서 그것에 연결된 타일 배열을 만듭니다. 그런 다음 전체 맵에서 각 타일을 검토하고 모든면에 표시된 타일로 둘러싸여 있는지 확인하십시오. – kidnim

답변

1

그래서,이 완벽하지 만 작동합니다. 너무 자세하게 설명하지는 않겠지 만 learncocos2d/steffen의 방법을 사용한 다음 경계 탐지기를 사용하여 구멍을 탐지하려고했습니다. 수평 스캔에서는이 작업을 수행합니다. 분명히이 코드에는 배열에 대한 호출이 포함되어 있지만 배열에 대한 호출이 포함되어 있지만 arrayofboxes는 각 항목이 Box라는 타일 행인 배열 배열입니다.

'사람에 경우의 도움을이 게시 해요 그리고 난 아직 거기 오류를이 arent

-(NSMutableDictionary*) returnMooreNeighborDictFromBox:(Box*)enteredBox { 

NSMutableDictionary * mooreDictToReturn = [NSMutableDictionary dictionaryWithCapacity:8]; 

//define moore-neighbor as the 8 boxes around any given box. p1 is upper left 

/* 
P1 P2 P3 
P8 XX P4 
P7 P6 P5 
*/ 

int depth = enteredBox.trueDepth; 
int depthRelativeXCoord = enteredBox.gridAbsoluteXCoord/((pow(2,depth))); 
int depthRelativeYCoord = enteredBox.gridAbsoluteYCoord/((pow(2,depth))); 
int xPosInArray = depthRelativeXCoord; 
int yPosInArray = depthRelativeYCoord; 

int maxCoord = (self.numberOfBoxesOfAcross/(pow(2,depth))-1); 

NSMutableArray *boxArray = [arrayOfBoxArrays objectAtIndex:depth]; 

NSMutableArray *boxRowOfArray = [boxArray objectAtIndex:yPosInArray]; 
    if ((xPosInArray-1)>=0){ 
     Box * p8Box = [boxRowOfArray objectAtIndex:xPosInArray-1]; 
     [mooreDictToReturn setObject:p8Box forKey:@"p8"]; 
     } 
    else { 
     [mooreDictToReturn setObject:[NSNull new] forKey:@"p8"]; 
     } 
    if ((xPosInArray+1)<=maxCoord){ 
     Box * p4Box = [boxRowOfArray objectAtIndex:xPosInArray+1]; 
     [mooreDictToReturn setObject:p4Box forKey:@"p4"]; 
     } 
    else { 
     [mooreDictToReturn setObject:[NSNull new] forKey:@"p4"]; 
     } 

if ((yPosInArray-1)>=0){ 
    NSMutableArray *boxRowBelowArray = [boxArray objectAtIndex:yPosInArray-1]; 
    Box * p6Box = [boxRowBelowArray objectAtIndex:xPosInArray]; 
    [mooreDictToReturn setObject:p6Box forKey:@"p6"]; 
    if ((xPosInArray-1)>=0){ 
     Box * p7Box = [boxRowBelowArray objectAtIndex:xPosInArray-1]; 
     [mooreDictToReturn setObject:p7Box forKey:@"p7"]; 
     } 
    else { 
     [mooreDictToReturn setObject:[NSNull new] forKey:@"p7"]; 
     } 
    if ((xPosInArray+1)<=maxCoord){ 
     Box * p5Box = [boxRowBelowArray objectAtIndex:xPosInArray+1]; 
     [mooreDictToReturn setObject:p5Box forKey:@"p5"]; 
     } 
    else { 
     [mooreDictToReturn setObject:[NSNull new] forKey:@"p5"]; 
     } 
    } 
else { 
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p5"]; 
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p6"]; 
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p7"];   
    } 


if ((yPosInArray+1)<=maxCoord){ 
    NSMutableArray *boxRowAboveArray = [boxArray objectAtIndex:yPosInArray+1]; 
    Box * p2Box = [boxRowAboveArray objectAtIndex:xPosInArray]; 
    [mooreDictToReturn setObject:p2Box forKey:@"p2"]; 
    if ((xPosInArray-1)>=0){ 
     Box * p1Box = [boxRowAboveArray objectAtIndex:xPosInArray-1]; 
     [mooreDictToReturn setObject:p1Box forKey:@"p1"]; 
     } 
    else { 
     [mooreDictToReturn setObject:[NSNull new] forKey:@"p1"]; 
     } 
    if ((xPosInArray+1)<=maxCoord){ 
     Box * p3Box = [boxRowAboveArray objectAtIndex:xPosInArray+1]; 
     [mooreDictToReturn setObject:p3Box forKey:@"p3"]; 
     } 
    else { 
     [mooreDictToReturn setObject:[NSNull new] forKey:@"p3"]; 
     } 
    } 
else { 
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p1"]; 
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p2"]; 
    [mooreDictToReturn setObject:[NSNull new] forKey:@"p3"]; 
    } 

return mooreDictToReturn; 

}

-(NSString*) stepClockwiseMooreItem:(NSString*) currentMooreItem { 

CCLOG(@"STEPCLOC"); 

//go through the 8-moore clockwise starting at P6 until hit. add hit to array. mark "entry box" as last box that was inactive before hit 
//go to hit box and go through 8 starting with last hit box (if p3 was prior hit then p2 was empty so go to p8 of new box. these are the pairs: if X hit then prior empty was Y: p1>p8 P2>p1, P3>p2, P4>p3 etc. 

NSString *returnString; 

if ([currentMooreItem isEqualToString:@"p1"]){returnString= [NSString stringWithFormat:@"p2"];} 
else if ([currentMooreItem isEqualToString:@"p2"]){returnString= [NSString stringWithFormat:@"p3"];} 
else if ([currentMooreItem isEqualToString:@"p3"]){returnString= [NSString stringWithFormat:@"p4"];} 
else if ([currentMooreItem isEqualToString:@"p4"]){returnString= [NSString stringWithFormat:@"p5"];} 
else if ([currentMooreItem isEqualToString:@"p5"]){returnString= [NSString stringWithFormat:@"p6"];} 
else if ([currentMooreItem isEqualToString:@"p6"]){returnString= [NSString stringWithFormat:@"p7"];} 
else if ([currentMooreItem isEqualToString:@"p7"]){returnString= [NSString stringWithFormat:@"p8"];} 
else if ([currentMooreItem isEqualToString:@"p8"]){returnString= [NSString stringWithFormat:@"p1"];} 

return returnString; 

}

-(NSString*) stepCounterClockMooreItem:(NSString*) currentMooreItem { 

//go through the 8-moore clockwise starting at P6 until hit. add hit to array. mark "entry box" as last box that was inactive before hit 
//go to hit box and go through 8 starting with last hit box (if p3 was prior hit then p2 was empty so go to p8 of new box. these are the pairs: if X hit then prior empty was Y: p1>p8 P2>p1, P3>p2, P4>p3 etc. 

NSString *returnString; 

if ([currentMooreItem isEqualToString:@"p1"]){returnString= [NSString stringWithFormat:@"p8"];} 
else if ([currentMooreItem isEqualToString:@"p2"]){returnString= [NSString stringWithFormat:@"p1"];} 
else if ([currentMooreItem isEqualToString:@"p3"]){returnString= [NSString stringWithFormat:@"p2"];} 
else if ([currentMooreItem isEqualToString:@"p4"]){returnString= [NSString stringWithFormat:@"p3"];} 
else if ([currentMooreItem isEqualToString:@"p5"]){returnString= [NSString stringWithFormat:@"p4"];} 
else if ([currentMooreItem isEqualToString:@"p6"]){returnString= [NSString stringWithFormat:@"p5"];} 
else if ([currentMooreItem isEqualToString:@"p7"]){returnString= [NSString stringWithFormat:@"p6"];} 
else if ([currentMooreItem isEqualToString:@"p8"]){returnString= [NSString stringWithFormat:@"p7"];} 

return returnString; 
} 

-(NSString*) stepMooreBacktrackAfterHitAt:(NSString*) currentMooreItem { 

// with the moore, you can only back track adjacenetly, not diagnally, so its not straightforward counterclockwise walk 

NSString *returnString; 

if ([currentMooreItem isEqualToString:@"p1"]){returnString= [NSString stringWithFormat:@"p6"];} 
else if ([currentMooreItem isEqualToString:@"p2"]){returnString= [NSString stringWithFormat:@"p8"];} 
else if ([currentMooreItem isEqualToString:@"p3"]){returnString= [NSString stringWithFormat:@"p8"];} 
else if ([currentMooreItem isEqualToString:@"p4"]){returnString= [NSString stringWithFormat:@"p2"];} 
else if ([currentMooreItem isEqualToString:@"p5"]){returnString= [NSString stringWithFormat:@"p2"];} 
else if ([currentMooreItem isEqualToString:@"p6"]){returnString= [NSString stringWithFormat:@"p4"];} 
else if ([currentMooreItem isEqualToString:@"p7"]){returnString= [NSString stringWithFormat:@"p4"];} 
else if ([currentMooreItem isEqualToString:@"p8"]){returnString= [NSString stringWithFormat:@"p6"];} 

return returnString; 
% 확신하지 않다

} 실제로

-(void) boxAreaCollapse:(float)boxCenterGridCoordX andY:(float)boxCenterGridCoordY atDepth:(int)depth{ 

//ok, to do this, we'll use moore neighbor tracing 
//might be able to capture edges by add a row above and below and to either side of grid. each of these has a unique identity so if end wall included or something but this is later 

//set array of boxes to empty 
NSMutableArray * boxesOnEdgeOfShapeContainingTouchedBox = [NSMutableArray arrayWithCapacity:((self.numberOfBoxesOfAcross/(pow(2,depth)))*(self.numberOfBoxesOfAcross/(pow(2,depth))))]; 

//get array of array for this "depth" 
NSMutableArray *boxArray = [arrayOfBoxArrays objectAtIndex:depth]; 

//get the box that was touched 
NSMutableArray * boxRowSameAsTouchedBox = [boxArray objectAtIndex:boxCenterGridCoordY]; 
Box * boxTouched = [boxRowSameAsTouchedBox objectAtIndex:boxCenterGridCoordX]; 

CCLOG(@"boxTouched %i,%i",boxTouched.gridAbsoluteXCoord,boxTouched.gridAbsoluteYCoord); 

//define boundaries > dont need to do this 
//bottom left coord is (0,0), top right is (maxCoord,maxCoord) 
//float maxCoord = (self.numberOfBoxesOfAcross/(pow(2,depth))-1); // the -1 because the first is "coord" is 0 

//must start on bottom left so move all the way left until hit inactive then down 
//move to the left through adjacenet boxes from touched best to find the furthest left connected directly to touch box 
int numberOfBoxesLeft = boxCenterGridCoordX; 
Box * furthestLeftBox = boxTouched; 
for (int i = 1; i <= numberOfBoxesLeft; i++){ 
    Box * box = [boxRowSameAsTouchedBox objectAtIndex:boxCenterGridCoordX-i]; 
    if (box.boxState == kBoxActivated || box.boxState == kBoxClicked || box.boxState == kBoxLastDepthCompleted){ 
     furthestLeftBox = box; 
     } 
    else { 
     break; 
     } 
    } 

//move down from there to connected boxes below 
int furthestLeftXCoord = (furthestLeftBox.gridAbsoluteXCoord/((pow(2,depth)))); 
int furthestLeftYCoord = (furthestLeftBox.gridAbsoluteYCoord/((pow(2,depth)))); 
int numberOfBoxesBelow = furthestLeftYCoord; 

Box * furthestDownBox = furthestLeftBox; 
for (int i = 1; i <= numberOfBoxesBelow; i++){ 
    NSMutableArray * boxRowBelow = [boxArray objectAtIndex:furthestLeftYCoord-i]; 
    Box * box = [boxRowBelow objectAtIndex:furthestLeftXCoord]; 
    if (box.boxState == kBoxActivated || box.boxState == kBoxClicked || box.boxState == kBoxLastDepthCompleted){ 
     furthestDownBox = box; 
     } 
    else { 
     break; 
     } 
    } 


//define starting Box and "entry box" for defining when to STOP this algorithim. we know p6 from the starting box should be empty and when we reenter the starting box from p6 we will define the "end" 
Box * startingBoxFilled = furthestDownBox; 
NSString * startingEnterPoint = @"p6"; 

//add start box to shape array > dont need to do this, it'll happen in first load of loop 
//[boxesOnEdgeOfShapeContainingTouchedBox addObject:furthestDownBox]; 

//go through the 8-moore clockwise starting at P6 until hit. add hit to array. mark "entry box" as last box that was inactive before hit 
//go to hit box and go through 8 starting with last hit box (if p3 was prior hit then p2 was empty so go to p8 of new box. these are the pairs: if X hit then prior empty was Y: p1>p8 P2>p1, P3>p2, P4>p3 etc. 


//define first hitBox and entry point for backtrack 
Box * hitBox =furthestDownBox; 
NSString * hitBoxEnteredFrom = @"p6"; 

//CCLOG(@"fursthest down box %i,%i: %@, ",hitBox.gridAbsoluteXCoord, hitBox.gridAbsoluteYCoord,hitBox); 

BOOL algorithimRunning = YES; 
BOOL singleCircleRunning; 

while (algorithimRunning == YES) { 
    //CCLOG(@"starting loop for box relX %i, relY%i", hitBox.gridAbsoluteXCoord, hitBox.gridAbsoluteYCoord); 

    NSMutableDictionary * mooreDictForBox = [self returnMooreNeighborDictFromBox:hitBox]; 
    singleCircleRunning = YES; 

    NSString * circleStartingChecker = hitBoxEnteredFrom; 

    while (singleCircleRunning == YES){ 

     //CCLOG(@"inner circle loop from box relX %i, relY%i checking %@ ", hitBox.gridAbsoluteXCoord, hitBox.gridAbsoluteYCoord,hitBoxEnteredFrom); 

     if ([hitBoxEnteredFrom isEqualToString:circleStartingChecker]){ 
      singleCircleRunning = NO; 
      //CCLOG(@"loop circled around, ending inner loop"); 
      } 

     if ([[mooreDictForBox objectForKey:hitBoxEnteredFrom] isKindOfClass:[Box class]]){ 
      Box * box = [mooreDictForBox objectForKey:hitBoxEnteredFrom]; 
      //CCLOG(@"moore object is box: state %i, depth %i, gridX %i, y %i", box.boxState, box.trueDepth, box.gridAbsoluteXCoord, box.gridAbsoluteYCoord); 
      if (box.boxState == kBoxActivated || box.boxState == kBoxClicked || box.boxState == kBoxLastDepthCompleted){ 
       hitBox = box; 
       [boxesOnEdgeOfShapeContainingTouchedBox addObject:box]; 
       NSString * newmooreChecker = [self stepMooreBacktrackAfterHitAt:hitBoxEnteredFrom]; 
       hitBoxEnteredFrom = newmooreChecker; 
       singleCircleRunning = NO; 
       //CCLOG(@"box is activated, etc, adjusting to %@ for backtrack ", hitBoxEnteredFrom); 
       } 
      else { 
       NSString * newmooreChecker = [self stepClockwiseMooreItem:hitBoxEnteredFrom]; 
       hitBoxEnteredFrom = newmooreChecker; 
       //CCLOG(@"box is not activated, etc, check next moore"); 
       } 
      } 
     else { 
      //CCLOG(@"moore object is nil, check next moore object"); 
      NSString * newmooreChecker = [self stepClockwiseMooreItem:hitBoxEnteredFrom]; 
      hitBoxEnteredFrom = newmooreChecker; 
      } 
     } 

    //repeat until you reach "starting down". if entered starting down from "entry" stop. 
    if ([hitBox isEqual:startingBoxFilled] && [hitBoxEnteredFrom isEqualToString:startingEnterPoint]) { 
     algorithimRunning = NO; 
     //CCLOG(@"hitbox is starting box and entered from is same as start"); 
     } 
    } 


//create an array of dictionaries to represent "on" and "off" for the rows. 
//first initialize the array of row arrays with NO in each pixel slot 
int numberOfRows = (self.numberOfBoxesOfAcross/(pow(2,depth))); 
NSMutableArray * arrayOfRowsForShape = [NSMutableArray arrayWithCapacity:numberOfRows]; 
for (int i= 0; i< numberOfRows; i++){ 
    NSMutableDictionary * rowDict = [NSMutableDictionary dictionaryWithCapacity:numberOfRows]; 
    for (int p = 0; p< numberOfRows; p++){ 
     [rowDict setObject:[NSString stringWithFormat:@"OUTSIDE"] forKey:[NSNumber numberWithInt:p]]; 
     } 
    [arrayOfRowsForShape addObject:rowDict]; 
    } 

//go through boxes in shape and change to YES if box located at that spot 
for (Box * box in boxesOnEdgeOfShapeContainingTouchedBox) { 
    NSMutableDictionary * rowDict = [arrayOfRowsForShape objectAtIndex:box.gridAbsoluteYCoord/(pow(2,depth))]; 
    [rowDict setObject:[NSString stringWithFormat:@"EDGE"] forKey:[NSNumber numberWithInt:box.gridAbsoluteXCoord/(pow(2,depth))]]; 
    } 

//go through array of dict and for each one, go left to right and mark anything bound on both sides as "inside" 
for (int locationY = 0; locationY<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationY++){ 
    NSMutableDictionary * rowDict = [arrayOfRowsForShape objectAtIndex:locationY]; 
    BOOL possiblyInsideContour = NO; 
    BOOL edgeFound = NO; 
    int holesFound = 0; 
    for (int locationXin = 0; locationXin<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationXin++){ 

     NSNumber * locationX = [NSNumber numberWithInt:locationXin]; 
     NSMutableArray * markForChanging = [NSMutableArray arrayWithCapacity:(self.numberOfBoxesOfAcross/(pow(2,depth)))]; 
     NSString * thisLocation = [rowDict objectForKey:locationX]; 
     if ([thisLocation isEqualToString:@"EDGE"] && edgeFound==NO && possiblyInsideContour==NO){ 
      edgeFound=YES; 
      } 
     else if ([thisLocation isEqualToString:@"OUTSIDE"] && edgeFound==YES && possiblyInsideContour==NO){ 
      possiblyInsideContour=YES; 
      [rowDict setObject:@"MAYBEINSIDE" forKey:locationX]; 
      [markForChanging addObject:[rowDict objectForKey:locationX]]; 
      } 
     else if ([thisLocation isEqualToString:@"OUTSIDE"] && possiblyInsideContour==YES){ 
      [rowDict setObject:@"MAYBEINSIDE" forKey:locationX]; 
      edgeFound=NO; 
      } 
     else if ([thisLocation isEqualToString:@"EDGE"] && possiblyInsideContour==YES){ 
      possiblyInsideContour=NO; 
      holesFound++; 
      edgeFound=YES; 
      } 

     } 

    int holesPatched = 0; 
    BOOL patchingHole=NO; 
    while (holesPatched < holesFound){ 
     for (int locationXin = 0; locationXin<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationXin++){ 
      NSNumber * locationX = [NSNumber numberWithInt:locationXin]; 
      NSString * thisLocation = [rowDict objectForKey:locationX]; 
      if ([thisLocation isEqualToString:@"MAYBEINSIDE"]){ 
       patchingHole = YES; 
       [rowDict setObject:@"INSIDE" forKey:locationX]; 
       } 
      else if ([thisLocation isEqualToString:@"EDGE"] && patchingHole == YES){ 
       holesPatched++; 
       patchingHole=NO; 
       } 
      } 
     } 

    } 


int locationY=0; 
for (NSMutableDictionary *rowDict in arrayOfRowsForShape){ 

    for (int locationXin = 0; locationXin<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationXin++){ 

     NSNumber * locationX = [NSNumber numberWithInt:locationXin]; 

     NSString * thisLocation = [rowDict objectForKey:locationX]; 
     if ([thisLocation isEqualToString:@"INSIDE"]){ 
      CCLOG(@"inside at %i,%i", [locationX intValue], locationY); 
      } 
     } 
    locationY++; 
    } 










} 
관련 문제