Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Grille</title>
- <style>
- body {
- background-color: #575757;
- color: #cbcbcb;
- }
- table {
- border-collapse: inherit;
- }
- th, td {
- border: 2px solid #009933;
- text-align: center;
- }
- td {
- width: 24px;
- height: 24px;
- }
- .movablePlayer {
- border: 2px solid #b6a62e;
- }
- .conflicPlayer {
- border: 2px solid #e62e00;
- }
- .player {
- border: 2px solid #0000e6;
- }
- </style>
- </head>
- <body>
- <?php
- // Les tableaux de la requête SQL
- // Tableau qui contient les informations de la partie
- $party = [
- 'id' => 1,
- 'nbPlayer' => 4
- ];
- // Tableau qui contient la liste des joueurs
- $players = [[
- 'id' => 1,
- 'x' => 1,
- 'y' => 1
- ], [
- 'id' => 2,
- 'x' => 8,
- 'y' => 1,
- ], [
- 'id' => 3,
- 'x' => 1,
- 'y' => 9,
- ], [
- 'id' => 4,
- 'x' => 3,
- 'y' => 9
- ]
- ];
- // Une sous fonction pour connaitre un player
- function checkPosition($player, $x, $y)
- {
- if ($player['x'] == $x and $player['y'] == $y) {
- return $player;
- }
- return null;
- }
- // Une fonction pour savoir si la case est un joueur
- function getPlayer($players, $x, $y)
- {
- foreach ($players as $player) {
- if (checkPosition($player, $x, $y)) {
- return $player;
- }
- }
- return null;
- }
- function reposPlayer($x, $y, $width, $height)
- {
- $px = $x;
- if ($px < 0) {
- $px = $width - 1;
- } else if ($px > ($width - 1)) {
- $px = 0;
- }
- $py = $y;
- if ($py < 0) {
- $py = $height - 1;
- } else if ($py > ($height - 1)) {
- $py = 0;
- }
- return [
- 'x' => $px,
- 'y' => $py
- ];
- }
- function movablePlayer($player, $x, $y, $width, $height)
- {
- $px = $player['x'];
- $py = $player['y'];
- $pos = [
- reposPlayer($px - 1, $py, $width, $height),
- reposPlayer($px, $py - 1, $width, $height),
- reposPlayer($px + 1, $py, $width, $height),
- reposPlayer($px, $py + 1, $width, $height)
- ];
- foreach($pos as $p) {
- if (checkPosition(['x' => $p['x'], 'y' => $p['y']], $x, $y)) {
- return $player;
- }
- }
- return null;
- }
- $width = 3;
- $height = 3;
- if ($party['nbPlayer'] === 4) {
- $width = 10;
- $height = 10;
- }
- echo '<table>';
- for ($y = 0; $y < $height; $y += 1) {
- echo '<tr>';
- for ($x = 0; $x < $width; $x += 1) {
- $class = '';
- $content = "{$x} {$y}";
- $player = getPlayer($players, $x, $y);
- if ($player) {
- $class = 'player';
- $content = "{$player['id']}";
- } else {
- $result = [];
- foreach ($players as $player) {
- $player = movablePlayer($player, $x, $y, $width, $height);
- if ($player) {
- $result[] = $player;
- }
- }
- $cnt = count($result);
- if ($cnt == 1) {
- $class = 'movablePlayer';
- } else if ($cnt > 1) {
- $class = 'conflicPlayer';
- }
- }
- if (!empty($class)) {
- $class = "class='{$class}'";
- }
- echo "<td {$class}>{$content}</td>";
- }
- echo '</tr>';
- }
- echo '</table>';
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement