Ik wil graag een schaakbord tekenen met twee integer values: $rows en $columns. Bestaat er een PHP functie die die meerdere arrays genereert na de comma?

Dus chessBoard(6,4) function returns deze array:

[
["O","X","O","X"],
["X","O","X","O"],
["O","X","O","X"],
["X","O","X","O"],
["O","X","O","X"],
["X","O","X","O"]
]

En chessBoard(3,7) returns dit:

[
["O","X","O","X","O","X","O"],
["X","O","X","O","X","O","X"],
["O","X","O","X","O","X","O"]
]
Huh?
Hichem touati op 01/03/2019 10:37:24

... die meerdere arrays genereert na de comma?

D'r is wel zo iets als een for-loop. Dat 2x (hoogte/breedte) en je hebt je schaakbord.

<?php
function chessBoard( $rows, $columns )
{
	$ex = 'X';
	$oh = 'O';
	$wissel = TRUE;
	$chessboard = array();

	for ( $row = 0; $row < $rows; $row++ )
	{
		$wissel = !$wissel;
		for ( $column = 0; $column < $columns; $column++ )
		{
			$chessboard[$row][$column] = ( $wissel ? $ex : $oh );
			$wissel = !$wissel;
		}
	}
	return $chessboard;
}

// in URL = chessboard.php?r=3&c=5

$regel = isset( $_GET['r'] ) ? (int)$_GET['r'] : 4;
$kolom = isset( $_GET['c'] ) ? (int)$_GET['c'] : 6;

$chessboard = chessBoard( $regel, $kolom );

echo '<pre>' . print_r( $chessboard, TRUE ) . '</pre>';

foreach( $chessboard as $chess )
{
	echo '|';
	foreach ( $chess as $ches )
	{
		echo $ches . '|';
	}
	echo '<br />';
}
?>
<?php
$boolean = true;
echo '<div style="border:2px solid blue;width:160px;height:160px;">';
for($y=0;$y<8;$y++)
{ for($x=1;$x<9;$x++)
{ $kleur = $boolean ? 'black' : 'white';
$boolean = !$boolean;
echo '<div style="background-color:'.$kleur.';width:20px;height:20px;float:left;"></div>';
}
$boolean = !$boolean;
echo '<div style="clear:both;"></div>';
}
echo '</div>';
?>
Wat voor schoolopdracht is dit?


<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Schaakbord</title>
        <style>
            .chessboard {
                border:2px solid blue;
                width:160px;
                height:160px;
            }
            .chessboard-field {
                width:20px; 
                height:20px;
                float:left;
                background-color: white;
            }
            .black {
                background-color: black;
            }
        </style>
    </head>
    <body>
        <div class="chessboard">
        <?php for($y = 0 ; $y < 8 ; $y++) : ?>
            <?php for($x = 0;$x < 8 ; $x++) : ?>
                <div class="chessboard-field<?php if($x % 2 - $y % 2) echo ' black'; ?>"></div>
            <?php endfor; ?>
            <div style="clear:both;"></div>
        <?php endfor; ?>
        </div>
    </body>
</html>
Klopt van geen kanten.

Een schaakbord is 8 op 8 en kan best als een dubbele array weergegeven worden.
Mijn bord is bijvoorbeeld:

$bord = array(  array("1", "2", "2", "2", "2", "2", "2", "2", "2", "3", " "),
    array("4", "R", "N", "B", "Q", "K", "B", "N", "R", "5", " "),
    array("4", "P", "P", "P", "P", "P", "P", "P", "P", "5", " "),
    array("4", " ", " ", " ", " ", " ", " ", " ", " ", "5", " "),
    array("4", " ", " ", " ", " ", " ", " ", " ", " ", "5", " "),
    array("4", " ", " ", " ", " ", " ", " ", " ", " ", "5", " "),
    array("4", " ", " ", " ", " ", " ", " ", " ", " ", "5", " "),
    array("4", "p", "p", "p", "p", "p", "p", "p", "p", "5", " "),
    array("4", "r", "n", "b", "q", "k", "b", "n", "r", "5", " "),
    array("6", "7", "7", "7", "7", "7", "7", "7", "7", "8", " "),
    array(" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ")
);

Waarbij 0 en 9-10 borders zijn.
Mijn kleur, zwart links onder, wordt berekend met een xor
Hoewel ook werkende van SanThe: $boolean = !$boolean; is de variable overbodig met een xor

Jan
Of met javascript:


<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Schaakbord</title>
        <style>
            #chessboard {
                border:2px solid blue;
                width:160px;
                height:160px;
            }
            .chessboard-field {
                width:20px; 
                height:20px;
                float:left;
                background-color: white;
            }
            .black {
                background-color: black;
            }
        </style>
    </head>
    <body>
        <div id="chessboard"></div>
        <script>
            var html = '';
            for(var y = 0 ; y < 8 ; y++) {
                for(var x = 0 ; x < 8 ; x++) {
                    html += '<div class="chessboard-field';
                    if(x%2-y%2) { html += ' black'; }
                    html += '"></div>';
                }
                html += '<div style="clear:both;"></div>';
            }
            document.getElementById("chessboard").innerHTML = html;
        </script>
    </body>
</html>

Reageren