<?php
/*
* ////////////////////////////////////////////////////////////////////////////////
* // JohnCMS                             Content Management System              //
* // Официальный сайт сайт проекта:      http://johncms.com                     //
* // Дополнительный сайт поддержки:      http://gazenwagen.com                  //
* ////////////////////////////////////////////////////////////////////////////////
* // JohnCMS core team:                                                         //
* // Евгений Рябинин aka john77          john77@gazenwagen.com                  //
* // Олег Касьянов aka AlkatraZ          alkatraz@gazenwagen.com                //
* //                                                                            //
* // Информацию о версиях смотрите в прилагаемом файле version.txt              //
* ////////////////////////////////////////////////////////////////////////////////
* // Пятнашки                                                                   //
* // Автор Regan                                                                //
* ////////////////////////////////////////////////////////////////////////////////
*/
define('_IN_JOHNCMS', 1);

$headmod = 'game_Pyatnashki';
$textl = 'Пятнашки';
$rootpath = '../../';
require_once ("../../incfiles/core.php");
require_once ("../../incfiles/head.php");

if ($user_id) {

    echo "<div class='phdr'>Пятнашки</div>";

    switch ($act) {
        case "hod":
            $ms = mysql_fetch_array(mysql_query("SELECT *
			FROM `game` 
			WHERE type='Pyatnashki' and users='" . $user_id . "' LIMIT 1;"));

            if (!$ms['id']) {
                header("Location: ?");
            }

            $motion = abs(intval($_POST['motion']));

            if ($motion > 15) {
                echo functions::display_error("Невыйдет!");
                echo "<div class='menu'><a href='?act=game'>Назад</a></div>";
                require_once ("../../incfiles/end.php");
                exit;
            }

            $dump = unserialize($ms['dump']);

            switch ($motion) {
                case 0:
                    if ($dump[($motion + 1)] == 0) {
                        $up = $motion + 1;
                    } elseif ($dump[($motion + 4)] == 0) {
                        $up = $motion + 4;
                    } else {
                        $up = "Error";
                    }
                    break;
                case 1:
                case 2:
                    if ($dump[($motion + 1)] == 0) {
                        $up = $motion + 1;
                    } elseif ($dump[($motion + 4)] == 0) {
                        $up = $motion + 4;
                    } elseif ($dump[($motion - 1)] == 0) {
                        $up = $motion - 1;
                    } else {
                        $up = "Error";
                    }
                    break;
                case 3:
                    if ($dump[($motion - 1)] == 0) {
                        $up = $motion - 1;
                    } elseif ($dump[($motion + 4)] == 0) {
                        $up = $motion + 4;
                    } else {
                        $up = "Error";
                    }
                    break;
                case 4:
                case 8:
                    if ($dump[($motion + 1)] == 0) {
                        $up = $motion + 1;
                    } elseif ($dump[($motion + 4)] == 0) {
                        $up = $motion + 4;
                    } elseif ($dump[($motion - 4)] == 0) {
                        $up = $motion - 4;
                    } else {
                        $up = "Error";
                    }
                    break;
                case 7:
                case 11:
                    if ($dump[($motion - 1)] == 0) {
                        $up = $motion - 1;
                    } elseif ($dump[($motion + 4)] == 0) {
                        $up = $motion + 4;
                    } elseif ($dump[($motion - 4)] == 0) {
                        $up = $motion - 4;
                    } else {
                        $up = "Error";
                    }
                    break;
                case 5:
                case 6:
                case 9:
                case 10:
                    if ($dump[($motion + 1)] == 0) {
                        $up = $motion + 1;
                    } elseif ($dump[($motion + 4)] == 0) {
                        $up = $motion + 4;
                    } elseif ($dump[($motion - 1)] == 0) {
                        $up = $motion - 1;
                    } elseif ($dump[($motion - 4)] == 0) {
                        $up = $motion - 4;
                    } else {
                        $up = "Error";
                    }
                    break;
                case 13:
                case 14:
                    if ($dump[($motion + 1)] == 0) {
                        $up = $motion + 1;
                    } elseif ($dump[($motion - 4)] == 0) {
                        $up = $motion - 4;
                    } elseif ($dump[($motion - 1)] == 0) {
                        $up = $motion - 1;
                    } else {
                        $up = "Error";
                    }
                    break;
                case 12:
                    if ($dump[($motion + 1)] == 0) {
                        $up = $motion + 1;
                    } elseif ($dump[($motion - 4)] == 0) {
                        $up = $motion - 4;
                    } else {
                        $up = "Error";
                    }
                    break;
                case 15:
                    if ($dump[($motion - 1)] == 0) {
                        $up = $motion - 1;
                    } elseif ($dump[($motion - 4)] == 0) {
                        $up = $motion - 4;
                    } else {
                        $up = "Error";
                    }
                    break;
                default:
                    $up = "Error";
            }

            if ($up !== 'Error') {

                $dump_motion = $dump[$motion];
                $dump[$motion] = $dump[$up];
                $dump[$up] = $dump_motion;

                mysql_query("UPDATE `game` SET
				`motion`=motion+1,
				`dump`='" . serialize($dump) . "'
				WHERE type='Pyatnashki' and users='" . $user_id . "' LIMIT 1;") or die('Error: UPDATE game');
            }

            header("Location: ?act=game");
            break;

            ////////////////////////////////////////////////////
            // Играем                                         //
            ////////////////////////////////////////////////////
        case "game":
            $ms = mysql_fetch_array(mysql_query("SELECT *
			FROM `game` 
			WHERE type='Pyatnashki' and users='" . $user_id . "' LIMIT 1;"));

            if (!$ms['id']) {
                header("Location: ?");
            }

            $dump = unserialize($ms['dump']);
            $time_game = time() - $ms['time'];

            echo "<table align=\"center\" width=\"50\" height=\"30\" bgcolor=\"#D2D2D2\" bordercolor=\"#D2D2D2\" bordercolordark=\"#2D2D2D\" bordercolorlight=\"#5A5A5A\"><tr>";

            for ($i = 0; $i < 16; $i++) {

                echo '<td><form action="?&amp;act=hod" method="POST">
				<input type="hidden" name="motion" value="' . $i . '"/>
				<input type="submit"
				style="width: 30px;
				height: 25px;
				font-size: 12px;
				background-color: #747474;
				color: ' . ($dump[$i] == 0 ? 'green' : '#CCCCCC') . '"
				value="' . $dump[$i] . '"/></form></td>';

                if ($i == 3 or $i == 7 or $i == 11) {
                    echo "</tr><tr>";
                }
            }
            echo '</tr></table><div class="gmenu">';

            if ($dump['0'] == 1 and $dump['1'] == 2 and $dump['2'] == 3 and $dump['3'] == 4 and
                $dump['4'] == 5 and $dump['5'] == 6 and $dump['6'] == 7 and $dump['7'] == 8 and
                $dump['8'] == 9 and $dump['9'] == 10 and $dump['10'] == 11 and $dump['11'] == 12 and
                $dump['12'] == 13 and (($dump['13'] == 14 and $dump['14'] == 15) or ($dump['13'] ==
                15 and $dump['14'] == 14))) {
                echo '<h3><span class="green">Вы выиграли!</span></h3>';
            }

            echo "Время игры: " . gmdate('H:i:s', ($time_game)) . "<br/>Ходов: " . $ms['motion'] . "</div>";
            echo "<div class='menu'><a href='?'>Заново</a>";
            echo '<br/><a href="../?">В раздел</a></div>';

            break;
            ////////////////////////////////////////////////////
            // Главная                                        //
            ////////////////////////////////////////////////////
        default:
            $old = time() - (3 * 24 * 3600);

            mysql_query("DELETE FROM `game` WHERE type='Pyatnashki' and
				(`users`='" . $user_id . "' or `time`>='" . $old . "');");
            mysql_query("OPTIMIZE TABLE `game`");

            $arr = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
                '13', '14', '15');

            shuffle($arr);

            mysql_query("INSERT INTO `game` SET
				`time`='" . time() . "',
				`users`='" . $user_id . "',
				`type`='Pyatnashki',
				`dump`='" . serialize($arr) . "';") or die('Error: INSERT INTO game');


            echo "<div class='gmenu'>Цель игры:<br />передвигая костяшки (нажатием кнопки мышки на костяшку рядом с пустой), добится одной из следующих комбинаций.<br />";
            echo '<table align="absmiddle" bgcolor="#E1E1E1" border="1" bordercolordark="#2D2D2D" bordercolorlight="#787878">';
            echo '<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>';
            echo '<tr><td>5</td><td>6</td><td>7</td><td>8</td></tr>';
            echo '<tr><td>9</td><td>10</td><td>11</td><td>12</td></tr>';
            echo '<tr><td>13</td><td>14</td><td>15</td><td>0</td></tr>';
            echo '</table><hr /><table align="absmiddle" bgcolor="#E1E1E1" border="1" bordercolordark="#2D2D2D" bordercolorlight="#787878">';
            echo '<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>';
            echo '<tr><td>5</td><td>6</td><td>7</td><td>8</td></tr>';
            echo '<tr><td>9</td><td>10</td><td>11</td><td>12</td>';
            echo '</tr><tr><td>13</td><td>15</td><td>14</td><td>0</td></tr></table></div>';
            echo "<div class='menu'><a href='?act=game'>Начать игру</a><br />";
            echo '<a href="../?">В раздел</a></div>';
            break;
    }
} else {
    echo functions::display_error("Игра доступна только для зарегистрированых пользователей сайта");
    echo '<a href="../?">В раздел</a>';
}

require_once ("../../incfiles/end.php");

?>