Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $isValidChessBoard = true;
- if (!isset($_GET['board'])) {
- $isValidChessBoard = false;
- }
- $boardRows = [];
- if ($isValidChessBoard) {
- $boardRows = explode('/', $_GET['board']);
- if (count($boardRows) != 8) {
- $isValidChessBoard = false;
- }
- }
- $pieces = array('Bishop' => 0, 'Horseman' => 0, 'King' => 0, 'Pawn' => 0, 'Queen' => 0, 'Rook' => 0);
- $board = [];
- if ($isValidChessBoard) {
- foreach ($boardRows as $row) {
- $cells = explode('-', $row);
- if (count($cells) != 8) {
- $isValidChessBoard = false;
- break;
- }
- $board[] = $cells;
- foreach ($cells as $cell) {
- switch ($cell) {
- case 'B':
- $pieces['Bishop'] += 1;
- break;
- case 'H':
- $pieces['Horseman'] += 1;
- break;
- case 'K':
- $pieces['King'] += 1;
- break;
- case 'P':
- $pieces['Pawn'] += 1;
- break;
- case 'Q':
- $pieces['Queen'] += 1;
- break;
- case 'R':
- $pieces['Rook'] += 1;
- break;
- case ' ':
- break;
- default:
- $isValidChessBoard = false;
- break;
- }
- }
- }
- }
- if (!$isValidChessBoard) {
- die('<h1>Invalid chess board</h1>');
- }
- echo '<table>';
- foreach ($board as $tableRow) {
- $textRow = '<tr>';
- foreach ($tableRow as $tableCol) {
- $textRow .= "<td>{$tableCol}</td>";
- }
- echo $textRow . '</tr>';
- }
- echo '</table>';
- foreach ($pieces as $key => &$value) {
- if ($value === 0) {
- unset($pieces[$key]);
- }
- }
- echo json_encode($pieces);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement