Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- session_unset();
- if (!isset($_SESSION["poskusi"])) {
- $_SESSION["poskusi"] = 0;
- }
- $_SESSION["ladja5"] = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]];
- do {
- $passed = false;
- $posX = rand(0, 9);
- $posY = rand(0, 9);
- $heading = rand(0, 3);
- if ($heading == 0) {
- if ($posY - 4 >= 0) {
- for ($i=0; $i < 5; $i++) {
- $_SESSION["ladja5"][$i][0] = $posX;
- $_SESSION["ladja5"][$i][1] = $posY - $i;
- }
- $passed = true;
- }
- } else if ($heading == 1) {
- if ($posX + 4 <= 9) {
- for ($i=0; $i < 5; $i++) {
- $_SESSION["ladja5"][$i][0] = $posX + $i;
- $_SESSION["ladja5"][$i][1] = $posY;
- }
- $passed = true;
- }
- } else if ($heading == 2) {
- if ($posY + 4 <= 9) {
- for ($i=0; $i < 5; $i++) {
- $_SESSION["ladja5"][$i][0] = $posX;
- $_SESSION["ladja5"][$i][1] = $posY + $i;
- }
- $passed = true;
- }
- } else {
- if ($posX - 4 >= 0) {
- for ($i=0; $i < 5; $i++) {
- $_SESSION["ladja5"][$i][0] = $posX - $i;
- $_SESSION["ladja5"][$i][1] = $posY;
- }
- $passed = true;
- }
- }
- } while ($passed != true);
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8"/>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <style>
- table, tr, td {
- border: 1px solid black;
- background-color: gray;
- border-collapse: collapse;
- }
- </style>
- <script>
- $(document).ready(function(){
- $(".preveri").click(function(el){
- var url = "preveri.php?x=" + el.target.cellIndex + "&y=" + el.target.parentNode.rowIndex;
- $.get(url, function(data){
- var odgovor = JSON.parse(data);
- if (odgovor.zadetek) {
- $(el.target).css("background-color", "red");
- } else {
- $(el.target).css("background-color", "blue");
- }
- $(el.target).unbind("click")
- if(odgovor.konec) {
- alert("Čestitam. Potopil si ladjo v " + (odgovor.poskusi+1) + " poskusih.");
- $(".preveri").unbind("click");
- }
- });
- });
- });
- </script>
- </head>
- <body>
- <?php
- echo "<table id='tabela'>";
- for ($i=0; $i < 10; $i++) {
- echo "<tr>";
- for ($j=0; $j < 10; $j++) {
- echo "<td class='preveri' style='width: 40px; height: 40px;'></td>";
- }
- echo "</tr>";
- }
- echo "</table>";
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement