Advertisement
quenti77

Grille and player

Oct 31st, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.51 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Grille</title>
  5.     <style>
  6.         body {
  7.             background-color: #575757;
  8.             color: #cbcbcb;
  9.         }
  10.  
  11.         table {
  12.             border-collapse: inherit;
  13.         }
  14.  
  15.         th, td {
  16.             border: 2px solid #009933;
  17.             text-align: center;
  18.         }
  19.  
  20.         td {
  21.             width: 24px;
  22.             height: 24px;
  23.         }
  24.  
  25.         .movablePlayer {
  26.             border: 2px solid #b6a62e;
  27.         }
  28.  
  29.         .conflicPlayer {
  30.             border: 2px solid #e62e00;
  31.         }
  32.  
  33.         .player {
  34.             border: 2px solid #0000e6;
  35.         }
  36.     </style>
  37. </head>
  38. <body>
  39.  
  40. <?php
  41.  
  42. // Les tableaux de la requête SQL
  43.  
  44. // Tableau qui contient les informations de la partie
  45. $party = [
  46.     'id' => 1,
  47.     'nbPlayer' => 4
  48. ];
  49.  
  50. // Tableau qui contient la liste des joueurs
  51. $players = [[
  52.     'id' => 1,
  53.     'x' => 1,
  54.     'y' => 1
  55. ], [
  56.     'id' => 2,
  57.     'x' => 8,
  58.     'y' => 1,
  59. ], [
  60.     'id' => 3,
  61.     'x' => 1,
  62.     'y' => 9,
  63. ], [
  64.     'id' => 4,
  65.     'x' => 3,
  66.     'y' => 9
  67. ]
  68. ];
  69.  
  70. // Une sous fonction pour connaitre un player
  71. function checkPosition($player, $x, $y)
  72. {
  73.     if ($player['x'] == $x and $player['y'] == $y) {
  74.         return $player;
  75.     }
  76.     return null;
  77. }
  78.  
  79. // Une fonction pour savoir si la case est un joueur
  80. function getPlayer($players, $x, $y)
  81. {
  82.     foreach ($players as $player) {
  83.         if (checkPosition($player, $x, $y)) {
  84.             return $player;
  85.         }
  86.     }
  87.     return null;
  88. }
  89.  
  90. function reposPlayer($x, $y, $width, $height)
  91. {
  92.     $px = $x;
  93.     if ($px < 0) {
  94.         $px = $width - 1;
  95.     } else if ($px > ($width - 1)) {
  96.         $px = 0;
  97.     }
  98.  
  99.     $py = $y;
  100.     if ($py < 0) {
  101.         $py = $height - 1;
  102.     } else if ($py > ($height - 1)) {
  103.         $py = 0;
  104.     }
  105.  
  106.     return [
  107.         'x' => $px,
  108.         'y' => $py
  109.     ];
  110. }
  111.  
  112. function movablePlayer($player, $x, $y, $width, $height)
  113. {
  114.     $px = $player['x'];
  115.     $py = $player['y'];
  116.  
  117.     $pos = [
  118.         reposPlayer($px - 1, $py,     $width, $height),
  119.         reposPlayer($px,     $py - 1, $width, $height),
  120.         reposPlayer($px + 1, $py,     $width, $height),
  121.         reposPlayer($px,     $py + 1, $width, $height)
  122.     ];
  123.  
  124.     foreach($pos as $p) {
  125.         if (checkPosition(['x' => $p['x'], 'y' => $p['y']], $x, $y)) {
  126.             return $player;
  127.         }
  128.     }
  129.  
  130.     return null;
  131. }
  132.  
  133. $width = 3;
  134. $height = 3;
  135. if ($party['nbPlayer'] === 4) {
  136.     $width = 10;
  137.     $height = 10;
  138. }
  139.  
  140. echo '<table>';
  141. for ($y = 0; $y < $height; $y += 1) {
  142.     echo '<tr>';
  143.  
  144.     for ($x = 0; $x < $width; $x += 1) {
  145.         $class = '';
  146.         $content = "{$x} {$y}";
  147.  
  148.         $player = getPlayer($players, $x, $y);
  149.         if ($player) {
  150.             $class = 'player';
  151.             $content = "{$player['id']}";
  152.         } else {
  153.             $result = [];
  154.             foreach ($players as $player) {
  155.                 $player = movablePlayer($player, $x, $y, $width, $height);
  156.                 if ($player) {
  157.                     $result[] = $player;
  158.                 }
  159.             }
  160.  
  161.             $cnt = count($result);
  162.             if ($cnt == 1) {
  163.                 $class = 'movablePlayer';
  164.             } else if ($cnt > 1) {
  165.                 $class = 'conflicPlayer';
  166.             }
  167.         }
  168.  
  169.         if (!empty($class)) {
  170.             $class = "class='{$class}'";
  171.         }
  172.         echo "<td {$class}>{$content}</td>";
  173.     }
  174.  
  175.     echo '</tr>';
  176. }
  177. echo '</table>';
  178.  
  179. ?>
  180.  
  181. </body>
  182. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement