Valleri

Chess

Aug 25th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1. <?php
  2. //$str = " - - - - - - - / - - - - - - - / - - - - - - - / - - - - - - - / - - - - - - - / - - - - - - - / - - - - - - - / - - - - - - - ";
  3. $str = $_GET['board'];
  4. $map = array(
  5.     "Bishop" => 0,
  6.     "Horseman" => 0,
  7.     "King" => 0,
  8.     "Pawn" => 0,
  9.     "Queen" => 0,
  10.     "Rook" => 0
  11. );
  12.  
  13. $items = explode('/', $str);
  14. if(count($items) != 8) {
  15.     echo "<h1>Invalid chess board</h1>";
  16.     die;
  17. }
  18. $result = '<table>';
  19.  
  20. for($i = 0; $i < count($items) ;$i++) {
  21.     $result .= '<tr>';
  22.     $currLine = explode('-', $items[$i]);
  23.     if(count($currLine) != 8) {
  24.         echo "<h1>Invalid chess board</h1>";
  25.         die;
  26.     }
  27.     for($j = 0; $j < count($currLine) ;$j++) {
  28.         if($currLine[$j] == ' ') {
  29.             $result .= '<td> </td>';
  30.         }
  31.         else {
  32.             $figure = $currLine[$j];
  33.             $result .= '<td>' . $figure . '</td>';
  34.  
  35.             if($figure == 'B') {
  36.                 $map['Bishop']++;
  37.             }
  38.             else if($figure == 'H') {
  39.                 $map['Horseman']++;
  40.             }
  41.             else if($figure == 'K') {
  42.                 $map['King']++;
  43.             }
  44.             else if($figure == 'P') {
  45.                 $map['Pawn']++;
  46.             }
  47.             else if($figure == 'Q') {
  48.                 $map['Queen']++;
  49.             }
  50.             else if($figure == 'R') {
  51.                 $map['Rook']++;
  52.             }
  53.              else {
  54.                 echo "<h1>Invalid chess board</h1>";
  55.                 die;
  56.             }
  57.         }
  58.     }
  59.     $result .= '</tr>';
  60. }
  61. $result .= '</table>';
  62.  
  63.  
  64. echo $result;
  65. $map = array_filter($map, function ($var) {
  66.     if($var == 0) {
  67.         return false;
  68.     }
  69.     return true;
  70. });
  71. if(count($map) > 0) {
  72.     echo json_encode($map);
  73. }
  74. ?>
Add Comment
Please, Sign In to add comment