所以我正在寻找如何制作一个棋盘但是用螺旋线代替这样的默认棋盘:
$checkerboard=array();
for($row=0;$row<10;$row++){
if($row%2==0){
for($col=0;$col<10;$col++){
if($col%2==0){
$checkerboard[$row][$col]="white";
}else{
$checkerboard[$row][$col]="black";
}
}
}else{
for($col=0;$col<10;$col++){
if($col%2==0){
$checkerboard[$row][$col]="black";
}else{
$checkerboard[$row][$col]="white";
}
}
}
}
我也试过这样的2对角线:
$diagonal=array();
for($row=0;$row<10;$row++){
for($col=0;$col<10;$col++){
if($row==$col){
$diagonal[$row][$col]='black';
}else{
$diagonal[$row][$col]='white';
}
if($row+$col==9){
$diagonal[$row][$col]='black';
}
}
}
然后echo就像这样:
echo "<table>";
for($row=0;$row<count($checkerboard);$row++){
echo "<tr>";
for($col=0;$col<count($checkerboard);$col++){
echo "<td width='50px' height='50px' bgcolor='".$checkerboard[$row][$col]."'></td>";
}
echo "</tr>";
}
我想保持代码简单,因为我已经很长时间没有编写php了,它必须使用数组。
我在这里试过了:
$spiral=array();
for($row=0;$row<10;$row++){
for($col=0;$col<10;$col++){
$spiral[$row][$col]='white';
if($row==0 or $row==9 or $col==0 or $col==9){
$spiral[$row][$col]='black';
}if($row==1 and $col==0){
$spiral[$row][$col]='white';
}if($row==2 and $col<8){
$spiral[$row][$col]='black';
}if($row>1 and $row<8 and $col==7){
$spiral[$row][$col]='black';
}if($row==7 and $col>1 and $col<8){
$spiral[$row][$col]='black';
}if($row>3 and $row<7 and $col==2){
$spiral[$row][$col]='black';
}if($row==4 and $col>2 and $col<6){
$spiral[$row][$col]='black';
}if($row==5 and $col==5){
$spiral[$row][$col]='black';
}
}
}
但如果棋盘变得更大,则很难改变。有没有办法让它变得更容易?
答案 0 :(得分:1)
试试这个:
我创建一个空板然后开始绘制水平&amp;垂直线,每次从边缘开始。代码可能需要一些调整,但这是一个好的开始
$checkerboard=array();
$size = 12;
for ($row=0; $row<$size; $row++) {
for ($col=0; $col<$size; $col++) {
$checkerboard[$row][$col]="red";
}
}
//horizontal
$pair = 0 ;
while ($pair < (int) $size / 2) {
//drawing top half rows
$row = 2 * $pair;
$end = min($row, $size - $row);
$start = $end - 2;
for ($col = $start; ($col < $size - $end) && ($row < $size / 2); $col++){
$checkerboard[$row][$col]="black";
}
//drawing bottom half rows
$far_row = $size - 1 - 2 * $pair;
$end = min($far_row, $size - $far_row) + 1 - 2;
$start = $end ;
for ($col = $start; ($col < $size - $end) && ($far_row > $size / 2 ); $col++){
$checkerboard[$far_row][$col]="black";
}
$pair++;
}
$pair = 0;
//vertical
while ($pair < (int) $size / 2) {
//drawing left half columns
$col = 2 * $pair;
$end = min($col, $size - $col);
$start = $end +2 ;
for ($row = $start; ($row < $size - $end) && ($col < $size / 2); $row++){
$checkerboard[$row][$col]="black";
}
//drawing right half columns
$far_columns = $size - 1 - 2 * $pair;
$end = min($far_columns, $size - $far_columns) - 1;
$start = $end ;
for ($row = $start; ($row < $size - $end) && ($far_columns >= ($size / 2 ) ); $row++){
$checkerboard[$row][$far_columns]="black";
}
$pair++;
}
echo "<table>";
for($row=0;$row< $size;$row++){
echo "<tr>";
for($col=0; $col< $size; $col++){
echo "<td width='50px' height='50px' bgcolor='".$checkerboard[$row][$col]."'></td>";
}
echo "</tr>";
}
您只需更改不同维度的$size
变量
答案 1 :(得分:1)
以下是我对此问题的尝试:
function buildSpiral($gridSize)
{
/**
* Origin is at the top left handcorner
*/
$x = 0;
$y = 0;
$xMin = 0;
$xMax = $gridSize-1;
$yMin = 2;
$yMax = $gridSize-1;
$pattern = [];
$size = $gridSize;
$collision = function($p, $limit) {
return (bool) ($p == $limit);
};
// increment x
$shadeRight = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x++, $xMax));
if ($x >= $xMax) {
$x=$xMax;
}
$xMax-=2;
};
// increment y
$shadeDown = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision) {
while ($y < $yMax && $yMin > $yMax) {
$pattern[++$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($y++, $yMax));
if ($y >= $yMax) {
$y = $yMax;
}
$yMax-=2;
};
// decrement x
$shadeLeft = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision, $gridSize) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x--, $xMin));
if ($x < $xMin) {
$x=$xMin;
}
$xMin+=2;
};
// decrement y
$shadeUp = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision, $gridSize) {
while ($y > $yMin && $yMin > $yMax) {
$pattern[--$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while(!$collision(--$y, $yMin));
if ($y < $yMin) {
$y = $yMin;
}
$yMin+=2;
};
while ($size > 0) {
$shadeRight($x, $y);
$shadeDown($x, $y);
$shadeLeft($x, $y);
$shadeUp($x, $y);
$size-=2;
}
return $pattern;
}
for ($i = 1; $i <= 25; $i++) {
$checkboard = buildSpiral($i);
echo "<h1>$i</h1>";
echo "<table style='margin-bottom: 2em;'>";
for($row=0;$row<count($checkboard);$row++){
echo "<tr>";
for($col=0;$col<count($checkboard);$col++){
if (!isset($checkboard[$row][$col])) {
echo "<td width='50px' height='50px' bgcolor=\"red\"></td>";
} else {
echo "<td width='50px' height='50px' bgcolor=\"black\"></td>";
}
}
echo "</tr>";
}
echo "</table>";
}
当达到限制时,我会停止每个着色方向。
更新让我们说我希望螺旋从右上角开始,然后我们只需要设置新原点并按照我们希望螺旋的方式调用着色器:
function buildSpiral($gridSize)
{
/**
* Origin is at the top left handcorner
*/
$x = $gridSize-1;
$y = 0;
$xMin = 0;
$xMax = $gridSize-1;
$yMin = 2;
$yMax = $gridSize-1;
$pattern = [];
$size = $gridSize;
$collision = function($p, $limit) {
return (bool) ($p == $limit);
};
// increment x
$shadeRight = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x++, $xMax));
if ($x >= $xMax) {
$x=$xMax;
}
$xMax-=2;
};
// increment y
$shadeDown = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision) {
while ($y < $yMax && $yMin > $yMax) {
$pattern[++$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($y++, $yMax));
if ($y >= $yMax) {
$y = $yMax;
}
$yMax-=2;
};
// decrement x
$shadeLeft = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision, $gridSize) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x--, $xMin));
if ($x < $xMin) {
$x=$xMin;
}
$xMin+=2;
};
// decrement y
$shadeUp = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision, $gridSize) {
while ($y > $yMin && $yMin > $yMax) {
$pattern[--$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while(!$collision(--$y, $yMin));
if ($y < $yMin) {
$y = $yMin;
}
$yMin+=2;
};
while ($size > 0) {
$shadeLeft($x, $y);
$shadeDown($x, $y);
$shadeRight($x, $y);
$shadeUp($x, $y);
$size-=2;
}
return $pattern;
}
for ($i = 1; $i <= 25; $i++) {
$checkboard = buildSpiral($i);
echo "<h1>$i</h1>";
echo "<table style='margin-bottom: 2em;'>";
for($row=0;$row<count($checkboard);$row++){
echo "<tr>";
for($col=0;$col<count($checkboard);$col++){
if (!isset($checkboard[$row][$col])) {
echo "<td width='50px' height='50px' bgcolor=\"red\"></td>";
} else {
echo "<td width='50px' height='50px' bgcolor=\"black\"></td>";
}
}
echo "</tr>";
}
echo "</table>";
}