php ob_start start microtime true db new PDO mysql host 127 dbname swo

  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
<?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);
?>