Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //$str = " - - - - - - - / - - - - - - - / - - - - - - - / - - - - - - - / - - - - - - - / - - - - - - - / - - - - - - - / - - - - - - - ";
- $str = $_GET['board'];
- $map = array(
- "Bishop" => 0,
- "Horseman" => 0,
- "King" => 0,
- "Pawn" => 0,
- "Queen" => 0,
- "Rook" => 0
- );
- $items = explode('/', $str);
- if(count($items) != 8) {
- echo "<h1>Invalid chess board</h1>";
- die;
- }
- $result = '<table>';
- for($i = 0; $i < count($items) ;$i++) {
- $result .= '<tr>';
- $currLine = explode('-', $items[$i]);
- if(count($currLine) != 8) {
- echo "<h1>Invalid chess board</h1>";
- die;
- }
- for($j = 0; $j < count($currLine) ;$j++) {
- if($currLine[$j] == ' ') {
- $result .= '<td> </td>';
- }
- else {
- $figure = $currLine[$j];
- $result .= '<td>' . $figure . '</td>';
- if($figure == 'B') {
- $map['Bishop']++;
- }
- else if($figure == 'H') {
- $map['Horseman']++;
- }
- else if($figure == 'K') {
- $map['King']++;
- }
- else if($figure == 'P') {
- $map['Pawn']++;
- }
- else if($figure == 'Q') {
- $map['Queen']++;
- }
- else if($figure == 'R') {
- $map['Rook']++;
- }
- else {
- echo "<h1>Invalid chess board</h1>";
- die;
- }
- }
- }
- $result .= '</tr>';
- }
- $result .= '</table>';
- echo $result;
- $map = array_filter($map, function ($var) {
- if($var == 0) {
- return false;
- }
- return true;
- });
- if(count($map) > 0) {
- echo json_encode($map);
- }
- ?>
Add Comment
Please, Sign In to add comment