Advertisement
Venciity

Chess

Aug 24th, 2014
2,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.36 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title></title>
  5.     <style>
  6.         label, textarea, input {
  7.             display: block;
  8.         }
  9.         input[type="text"] {
  10.             width: 700px;
  11.         }
  12.     </style>
  13. </head>
  14. <body>
  15. <form method="get" action="">
  16.     <label>Board:</label>
  17.     <input type="text" name="board" value="R-H-B-K-Q-B-H-R/P-P- -P-P- -P-P/ - -P- - - - - / - - - - -P- - / - - - - - - - / -P- - - - - -H/P- -P-P-P-P-P-P/R-H-B-K-Q-B- -R"/>
  18.     <input type="submit"/>
  19. </form>
  20. <?php
  21. $stringBoard = $_GET['board'];
  22. $rows = explode("/", $stringBoard);
  23. //var_dump($rows);
  24.  
  25. $isValid = isValidChessboard($rows);
  26. if (!$isValid) {
  27.     echo "<h1>Invalid chess board</h1>";
  28.     return;
  29. }
  30.  
  31. $figures = [];
  32.  
  33.     echo "<table>";
  34.     for ($i = 0; $i < count($rows); $i++) {
  35.         echo "<tr>";
  36.         $currentRow = explode("-", $rows[$i]);
  37.         for ($j = 0; $j < count($currentRow); $j++) {
  38.             echo "<td>" . $currentRow[$j] . "</td>";
  39.             if ($currentRow[$j] === 'B') {
  40.                 if (isset($figures['Bishop'])) {
  41.                     $figures['Bishop']++;
  42.                 }
  43.                 else {
  44.                     $figures['Bishop'] = 1;
  45.                 }
  46.             }
  47.             else if ($currentRow[$j] === 'H') {
  48.                 if (isset($figures['Horseman'])) {
  49.                     $figures['Horseman']++;
  50.                 }
  51.                 else {
  52.                     $figures['Horseman'] = 1;
  53.                 }
  54.             }
  55.             else if ($currentRow[$j] === 'K') {
  56.                 if (isset($figures['King'])) {
  57.                     $figures['King']++;
  58.                 }
  59.                 else {
  60.                     $figures['King'] = 1;
  61.                 }
  62.             }
  63.             else if ($currentRow[$j] === 'Q') {
  64.                 if (isset($figures['Queen'])) {
  65.                     $figures['Queen']++;
  66.                 }
  67.                 else {
  68.                     $figures['Queen'] = 1;
  69.                 }
  70.             }
  71.             else if ($currentRow[$j] === 'P') {
  72.                 if (isset($figures['Pawn'])) {
  73.                     $figures['Pawn']++;
  74.                 }
  75.                 else {
  76.                     $figures['Pawn'] = 1;
  77.                 }
  78.             }
  79.             else if ($currentRow[$j] === 'R') {
  80.                 if (isset($figures['Rook'])) {
  81.                     $figures['Rook']++;
  82.                 }
  83.                 else {
  84.                     $figures['Rook'] = 1;
  85.                 }
  86.             }
  87.         }
  88.         echo '</tr>';
  89.     }
  90.     echo '</table>';
  91.     ksort($figures);
  92.     echo json_encode($figures);
  93.  
  94.  
  95.  
  96. function isValidChessboard($lines) {
  97.     // check if rows == 8
  98.     if (count($lines) != 8) {
  99.         return false;
  100.     }
  101.     foreach ($lines as $line) {
  102.         // check if each row has length 15 ( all "-" + pieces)
  103.         if (strlen($line) != 15) {
  104.             return false;
  105.         }
  106.         // check if all letters are valid pieces (excluding "-" on odd positions)
  107.         for ($i = 0; $i < strlen($line); $i += 2) {
  108.             if (!isPieceOrEmpty($line[$i])) {
  109.                 return false;
  110.             }
  111.         }
  112.     }
  113.     return true;
  114. }
  115.  
  116. function isPieceOrEmpty($letter) {
  117.     return $letter == "R" || $letter == "B" || $letter == "H" ||
  118.     $letter == "Q" || $letter == "K" || $letter == "P" || $letter == " ";
  119. }
  120. ?>
  121. </body>
  122. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement