-
Notifications
You must be signed in to change notification settings - Fork 9
/
Graph.m
171 lines (150 loc) · 4.24 KB
/
Graph.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
//
// Graph.m
// Connect5
//
// Created by Mohammed Eldehairy on 6/21/13.
// Copyright (c) 2013 Mohammed Eldehairy. All rights reserved.
//
#import "Graph.h"
@interface Graph()
@property(nonatomic,retain)NSMutableArray *graphArray;
@end
@implementation Graph
-(id)initWithSize:(MSize *)size
{
self = [super init];
if(self)
{
_size = size;
self.graphArray = [NSMutableArray array];
int TotalNoOfCells = self.size.width*self.size.height;
for(int i = 0;i< TotalNoOfCells;i++)
{
GraphCellStatus stat = unOccupied;
GraphCell *cell = [[GraphCell alloc] initWithColor:stat] ;
[self AddGraphCell:cell];
}
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if(self)
{
self.graphArray = [aDecoder decodeObjectForKey:@"graphArray"];
self.size = [aDecoder decodeObjectForKey:@"size"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.graphArray forKey:@"graphArray"];
[aCoder encodeObject:self.size forKey:@"size"];
}
-(void)AddGraphCell:(GraphCell *)cell
{
int Currentindex = self.graphArray.count;
cell.x = Currentindex%self.size.width;
cell.y = Currentindex/self.size.width;
cell.index = Currentindex;
[self.graphArray addObject:cell];
}
-(GraphCell*)getGraphCellWithIndex:(int)index
{
GraphCell *cell = nil;
if(index<self.graphArray.count)
cell = [self.graphArray objectAtIndex:index];
return cell;
}
-(void)UpdateGraphCellAtIndex:(int)index WithCell:(GraphCell*)cell
{
[self.graphArray replaceObjectAtIndex:index withObject:cell];
}
-(void)UpdateGraphCellAtX:(int)x AndY:(int)Y WithCell:(GraphCell*)cell
{
GraphCell *OldCell = [self getGraphCellWithX:x withY:Y];
[self.graphArray replaceObjectAtIndex:[self.graphArray indexOfObject:OldCell] withObject:cell];
}
-(int)indexWithX:(int)x AndY:(int)y
{
return y*self.size.width+x;
}
-(GraphCell*)getGraphCellWithX:(int)x withY:(int)y
{
int index = x*self.size.width+y;
GraphCell *cell = nil;
if(index<self.graphArray.count)
cell = [self.graphArray objectAtIndex:index];
return cell;
}
-(void)ExchangeCellAtIndex:(int)FromIndex WithCellAtIndex:(int)ToIndex
{
if(FromIndex<self.graphArray.count && ToIndex<self.graphArray.count)
{
GraphCell *fromGCell = [self getGraphCellWithIndex:FromIndex];
GraphCell *toGCell = [self getGraphCellWithIndex:ToIndex];
int fromI = fromGCell.index;
fromGCell.index = toGCell.index;
toGCell.index = fromI;
[self.graphArray exchangeObjectAtIndex:FromIndex withObjectAtIndex:ToIndex];
}
}
-(NSArray*)getOcuupiedCells
{
NSMutableArray *OccupiedCells = [NSMutableArray array];
for(GraphCell *cell in self.graphArray)
{
if(cell.color!=unOccupied && !cell.temporarilyUnoccupied)
{
NSNumber *number = [NSNumber numberWithInt:[self.graphArray indexOfObject:cell]];
[OccupiedCells addObject:number];
}
}
return OccupiedCells;
}
-(NSArray*)getUnOccupiedCells
{
NSMutableArray *UnOccupiedCells = [NSMutableArray array];
for(GraphCell *cell in self.graphArray)
{
if(cell.color==unOccupied)
{
NSNumber *number = [NSNumber numberWithInt:[self.graphArray indexOfObject:cell]];
[UnOccupiedCells addObject:number];
}
}
return UnOccupiedCells;
}
-(int)getIndexOfGraphCell:(GraphCell*)GCell
{
return [self.graphArray indexOfObject:GCell];
}
-(void)ResetGraph
{
for(GraphCell *cell in _graphArray)
{
cell.color = unOccupied;
cell.temporarilyUnoccupied = NO;
}
}
-(void)dealloc
{
}
-(id)copyWithZone:(NSZone *)zone
{
Graph *copy = [[Graph alloc] init];
if(copy)
{
NSMutableArray *CopyArray = [NSMutableArray array];
for(GraphCell *cell in _graphArray)
{
GraphCell *copyGCell = [cell copyWithZone:zone];
[CopyArray addObject:copyGCell];
}
[copy setGraphArray:CopyArray];
[copy setSize:[self.size copyWithZone:zone]];
}
return copy;
}
@end