1 && is_float(log($_GET['n'], 2)))
$n = $_GET['n'];
else $n = 2;
if (key_exists('x', $_GET) && $_GET['x'] >= 0)
$x = $_GET['x'];
else $x = 0;
if (key_exists('y', $_GET) && $_GET['y'] >= 0)
$y = $_GET['y'];
else $y = 0;
echo '';
$triminal = new Triminal($n, $x, $y);
if (key_exists('time_test', $_GET)) {
$chess_array = '';
$triminal->time_test();
}
else {
$chess_array = $triminal->generate();
$triminal->print_statistics();
}
echo '';
?>
error = false;
$this->end = false;
$this->rozmer = $rozmer;
$this->clear_chess_generate();
$this->chess_array[$y][$x] = -1;
if (!$this->time) $this->time = microtime(true);
}
public function __invoke() {
$this->generate();
}
public function generate() {
$this->search_track();
return json_encode($this->chess_array);
}
public function search_track($triminal = array(), $color = 0, $posun = 0 /* == posun po šachovnici */) {
$n = $this->rozmer;
$x = 0; $y = 0;
$posun_max = ($n*$n)-1;
for ($i = 0; $i < count($triminal); $i+=2) { // vyfarbenie posledného nájdeného trimiralu priradením jeho ID
$this->chess_array[ $triminal[$i] ][ $triminal[$i+1] ] = $color;
}
for (; $this->chess_array[$y][$x] != 0 && $posun < $posun_max; $posun++) { // posun na voľné políčko, okdkiaľ sa začne hľadať triminal
$y = floor($posun/$n);
$x = $posun%$n;
if ($y > 4 && $y >= $n-4) {
$posunX = $posun-($n*($n-4));
$x = floor($posunX/4);
$y = ($n-4)+($posunX%4);
}
}
if ($posun == $posun_max) $this->end = true;
if (!$this->end) $this->triminal($y, $x, $color+1, $posun); // určenie natočenia nového triminálu
if ($this->error) { // triminal na danej pozícií neexistuje, preto sa zmaže aj posledný existujúci
$this->clear_trimiral($color);
$this->error = false;
}
}
public function triminal($y, $x, $color, $posun) {
$n = $this->rozmer -1;
for ($e = 0; $e < 12; $e++) { // skúšame všetky natočenia
if ($color%2 == 0) $i = $e; else $i = 12-$e;
if (0 == $i && !$this->end && $x > 0 && $y > 0 && $this->chess_array[$y-1][$x-1] == 0 && $this->chess_array[$y-1][$x ] == 0) $this->search_track(array($y-1, $x-1, $y-1, $x , $y, $x), $color, $posun);
if (1 == $i && !$this->end && $x < $n && $y > 0 && $this->chess_array[$y-1][$x ] == 0 && $this->chess_array[$y-1][$x+1] == 0) $this->search_track(array($y-1, $x , $y-1, $x+1, $y, $x), $color, $posun);
if (2 == $i && !$this->end && $x < $n && $y > 0 && $this->chess_array[$y-1][$x ] == 0 && $this->chess_array[$y ][$x+1] == 0) $this->search_track(array($y-1, $x , $y , $x+1, $y, $x), $color, $posun);
if (3 == $i && !$this->end && $x < $n && $y > 0 && $this->chess_array[$y-1][$x+1] == 0 && $this->chess_array[$y ][$x+1] == 0) $this->search_track(array($y-1, $x+1, $y , $x+1, $y, $x), $color, $posun);
if (4 == $i && !$this->end && $x < $n && $y < $n && $this->chess_array[$y ][$x+1] == 0 && $this->chess_array[$y+1][$x+1] == 0) $this->search_track(array($y , $x+1, $y+1, $x+1, $y, $x), $color, $posun);
if (5 == $i && !$this->end && $x < $n && $y < $n && $this->chess_array[$y ][$x+1] == 0 && $this->chess_array[$y+1][$x ] == 0) $this->search_track(array($y , $x+1, $y, $x, $y+1, $x ), $color, $posun);
if (6 == $i && !$this->end && $x < $n && $y < $n && $this->chess_array[$y+1][$x+1] == 0 && $this->chess_array[$y+1][$x ] == 0) $this->search_track(array($y, $x, $y+1, $x+1, $y+1, $x ), $color, $posun);
if (7 == $i && !$this->end && $x > 0 && $y < $n && $this->chess_array[$y+1][$x ] == 0 && $this->chess_array[$y+1][$x-1] == 0) $this->search_track(array($y, $x, $y+1, $x , $y+1, $x-1), $color, $posun);
if (8 == $i && !$this->end && $x > 0 && $y < $n && $this->chess_array[$y+1][$x ] == 0 && $this->chess_array[$y ][$x-1] == 0) $this->search_track(array($y, $x, $y+1, $x , $y , $x-1), $color, $posun);
if (9 == $i && !$this->end && $x > 0 && $y < $n && $this->chess_array[$y+1][$x-1] == 0 && $this->chess_array[$y ][$x-1] == 0) $this->search_track(array($y, $x-1, $y, $x, $y+1, $x-1), $color, $posun);
if (10== $i && !$this->end && $x > 0 && $y > 0 && $this->chess_array[$y ][$x-1] == 0 && $this->chess_array[$y-1][$x-1] == 0) $this->search_track(array($y-1, $x-1, $y, $x, $y , $x-1), $color, $posun);
if (11== $i && !$this->end && $x > 0 && $y > 0 && $this->chess_array[$y ][$x-1] == 0 && $this->chess_array[$y-1][$x ] == 0) $this->search_track(array($y-1, $x , $y, $x, $y , $x-1), $color, $posun);
}
if ($this->chess_array[$y][$x] == 0) { // žiadne natočenie nevyhovuje - triminal neexistuje
$this->error = true;
}
}
public function time_test() {
$this->time = microtime(true);
for ($y = 0; $y < $this->rozmer; $y++) {
for ($x = 0; $x < $this->rozmer; $x++) {
$this->__construct($this->rozmer, $x, $y);
$this->search_track();
}
}
return $this->print_statistics();
}
function clear_trimiral($color) {
for ($y = 0; $y < $this->rozmer; $y++) {
for ($x = 0; $x < $this->rozmer; $x++) {
if ($this->chess_array[$y][$x] >= $color)
$this->chess_array[$y][$x] = 0;
}
}
}
public function clear_chess_generate() {
$this->chess_array = array();
for ($y = 0; $y < $this->rozmer; $y++) {
$this->chess_array[$y] = array();
for ($x = 0; $x < $this->rozmer; $x++)
$this->chess_array[$y][$x] = 0;
}
}
public function print_statistics() {
echo '';
}
}
?>