This repository has been archived by the owner on Feb 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.m
191 lines (160 loc) · 5.54 KB
/
Test.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
//
// Test.m
// FindPath
//
// Created by VCS Team on 11/2/17.
// Copyright © 2017 test. All rights reserved.
//
#import "Test.h"
#import "Pair.h"
#define row 10
#define column 10
#define COMMAND_LEFT @"left"
#define COMMAND_RIGHT @"right"
#define COMMAND_MOVE @"move"
#define POSITION_TOP 0
#define POSITION_RIGHT 1
#define POSITION_BOT 2
#define POSITION_LEFT 3
@interface Test() <HSAIPathFindingDelegate>
@property (strong, nonatomic) NSArray *maze;
@property (strong, nonatomic) NSMutableArray *result;
@property (strong, nonatomic) NSMutableArray *commands;
@property CGPoint startPoint;
@property int facingPosition;
@end
@implementation Test
-(instancetype)init
{
self = [super init];
if (self)
{
self.maze = @[
@[ @1, @1, @1, @1, @1, @1, @1, @1, @1, @1],
@[ @1, @0, @1, @1, @1, @1, @1, @1, @0, @1],
@[ @1, @0, @0, @1, @1, @1, @1, @1, @1, @1],
@[ @1, @0, @1, @1, @0, @0, @0, @0, @0, @1],
@[ @1, @1, @1, @1, @0, @1, @1, @0, @0, @1],
@[ @1, @0, @0, @1, @2, @1, @0, @1, @0, @1],
@[ @1, @1, @1, @1, @0, @0, @1, @0, @0, @1],
@[ @1, @0, @0, @0, @0, @1, @1, @1, @1, @1],
@[ @1, @3, @0, @0, @0, @0, @0, @0, @0, @1],
@[ @1, @1, @1, @1, @1, @1, @1, @1, @1, @1],
];
self.facingPosition = POSITION_TOP;
}
return self;
}
- (void) findPathFrom:(CGPoint)start to:(CGPoint)end
{
self.startPoint = start;
self.result = [[NSMutableArray alloc] init];
self.commands = [[NSMutableArray alloc] init];
HSAIPathFinding *pathFinder = [[HSAIPathFinding alloc] init];
pathFinder.delegate = self;
pathFinder.heuristic = [HSAIPathFindingHeuristic diagonalHeuristic];
[pathFinder findPathFrom: start to: end]; // returns an array of HSAIPathFindingNodes
}
- (BOOL) nodeIsPassable: (HSAIPathFindingNode *) node
{
NSArray *curentRow = [self.maze objectAtIndex:node.point.y];
NSNumber *value = [curentRow objectAtIndex:node.point.x];
return value.integerValue != 1;
}
- (NSArray *) neighborsFor: (HSAIPathFindingNode *) node
{
NSMutableArray *neighbors = [[NSMutableArray alloc] init];
// The logic you use to figure out neighbors
if (node.point.x < row -1)
{
CGPoint neighborPoint = CGPointMake(node.point.x+1, node.point.y);
[neighbors addObject:[[HSAIPathFindingNode alloc] initWithPosition:neighborPoint]];
}
if (node.point.x > 0)
{
CGPoint neighborPoint = CGPointMake(node.point.x-1, node.point.y);
[neighbors addObject:[[HSAIPathFindingNode alloc] initWithPosition:neighborPoint]];
}
if (node.point.y > 0)
{
CGPoint neighborPoint = CGPointMake(node.point.x, node.point.y-1);
[neighbors addObject:[[HSAIPathFindingNode alloc] initWithPosition:neighborPoint]];
}
if (node.point.y < column -1)
{
CGPoint neighborPoint = CGPointMake(node.point.x, node.point.y+1);
[neighbors addObject:[[HSAIPathFindingNode alloc] initWithPosition:neighborPoint]];
}
return neighbors;
}
-(void)nodeWasAddedToPath:(HSAIPathFindingNode *)node {
[self.result addObject:node];
}
-(NSArray*)getCommands {
self.commands = [[NSMutableArray alloc] init];
CGPoint currentPoint = self.startPoint;
for (int i = (int)self.result.count - 1; i >= 0; i--) {
HSAIPathFindingNode *node = [self.result objectAtIndex:i];
CGPoint nodePoint = node.point;
[self rotate:nodePoint andCurrentPoint:currentPoint];
currentPoint = nodePoint;
[self.commands addObject:COMMAND_MOVE];
}
return [self getMergedCommands:self.commands];
}
-(NSArray*) getMergedCommands:(NSArray*)commands {
NSMutableArray<Pair *> *mergedCommands = [[NSMutableArray alloc] init];
Pair *pair;
for (NSString *command in commands) {
Pair *lastPair = [mergedCommands lastObject];
if(![command isEqual:lastPair.second]) {
pair = [[Pair alloc] init];
pair.first = 1;
pair.second = command;
[mergedCommands addObject:pair];
continue;
} else {
lastPair.first++;
}
}
return mergedCommands;
}
-(void) rotate:(CGPoint) nodePoint andCurrentPoint:(CGPoint) currentPoint {
while(currentPoint.x < nodePoint.x && self.facingPosition != POSITION_RIGHT) {
[self rotateRight];
[self.commands addObject:COMMAND_RIGHT];
}
while(currentPoint.x > nodePoint.x && self.facingPosition != POSITION_LEFT) {
[self rotateRight];
[self.commands addObject:COMMAND_RIGHT];
}
while(currentPoint.y > nodePoint.y && self.facingPosition != POSITION_TOP) {
[self rotateLeft];
[self.commands addObject:COMMAND_LEFT];
}
while(currentPoint.y < nodePoint.y && self.facingPosition != POSITION_BOT) {
[self rotateLeft];
[self.commands addObject:COMMAND_LEFT];
}
}
-(void) rotateRight {
self.facingPosition++;
if(self.facingPosition > 3) {
self.facingPosition = 0;
}
}
-(void) rotateLeft {
self.facingPosition--;
if(self.facingPosition < 0) {
self.facingPosition = 3;
}
}
-(void)printMaze {
for (int i = 0; i < row; i++) {
for (int k = 0; k < column; k++) {
printf("%d ",[(NSNumber*)self.maze[i][k] intValue]);
}
printf("\n");
}
}
@end