@@ -60,6 +60,9 @@ discard block |
||
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | + /** |
|
| 64 | + * @param string $alias |
|
| 65 | + */ |
|
| 63 | 66 | public function hasAction($alias): bool |
| 64 | 67 | { |
| 65 | 68 | return array_key_exists($alias, $this->actions); |
@@ -140,6 +143,9 @@ discard block |
||
| 140 | 143 | $this->getAction($operation['action'])->execute($this, $alias, $operation); |
| 141 | 144 | } |
| 142 | 145 | |
| 146 | + /** |
|
| 147 | + * @param string $alias |
|
| 148 | + */ |
|
| 143 | 149 | public function getActorNextMove($alias): array |
| 144 | 150 | { |
| 145 | 151 | list($x, $y) = $this->getActorPosition($alias); |
@@ -163,6 +169,9 @@ discard block |
||
| 163 | 169 | return $this->functions[$name]; |
| 164 | 170 | } |
| 165 | 171 | |
| 172 | + /** |
|
| 173 | + * @param string $alias |
|
| 174 | + */ |
|
| 166 | 175 | public function moveActor($alias) |
| 167 | 176 | { |
| 168 | 177 | $moveMap = [ |
@@ -191,6 +200,9 @@ discard block |
||
| 191 | 200 | { |
| 192 | 201 | } |
| 193 | 202 | |
| 203 | + /** |
|
| 204 | + * @param string $field |
|
| 205 | + */ |
|
| 194 | 206 | public function setField($x, $y, $field) |
| 195 | 207 | { |
| 196 | 208 | $this->fields[$x][$y] = $field; |
@@ -206,6 +218,9 @@ discard block |
||
| 206 | 218 | return $this->actors[$alias]['direction']; |
| 207 | 219 | } |
| 208 | 220 | |
| 221 | + /** |
|
| 222 | + * @param string $alias |
|
| 223 | + */ |
|
| 209 | 224 | public function setActorDirection($alias, $direction) |
| 210 | 225 | { |
| 211 | 226 | $this->actors[$alias]['direction'] = $direction; |
@@ -216,6 +231,10 @@ discard block |
||
| 216 | 231 | return $this->actors[$alias]['pick']; |
| 217 | 232 | } |
| 218 | 233 | |
| 234 | + /** |
|
| 235 | + * @param string $alias |
|
| 236 | + * @param string|null $pick |
|
| 237 | + */ |
|
| 219 | 238 | public function setActorPick($alias, $pick) |
| 220 | 239 | { |
| 221 | 240 | $this->actors[$alias]['pick'] = $pick; |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | public function addActions(array $actions) |
| 36 | 36 | { |
| 37 | - foreach($actions as $action) { |
|
| 37 | + foreach ($actions as $action) { |
|
| 38 | 38 | $this->addAction($action); |
| 39 | 39 | } |
| 40 | 40 | } |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | |
| 47 | 47 | public function getAction(string $alias): ActionInterface |
| 48 | 48 | { |
| 49 | - if(!$this->hasAction($alias)) { |
|
| 49 | + if (!$this->hasAction($alias)) { |
|
| 50 | 50 | throw new \RuntimeException(sprintf('Action %s does not exist!', $alias)); |
| 51 | 51 | } |
| 52 | 52 | |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | |
| 56 | 56 | public function addFieldTypes(array $types) |
| 57 | 57 | { |
| 58 | - foreach($types as $alias => $symbol) { |
|
| 58 | + foreach ($types as $alias => $symbol) { |
|
| 59 | 59 | $this->addFieldType($alias, $symbol); |
| 60 | 60 | } |
| 61 | 61 | } |
@@ -76,9 +76,9 @@ discard block |
||
| 76 | 76 | $lines = explode("\n", trim($string)); |
| 77 | 77 | $height = \count($lines); |
| 78 | 78 | $width = \strlen(trim($lines[0])); |
| 79 | - for($i = 0; $i < $height; $i++) { |
|
| 79 | + for ($i = 0; $i < $height; $i++) { |
|
| 80 | 80 | $line = trim($lines[$i]); |
| 81 | - for($j = 0; $j < $width; $j++) { |
|
| 81 | + for ($j = 0; $j < $width; $j++) { |
|
| 82 | 82 | $this->fields[$i][$j] = $types[$line[$j]]; |
| 83 | 83 | } |
| 84 | 84 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | |
| 106 | 106 | public function addFunctions(array $functions) |
| 107 | 107 | { |
| 108 | - foreach($functions as $name => $program) { |
|
| 108 | + foreach ($functions as $name => $program) { |
|
| 109 | 109 | $this->functions[$name] = $program; |
| 110 | 110 | } |
| 111 | 111 | } |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | |
| 131 | 131 | public function runActorProgram($alias, array $program) |
| 132 | 132 | { |
| 133 | - foreach($program as $operation) { |
|
| 133 | + foreach ($program as $operation) { |
|
| 134 | 134 | $this->runActorOperation($alias, $operation); |
| 135 | 135 | } |
| 136 | 136 | } |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | $newY = $actor['y'] + $y; |
| 179 | 179 | |
| 180 | 180 | $this->debug('Move [%s:%s] -> [%s:%s]', $actor['y'], $actor['x'], $newY, $newX); |
| 181 | - if('wall' === $this->fields[$newX][$newY]) { |
|
| 181 | + if ('wall' === $this->fields[$newX][$newY]) { |
|
| 182 | 182 | throw new \RuntimeException(sprintf('Wall at [%s, %s]', $newX, $newY)); |
| 183 | 183 | } |
| 184 | 184 | |
@@ -230,8 +230,8 @@ discard block |
||
| 230 | 230 | { |
| 231 | 231 | $return = ''; |
| 232 | 232 | $return .= "\n"; |
| 233 | - for($i = 0; $i < $this->height; $i++) { |
|
| 234 | - for($j = 0; $j < $this->width; $j++) { |
|
| 233 | + for ($i = 0; $i < $this->height; $i++) { |
|
| 234 | + for ($j = 0; $j < $this->width; $j++) { |
|
| 235 | 235 | $symbol = $this->fieldTypes[$this->fields[$i][$j]]; |
| 236 | 236 | $isActor = array_reduce($this->actors, function($state, array $item) use ($i, $j) { |
| 237 | 237 | $state += $item['x'] === $i && $item['y'] === $j; |
@@ -241,10 +241,10 @@ discard block |
||
| 241 | 241 | $state += $item['x'] === $i && $item['y'] === $j; |
| 242 | 242 | return $state; |
| 243 | 243 | }, 0); |
| 244 | - if($isExit) { |
|
| 244 | + if ($isExit) { |
|
| 245 | 245 | $symbol = 'E'; |
| 246 | 246 | } |
| 247 | - if($isActor) { |
|
| 247 | + if ($isActor) { |
|
| 248 | 248 | $symbol = 'A'; |
| 249 | 249 | } |
| 250 | 250 | $return .= $symbol; |
@@ -9,7 +9,7 @@ |
||
| 9 | 9 | { |
| 10 | 10 | public function execute(Board $board, string $alias, array $operation) |
| 11 | 11 | { |
| 12 | - for($i = 0; $i < $operation['iterations']; $i++) { |
|
| 12 | + for ($i = 0; $i < $operation['iterations']; $i++) { |
|
| 13 | 13 | $board->runActorProgram($alias, $operation['program']); |
| 14 | 14 | } |
| 15 | 15 | } |
@@ -10,14 +10,14 @@ |
||
| 10 | 10 | public function execute(Board $board, string $alias, array $operation) |
| 11 | 11 | { |
| 12 | 12 | $length = $this->getMoveLength($board, $operation); |
| 13 | - for($i = 0; $i < $length; $i++) { |
|
| 13 | + for ($i = 0; $i < $length; $i++) { |
|
| 14 | 14 | $board->moveActor($alias); |
| 15 | 15 | } |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | private function getMoveLength(Board $board, array $operation) |
| 19 | 19 | { |
| 20 | - if(is_numeric($operation['distance'])) { |
|
| 20 | + if (is_numeric($operation['distance'])) { |
|
| 21 | 21 | $length = $operation['distance']; |
| 22 | 22 | $board->debug('Move Length[%s]', $length); |
| 23 | 23 | return $length; |
@@ -21,11 +21,11 @@ |
||
| 21 | 21 | $newX = $x + static::$moveMap[$direction][0]; |
| 22 | 22 | $newY = $y + static::$moveMap[$direction][1]; |
| 23 | 23 | |
| 24 | - if('up' === $operation['direction']) { |
|
| 24 | + if ('up' === $operation['direction']) { |
|
| 25 | 25 | $board->debug('Pick[up]'); |
| 26 | 26 | $board->setField($newX, $newY, 'ground'); |
| 27 | 27 | $board->setActorPick($alias, 'brick'); |
| 28 | - } elseif('down' === $operation['direction']) { |
|
| 28 | + } elseif ('down' === $operation['direction']) { |
|
| 29 | 29 | $board->debug('Pick[down]'); |
| 30 | 30 | $board->setField($newX, $newY, 'brick'); |
| 31 | 31 | $board->setActorPick($alias, null); |
@@ -17,18 +17,18 @@ |
||
| 17 | 17 | public function execute(Board $board, string $alias, array $operation) |
| 18 | 18 | { |
| 19 | 19 | $iteration = 0; |
| 20 | - while(true) { |
|
| 20 | + while (true) { |
|
| 21 | 21 | $left = (string)$board->getVariable($operation['left']); |
| 22 | 22 | $board->debug(sprintf('While Evaluate %s %s %s', $left, $operation['operator'], $operation['right'])); |
| 23 | - if($operation['operator'] === 'is' && $left === (string)$operation['right']) { |
|
| 23 | + if ($operation['operator'] === 'is' && $left === (string)$operation['right']) { |
|
| 24 | 24 | $board->runActorProgram($alias, $operation['program']); |
| 25 | - } elseif($operation['operator'] === 'not' && $left !== (string)$operation['right']) { |
|
| 25 | + } elseif ($operation['operator'] === 'not' && $left !== (string)$operation['right']) { |
|
| 26 | 26 | $board->runActorProgram($alias, $operation['program']); |
| 27 | 27 | } else { |
| 28 | 28 | $board->debug('While LoopEnd'); |
| 29 | 29 | break; |
| 30 | 30 | } |
| 31 | - if($iteration > $this->iterations) { |
|
| 31 | + if ($iteration > $this->iterations) { |
|
| 32 | 32 | $board->debug('Iterations exceeded!'); |
| 33 | 33 | break; |
| 34 | 34 | } |
@@ -10,7 +10,7 @@ |
||
| 10 | 10 | public function execute(Board $board, string $alias, array $operation) |
| 11 | 11 | { |
| 12 | 12 | list($newX, $newY) = $board->getActorNextMove($alias); |
| 13 | - if('door' === $board->getField($newX, $newY)) { |
|
| 13 | + if ('door' === $board->getField($newX, $newY)) { |
|
| 14 | 14 | $board->setField($newX, $newY, 'ground'); |
| 15 | 15 | $board->debug('Door Open[%s:%s]', $newY, $newX); |
| 16 | 16 | return; |
@@ -9,9 +9,9 @@ discard block |
||
| 9 | 9 | $lines = explode("\n", trim($code)); |
| 10 | 10 | $tree = $this->buildTree($lines); |
| 11 | 11 | $functions = []; |
| 12 | - foreach($tree as $fn) { |
|
| 12 | + foreach ($tree as $fn) { |
|
| 13 | 13 | $tokens = explode(' ', $fn['line']); |
| 14 | - if(!$tokens || !$fn['line']) { |
|
| 14 | + if (!$tokens || !$fn['line']) { |
|
| 15 | 15 | continue; |
| 16 | 16 | } |
| 17 | 17 | $functions[$tokens[1]] = $this->compileTree($board, $fn['sub']); |
@@ -23,16 +23,16 @@ discard block |
||
| 23 | 23 | private function compileTree(Board $board, array $tree): array |
| 24 | 24 | { |
| 25 | 25 | $code = []; |
| 26 | - while(true) { |
|
| 27 | - if(!$tree) { |
|
| 26 | + while (true) { |
|
| 27 | + if (!$tree) { |
|
| 28 | 28 | break; |
| 29 | 29 | } |
| 30 | 30 | $line = array_shift($tree); |
| 31 | 31 | $tokens = explode(' ', $line['line']); |
| 32 | - if(!$tokens || !$line['line']) { |
|
| 32 | + if (!$tokens || !$line['line']) { |
|
| 33 | 33 | continue; |
| 34 | 34 | } |
| 35 | - switch($tokens[0]) { |
|
| 35 | + switch ($tokens[0]) { |
|
| 36 | 36 | case 'for': { |
| 37 | 37 | $code[] = [ |
| 38 | 38 | 'action' => 'for', |
@@ -77,15 +77,15 @@ discard block |
||
| 77 | 77 | { |
| 78 | 78 | $action = array_shift($tokens); |
| 79 | 79 | $number = 1; |
| 80 | - if(is_numeric($action)) { |
|
| 80 | + if (is_numeric($action)) { |
|
| 81 | 81 | $number = $action; |
| 82 | 82 | $action = array_shift($tokens); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | $code = []; |
| 86 | - for($i = 0; $i < $number; $i++) { |
|
| 86 | + for ($i = 0; $i < $number; $i++) { |
|
| 87 | 87 | $args = $board->getAction($action)->getArguments(); |
| 88 | - if(\count($args) !== \count($tokens)) { |
|
| 88 | + if (\count($args) !== \count($tokens)) { |
|
| 89 | 89 | throw new \RuntimeException(sprintf('Action args %s does not match tokens %s!', json_encode($args), json_encode($tokens))); |
| 90 | 90 | } |
| 91 | 91 | $code[] = array_merge(['action' => $action], array_combine($args, $tokens)); |
@@ -97,11 +97,11 @@ discard block |
||
| 97 | 97 | private function buildTree(array $lines): array |
| 98 | 98 | { |
| 99 | 99 | $tree = []; |
| 100 | - while($lines) { |
|
| 100 | + while ($lines) { |
|
| 101 | 101 | $line = array_shift($lines); |
| 102 | 102 | $level = $this->getLineLevel($line); |
| 103 | 103 | $subLines = []; |
| 104 | - while($lines && $this->getLineLevel($lines[0]) > $level) { |
|
| 104 | + while ($lines && $this->getLineLevel($lines[0]) > $level) { |
|
| 105 | 105 | $subLines[] = array_shift($lines); |
| 106 | 106 | } |
| 107 | 107 | $tree[] = [ |