-
Notifications
You must be signed in to change notification settings - Fork 9
/
ConnectedCellRowsDetector.m
207 lines (176 loc) · 6.21 KB
/
ConnectedCellRowsDetector.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// ConnectedCellRowsDetector.m
// Connect5
//
// Created by Mohammed Eldehairy on 10/10/13.
// Copyright (c) 2013 Mohammed Eldehairy. All rights reserved.
//
#import "ConnectedCellRowsDetector.h"
@implementation ConnectedCellRowsDetector
+(void)getConnectedCellsWithGraph:(Graph*)graph withVertices:(NSArray*)vertices withCompletionBlock:(DetectedResultArrayBlock)block
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
NSMutableArray *result = [NSMutableArray array];
for(GraphCell *GCell in vertices)
{
[result addObjectsFromArray:[self detectConnectedRowsVerticallyWithGraph:graph withVertixe:GCell]];
[result addObjectsFromArray:[self detectConnectedRowsHorizontallyWithGraph:graph withVertixe:GCell]];
[result addObjectsFromArray:[self detectConnectedRowsDiagonallyToTheRightWithGraph:graph withVertixe:GCell]];
[result addObjectsFromArray:[self detectConnectedRowsDiagonallyToTheLeftWithGraph:graph withVertixe:GCell]];
}
NSSet *set = [NSSet setWithArray:result];
NSArray *uniqueArray = [set allObjects];
dispatch_async(dispatch_get_main_queue(), ^(void){
block(uniqueArray);
});
});
//return result;
}
+(NSArray*)detectConnectedRowsVerticallyWithGraph:(Graph*)graph withVertixe:(GraphCell*)VertixGCell
{
NSMutableArray *result = [NSMutableArray array];
CGPoint VertixCellPoint = [VertixGCell getPointWithSize:graph.size];
int StartX = VertixCellPoint.x;
int StartY = 0;
for(int i = 0;i<graph.size.height;i++)
{
BOOL breakLoop = [self DetectConnectedCellsInaRowWithConnectedCellsArray:result withCurrentGCell:[graph getGraphCellWithX:StartX withY:StartY+i]];
if(breakLoop)
{
break;
}
}
if(result.count>=CONNECTED_CELLS_COUNT)
{
return result;
}
return [NSArray array];
}
+(NSArray*)detectConnectedRowsHorizontallyWithGraph:(Graph*)graph withVertixe:(GraphCell*)VertixGCell
{
NSMutableArray *result = [NSMutableArray array];
CGPoint VertixCellPoint = [VertixGCell getPointWithSize:graph.size];
int StartX = 0;
int StartY = VertixCellPoint.y;
for(int i = 0;i<graph.size.width;i++)
{
BOOL breakLoop = [self DetectConnectedCellsInaRowWithConnectedCellsArray:result withCurrentGCell:[graph getGraphCellWithX:StartX+i withY:StartY]];
if(breakLoop)
{
break;
}
}
if(result.count>=CONNECTED_CELLS_COUNT)
{
return result;
}
return [NSArray array];
}
+(NSArray*)detectConnectedRowsDiagonallyToTheRightWithGraph:(Graph*)graph withVertixe:(GraphCell*)VertixGCell
{
NSMutableArray *result = [NSMutableArray array];
CGPoint VertixCellPoint = [VertixGCell getPointWithSize:graph.size];
int StartX = VertixCellPoint.x;
int StartY = VertixCellPoint.y;
//int limit = 1;
int EndX = VertixCellPoint.x;
int EndY = VertixCellPoint.y;
for(int z=0;z<(graph.size.width>graph.size.height?graph.size.width:graph.size.height);z++)
{
if(EndY==0 || EndX==graph.size.width-1)
{
break;
}
EndX++;
EndY--;
}
for(int z=0;z<(graph.size.width>graph.size.height?graph.size.width:graph.size.height);z++)
{
if(StartY==graph.size.height-1 || StartX==0)
{
break;
}
StartX--;
StartY++;
}
for(int i = 0 ;StartX+i<=EndX||StartY-i>=EndY;i++)
{
BOOL breakLoop = [self DetectConnectedCellsInaRowWithConnectedCellsArray:result withCurrentGCell:[graph getGraphCellWithX:StartX+i withY:StartY-i]];
if(breakLoop)
{
break;
}
}
if(result.count>=CONNECTED_CELLS_COUNT)
{
return result;
}
return [NSArray array];
}
+(NSArray*)detectConnectedRowsDiagonallyToTheLeftWithGraph:(Graph*)graph withVertixe:(GraphCell*)VertixGCell
{
NSMutableArray *result = [NSMutableArray array];
CGPoint VertixCellPoint = [VertixGCell getPointWithSize:graph.size];
int StartX = 0;
int StartY = 0;
int iteratorLimit = 0;
if(VertixCellPoint.x>VertixCellPoint.y)
{
StartY = 0;
StartX = VertixCellPoint.x-VertixCellPoint.y;
iteratorLimit = graph.size.width-1-StartX;
}else if (VertixCellPoint.y>VertixCellPoint.x)
{
StartX = 0;
StartY = VertixCellPoint.y-VertixCellPoint.x;
iteratorLimit = graph.size.height-1-StartY;
}else
{
StartX = 0;
StartY = 0;
iteratorLimit = graph.size.height>graph.size.width?graph.size.width:graph.size.height;
iteratorLimit--;
}
for(int i = 0 ;i<=iteratorLimit;i++)
{
BOOL breakLoop = [self DetectConnectedCellsInaRowWithConnectedCellsArray:result withCurrentGCell:[graph getGraphCellWithX:StartX+i withY:StartY+i]];
if(breakLoop)
{
break;
}
}
if(result.count>=CONNECTED_CELLS_COUNT)
{
return result;
}
return [NSArray array];
}
+(BOOL)DetectConnectedCellsInaRowWithConnectedCellsArray:(NSMutableArray*)ConnectedCells withCurrentGCell:(GraphCell*)CurrentCell
{
if(CurrentCell.color==unOccupied && [ConnectedCells count]<CONNECTED_CELLS_COUNT)
{
[ConnectedCells removeAllObjects];
return NO;
}
GraphCell *LastCellInConnectedArray = nil;
if(ConnectedCells.count>0 )
{
LastCellInConnectedArray = [ConnectedCells lastObject];
if(LastCellInConnectedArray.color!=CurrentCell.color && [ConnectedCells count]<CONNECTED_CELLS_COUNT)
{
[ConnectedCells removeAllObjects];
[ConnectedCells addObject:CurrentCell];
}else if (LastCellInConnectedArray.color!=CurrentCell.color && [ConnectedCells count]>=CONNECTED_CELLS_COUNT)
{
return YES;
}else if (LastCellInConnectedArray.color==CurrentCell.color)
{
[ConnectedCells addObject:CurrentCell];
}
}else
{
[ConnectedCells addObject:CurrentCell];
}
return NO;
}
@end