-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.php
390 lines (327 loc) · 7.26 KB
/
Robot.php
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/*doc Robot.php
Versión : 1.0
Fecha : 2018-06-13
Autor : Nelson Sanchez Bernal ([email protected])
--------------------------------------------------------------
DESCARGO:
--------------------------------------------------------------
doc*/
Class Robot {
protected $map;
protected $posIni;
protected $posAct;
protected $batteryIni;
protected $batteryEnd;
protected $startCommands;
protected $backStrategy = array(array("TR","A"),
array("TL","B","TR","A"),
array("TL", "TL", "A"),
array("TR", "B", "TR", "A"),
array("TL", "TL", "A")
);
protected $debug = false;
protected $destFile;
function __construct(){}
/**
* Set data to start process
* @param Array $data
* @return Robot
*/
public function setData($data){
// Load initial data
$this->map = new Map();
$this->map->setMap($data['map']);
// Load start pos
$this->posIni = new Position($data['start']['X'], $data['start']['Y'], $data['start']['facing']);
$this->posAct = clone $this->posIni;
// Load Battery Level
$this->batteryIni= intval($data['battery']);
$this->batteryEnd= $this->batteryIni;
// Load program
$this->startCommands = $data['commands'];
return $this;
}
/**
* Start Clean Process
*
* @param string $file Destination file to save process result
* @return boolean
*/
public function startClean($file){
$exito = true;
// Set destination file
$this->destFile = $file;
if ($this->_executeProcess($this->startCommands)==true) {
// Finished OK
}else {
// Finished with error
$exito = false;
}
$this->_createOutput();
$this->_printStepByStep(); // Print final result
return $exito;
}
/**
* Method that execute a list of commands
*
* @param array $commands
* @return boolean
*/
protected function _executeProcess($commands){
foreach ($commands as $value){
// Validate Battery
if ( intval($this->batteryEnd)<=4) {
return false;
}
if ($this->_executeCommand($value)!==true) {
// Error case
if ($value=="A") {
$exito2=false;
$i = 0;
do {
echo "\tApply error commands...".$i.PHP_EOL;
if ($this->_executeBackStrategy($this->backStrategy[$i])===true) {
$exito2 = true;
break;
}
$i++;
} while ($i < count($this->backStrategy) );
//return false;
if ($exito2==false) {
return false;
}
}
}
}
return true;
}
protected function _executeBackStrategy($commands){
foreach ($commands as $value){
// Validate Battery
if ( intval($this->batteryEnd)<=4) {
return false;
}
if ($this->_executeCommand($value)!==true) {
return false;
}
}
return true;
}
/**
* Method tha execute only command
*
* @param unknown $command
* @return boolean
*/
private function _executeCommand($command){
$exito = true;
$bateria = 0;
// TL y TR
// ==> Change orientation
if (strtoupper($command)=="TL" || strtoupper($command)=="TR") {
$bateria = 1;
$this->_changeOrientation($command);
}
// A
// ==> Change pos - Change battery level, change orientation
if (strtoupper($command)=="A" ) {
$bateria = 2;
if ($this->_advance($command)==false) {
$exito = false;
}
}
// B
// ==> Change pos - change cell statte and change battery level
if (strtoupper($command)=="B" ) {
$bateria = 3;
if ($this->_advance($command)==false) {
$exito = false;
}
}
// C
// ==> Change cell state and battery level
if (strtoupper($command)=="C" ) {
$bateria = 5;
if ( $this->map->getCellState($this->posAct->getPos()['X'], $this->posAct->getPos()['Y'])!="C"
&& $this->map->getCellState($this->posAct->getPos()['X'], $this->posAct->getPos()['Y'])!=null
&& $this->batteryEnd>$bateria) {
$this->_changeCellState($command);
}else {
$exito = false;
}
}
if ($exito) {
// Update battery level
$this->_updateBatteryLevel($bateria);
}
// Print Messages
$this->_printStepByStep($command);
return $exito;
}
/**
* Print Battery value in debug mode
*/
private function _printBattery(){
echo "Battery : ".$this->batteryEnd.PHP_EOL;
}
/**
* Set debug value
*
* @param boolean $valor
*/
public function setDebug($valor=false){
$this->debug = $valor;
}
/**
* Print actual data only in debug mode
*/
private function _printStepByStep($command="X"){
if ($this->debug) {
$this->map->printMap();
if ($command!="X") {
echo "Command : ".$command.PHP_EOL;
}
$this->posAct->printPos();
$this->_printBattery();
$this->map->printCleaned();
$this->map->printVisited();
echo PHP_EOL;
sleep(1);
}
}
/**
* Create output file
*
* @return boolean
*/
private function _createOutput(){
$result = array("battery"=>$this->batteryEnd,
"final" => $this->posAct->getPos(),
"cleaned" =>$this->map->getCleaned() ,
"visited" => $this->map->getVisited()
);
$data = json_encode($result);
$exito = file_put_contents ( $this->destFile , $data );
return ($exito!==false)?true:$exito;
}
/**
* Update Battery value
*
* @param int $valor
*/
private function _updateBatteryLevel($valor){
$this->batteryEnd = $this->batteryEnd-$valor;
}
/**
* Change orientation of robot
*
* @param string $command
* @return boolean
*/
private function _changeOrientation($command){
$f = $this->posAct->getFacing();
switch ($f) {
case "N":
if ($command=="TL") {
$new = "W";
}else {
$new = "E";
}
break;
case "S":
if ($command=="TL") {
$new = "E";
}else {
$new = "W";
}
break;
case "E":
if ($command=="TL") {
$new = "N";
}else {
$new = "S";
}
break;
case "W":
if ($command=="TL") {
$new = "S";
}else {
$new = "N";
}
break;
}
$this->posAct->setFacing($new);
return true;
}
/**
* Change the state of the actual position
* Apply for Clean or visited cell
*
* @param string $command
*/
private function _changeCellState($command){
$x = $this->posAct->getPos()['X'];
$y = $this->posAct->getPos()['Y'];
if ($command=="C") {
$this->map->changeCellState($x, $y, "C");
}else {
$this->map->changeCellState($x, $y, "V");
}
}
private function _advance($command) {
$exito = false;
$x = $this->posAct->getPos()['X'];
$y = $this->posAct->getPos()['Y'];
$f = $this->posAct->getPos()['facing'];
switch ($f) {
case "N":
if ($command=="B") {
$x2 = $x;
$y2 = $y+1;
}else {
$x2 = $x;
$y2 = $y-1;
}
break;
case "S":
if ($command=="B") {
$x2 = $x;
$y2 = $y-1;
}else {
$x2 = $x;
$y2 = $y+1;
}
break;
case "E":
if ($command=="B") {
$x2 = $x-1;
$y2 = $y;
}else {
$x2 = $x+1;
$y2 = $y;
}
break;
case "W":
if ($command=="B") {
$x2 = $x+1;
$y2 = $y;
}else {
$x2 = $x-1;
$y2 = $y;
}
break;
}
$newPos = new Position($x2, $y2, $f);
if ($this->map->cellExist($x2, $y2)) {
if ($this->map->isCellAvailable($x2, $y2)) {
$this->posAct = $newPos;
$exito = true;
$this->map->setVisited($x2, $y2);
echo "\t Si pudo avanzar...".PHP_EOL;
}
}
return $exito;
}
}
?>