-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRight.php
54 lines (48 loc) · 1.47 KB
/
Right.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
<?php
class Right implements Action
{
use \MergeOperation;
/**
* @throws ActionError
*/
public function handle(Board $board)
{
$array = $board->getBoard();
for ($i = 0; $i < $board->getSize(); $i++) {
for ($j = $board->getSize() - 1; $j >= 0; $j--) {
if (isset($array[$i][$j])) {
$this->pushTemp($array[$i][$j]);
}
}
repeat:
if (!$this->isEmptyTemp()) {
foreach (array_chunk($this->temp, 2) as $pair) {
if (count($pair) == 1) {
$this->pushResult($pair[0]);
$this->shiftTemp();
break;
}
if ($pair[0] == $pair[1]) {
$this->pushResult(2 * $pair[0]);
$this->shiftTemp();
$this->shiftTemp();
} else {
$this->pushResult($pair[0]);
$this->shiftTemp();
goto repeat;
}
}
}
for ($j = $board->getSize() - 1; $j >= 0; $j--) {
$board->setBoard($i, $j, $this->shiftResult());
}
$this->resetTemp();
$this->resetResult();
}
$this->check($array, $board);
}
public function name(): string
{
return 'right';
}
}