Schaakbord tekenen
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"]
]
Gewijzigd op 01/03/2019 10:49:29 door Hichem touati
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.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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 />';
}
?>
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 />';
}
?>
Gewijzigd op 01/03/2019 14:11:34 door Adoptive Solution
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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>';
?>
$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>';
?>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!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>
<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>
Een schaakbord is 8 op 8 en kan best als een dubbele array weergegeven worden.
Mijn bord is bijvoorbeeld:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
$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(" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ")
);
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
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!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>
<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>
Gewijzigd op 01/03/2019 18:48:13 door Frank Nietbelangrijk
dank allen