<?php

$input = $_GET['board'];
$lines = preg_split('/\\//', $input, -1, PREG_SPLIT_NO_EMPTY);
// Validate the chessboard
isValidChessBoard($lines);
$board = [];
for ($row = 0, $startIndex = 0; $startIndex < strlen($input); $row++, $startIndex += 17) {
    $board[] = preg_split("/-/", $lines[$row], -1, PREG_SPLIT_NO_EMPTY);
}
$pieces = array();
echo "<table>";
for ($row = 0; $row < count($board); $row++) {
    echo "<tr>";
    for ($col = 0; $col < count($board[$row]); $col++) {
        $piece = getPiece($row, $col, $board);
        if ($piece != 'empty') {
            if (!isset($pieces[$piece])) {
                $pieces[$piece] = 1;
            } else {
                $pieces[$piece]++;
            }
        }
        echo "<td>" . $board[$row][$col] . "</td>";
    }
    echo "</tr>";
}
echo "</table>";
ksort($pieces);
echo json_encode($pieces);
function isValidChessBoard($boardLines)
예제 #2
0
// Validate the chessboard
$isValid = isValidChessboard($lines);
if (!$isValid) {
    echo "<h1>Invalid chess board</h1>";
    return;
}
$board = array();
for ($i = 0, $startIndex = 0; $startIndex < strlen($input); $i++, $startIndex += 17) {
    $board[] = preg_split("/-/", $lines[$i], -1, PREG_SPLIT_NO_EMPTY);
}
$pieces = array();
echo "<table>";
for ($i = 0; $i < count($board); $i++) {
    echo "<tr>";
    for ($j = 0; $j < count($board[$i]); $j++) {
        $piece = getPiece($i, $j, $board);
        if ($piece != "empty") {
            if (!isset($pieces[$piece])) {
                $pieces[$piece] = 1;
            } else {
                $pieces[$piece]++;
            }
        }
        echo "<td>" . $board[$i][$j] . "</td>";
    }
    echo "</tr>";
}
echo "</table>";
ksort($pieces);
echo json_encode($pieces);
function getPiece($row, $col, $board)