-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.php
155 lines (134 loc) · 2.63 KB
/
Map.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
<?php
/*doc Map.php
Versión : 1.0
Fecha : 2018-06-13
Autor : Nelson Sanchez Bernal (nelson@moviltracing.com)
--------------------------------------------------------------
DISCLAIMER:
Use only for didactical purpose.
--------------------------------------------------------------
doc*/
Class Map {
protected $map;
protected $visited = [];
function __construct(){
}
/**
* Create map with data provided
* @param array $data
*/
public function setMap($data){
$this->map = $data;
}
/**
* Print all map
*/
public function printMap(){
echo PHP_EOL;
foreach ($this->map as $key => $value){
foreach ($value as $key2 => $value2){
if ($value2=="S") {
echo "[\xb0]\t";
}elseif ($value2=="null") {
echo "[\x20]\t";
}else{
echo "[".$value2."]\t";
}
}
echo PHP_EOL;
}
}
/**
* Print list of cleaned cells
*/
public function printCleaned(){
$result = $this->getCleaned();
echo "Cleaned : ".implode(",", $result) . PHP_EOL;
}
/**
* Get list of clean cells
* Warning : the list contains original cells marked as clean
*
* @return string[]
*/
public function getCleaned(){
$result = [];
for($i=0; $i<count($this->map); $i++){
for($j=0; $j<count($this->map[$i]); $j++){
if ($this->map[$i][$j]=="C") {
$result[] = "{X:".$j.", Y:".$i."}";
}
}
}
return $result;
}
/**
* Print list of visited cells
*/
public function printVisited(){
$result = $this->getVisited();
echo "Visited : ".implode(",", $result) . PHP_EOL;
}
/**
* Return list of visited cells
*
* @return array
*/
public function getVisited(){
return $this->visited;
}
/**
* Change cell state
*
* @param int $x
* @param int $y
* @param string $state
*/
public function changeCellState($x, $y, $state){
$this->map[$y][$x] = $state;
//$this->setVisited($x, $y);
}
/**
* Add position to cell visited list
*
* @param int $x
* @param int $y
*/
public function setVisited($x, $y){
$this->visited[] = "{X:".$x.", Y:".$y."}";
}
/**
* Return cell state (C,S,null)
*
* @param int $x
* @param int $y
* @return string
*/
public function getCellState($x, $y){
return $this->map[$y][$x];
}
/**
* Determines if the cell given is available to clean.
*
* @param int $x
* @param int $y
* @return boolean
*/
public function isCellAvailable($x, $y){
if ($this->map[$y][$x]=="S") {
return true;
}
return false;
}
/**
* Determines if the cell given exists.
*
* @param int $x
* @param int $y
* @return boolean
*/
public function cellExist($x, $y){
return isset($this->map[$y][$x]);
}
}
?>