<?php
ob_start();
$start = microtime(true);
$db = new PDO('mysql:host=127.0.0.1;dbname=sword;charset=UTF8', 'root', '');
function calc($x, $y, $set = array()) {
global $dash, $size;
if (empty($dash[$x][$y])) {
return $set;
}
$set[$x . '_' . $y] = $dash[$x][$y];
// echo '<p>' . $x . '-' . $y . '</p>';
for ($i = 0; $i < 4; ++$i) {
switch ($i) {
case 0:
$x2 = $x + 1;
if ($x2 <= $size[0] && isset($dash[$x2][$y]) && $dash[$x2][$y] == $dash[$x][$y]) {
if (empty($set[$x2 . '_' . $y])) {
$set = calc($x2, $y, $set);
}
}
break;
case 1:
$x2 = $x - 1;
if ($x2 >= 0 && isset($dash[$x2][$y]) && $dash[$x2][$y] == $dash[$x][$y]) {
if (empty($set[$x2 . '_' . $y])) {
$set = calc($x2, $y, $set);
}
}
break;
case 2:
$y2 = $y + 1;
if ($y2 <= $size[1] && isset($dash[$x][$y2]) && $dash[$x][$y2] == $dash[$x][$y]) {
if (empty($set[$x . '_' . $y2])) {
$set = calc($x, $y2, $set);
}
}
break;
case 3:
$y2 = $y - 1;
if ($y2 >= 0 && isset($dash[$x][$y2]) && $dash[$x][$y2] == $dash[$x][$y]) {
if (empty($set[$x . '_' . $y2])) {
$set = calc($x, $y2, $set);
}
}
break;
}
}
return $set;
}
$size = [3, 3];
$_areas = $db -> prepare('SELECT * FROM `areas`');
$_areas -> execute();
$areas = $_areas -> fetchAll();
$_user = $db -> prepare('SELECT * FROM `user` WHERE `id` = ?');
$_user -> execute([1]);
$user = $_user -> fetch();
echo '
<style>
a{
border:none;
text-decoration: none;
margin:0;
padding:0;
width:16px;
height:16px;
}
</style>
';
if(isset($_GET['x']) and isset($_GET['y']) and isset($_GET['area'])){
$_areas = $db -> prepare('SELECT * FROM `area` WHERE `id` = ?');
$_areas -> execute([(int)$_GET['area']]);
$area = $_areas -> fetch();
if($area){
$_check = $db->prepare('SELECT * FROM `areas` WHERE `x` = ? AND `y` = ? LIMIT 1');
$_check -> execute([(int)$_GET['x'], (int)$_GET['y']]);
$check = $_check->fetch();
if($check){
header('Location:/area.php');
exit;
}
else{
$__table = $db->prepare('INSERT INTO `areas` SET `user` = ?, `area` = ?, `x` = ?, `y` = ?');
$__table->execute([1,(int)$_GET['area'],(int)$_GET['x'],(int)$_GET['y']]);
}
header('Location:/area.php');
exit;
}
}
foreach ($areas as $area) {
$dash[$area['x']][$area['y']] = $area['area'];
}
for ($y = 0; $y < $size[0]; ++$y) {
for ($x = 0; $x < $size[0]; ++$x) {
echo $dash[$x][$y] ? '
<img src="images/areas/'.$dash[$x][$y].'.png" />
' : '
<a href="?x='.$x.'&y='.$y.'&area=1">
<img src="images/areas/0.png" />
</a>';
}
echo '
<br />
';
}
echo count(calc(1, 1));
$time = microtime(true) - $start;
printf('<br />Скрипт выполнялся %.4F сек.', $time);
?>