This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
/
FifteenControl.php
152 lines (122 loc) Β· 2.85 KB
/
FifteenControl.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
<?php
declare(strict_types=1);
use Nette\Application\UI;
/**
* The Fifteen game control
*/
class FifteenControl extends UI\Control
{
/** @var int */
public $width = 4;
/** @var callable[] function (FifteenControl $sender): void */
public $onAfterClick;
/** @var callable[] function (FifteenControl $sender, int $round): void */
public $onGameOver;
/** @persistent array */
public $order = [];
/** @persistent int */
public $round = 0;
public function __construct()
{
parent::__construct();
$this->order = range(0, $this->width * $this->width - 1);
}
public function handleClick(int $x, int $y): void
{
if (!$this->isClickable($x, $y)) {
throw new UI\BadSignalException('Action not allowed.');
}
$this->move($x, $y);
$this->round++;
$this->onAfterClick($this);
if ($this->order == range(0, $this->width * $this->width - 1)) {
$this->onGameOver($this, $this->round);
}
}
public function handleShuffle(): void
{
$i = 100;
while ($i) {
$x = rand(0, $this->width - 1);
$y = rand(0, $this->width - 1);
if ($this->isClickable($x, $y)) {
$this->move($x, $y);
$i--;
}
}
$this->round = 0;
}
public function getRound(): int
{
return $this->round;
}
public function isClickable(int $x, int $y, string &$rel = null): bool
{
$rel = null;
$pos = $x + $y * $this->width;
$empty = $this->searchEmpty();
$y = (int) ($empty / $this->width);
$x = $empty % $this->width;
if ($x > 0 && $pos === $empty - 1) {
$rel = '-1,';
return true;
}
if ($x < $this->width - 1 && $pos === $empty + 1) {
$rel = '+1,';
return true;
}
if ($y > 0 && $pos === $empty - $this->width) {
$rel = ',-1';
return true;
}
if ($y < $this->width - 1 && $pos === $empty + $this->width) {
$rel = ',+1';
return true;
}
return false;
}
private function move(int $x, int $y): void
{
$pos = $x + $y * $this->width;
$emptyPos = $this->searchEmpty();
$this->order[$emptyPos] = $this->order[$pos];
$this->order[$pos] = 0;
}
private function searchEmpty(): int
{
return array_search(0, $this->order, true);
}
public function render(): void
{
$template = $this->template;
$template->width = $this->width;
$template->order = $this->order;
$template->render(__DIR__ . '/FifteenControl.latte');
}
/**
* Loads params.
*/
public function loadState(array $params): void
{
if (isset($params['order'])) {
$params['order'] = array_map('intval', explode('.', (string) $params['order']));
// validate
$copy = $params['order'];
sort($copy);
if ($copy != range(0, $this->width * $this->width - 1)) {
unset($params['order']);
}
}
parent::loadState($params);
}
/**
* Save params.
*/
public function saveState(array &$params): void
{
parent::saveState($params);
if (isset($params['order'])) {
$params['order'] = implode('.', $params['order']);
}
}
}