Script review: mijnenveger (Minesweeper)
Waarschijnlijk zoals duizenden maar tijdens deze lockdown (corona) beetje bezig houden met wat anders.
Het is geen werkend spel maar wel de opbouw van een veld.
Voor die dat wilt kan er een leuk spel van maken.
Het is geen werkend spel maar wel de opbouw van een veld.
Voor die dat wilt kan er een leuk spel van maken.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
// Breedde van het veld
$x = 10;
// Hoogte van het veld
$y = 10;
// Aantal bommen
$b = 30;
// Bereken het veld met de aantal bommen
if ($x < 2){ $x = rand(2, 10); }
if ($y < 2){ $y = rand(2, 10); }
$f = $x * $y;
$field = array_fill(1, $f, 0);
if ($b < 1 || $b > $f){
// Keuze uit delen door of optellen
$b = rand(1, floor($f / 6));
// $b = rand(1, $x + $y);
}
print 'Breedde: '.$x.'<br>';
print 'Hoogte: '.$y.'<br>';
print 'Bommen: '.$b.'<hr>';
// Bommen plaatsen op willekeurig
$random = array_rand($field, $b);
if (!is_array($random)){ $random = array($random); }
foreach ($random as $key){
$field[$key] = 'X';
}
// Bouw het veld
$minefield = array();
for ($height = 1; $height <= $y; $height++){
for ($width = 1; $width <= $x; $width++){
$thisfield = (($height - 1) * $x) + $width;
$minefield[$height][$width] = $field[$thisfield];
}
}
// Start het tellen van de bommen/vakjes
for ($height = 1; $height <= $y; $height++){
for ($width = 1; $width <= $x; $width++){
$up = $height - 1; // Lijn hoger
$down = $height + 1; // Lijn lager
$left = $width - 1; // Kolom links
$right = $width + 1; // Kolom rechts
// Start de telling
if ($minefield[$height][$width] === 0){
$count = 0;
if ($up >= 1){
if ($left >= 1){
if ($minefield[$up][$left] === 'X'){ $count++; }
}
if ($minefield[$up][$width] === 'X'){ $count++; }
if ($right <= $x){
if ($minefield[$up][$right] === 'X'){ $count++; }
}
}
if ($left >= 1){
if ($minefield[$height][$left] === 'X'){ $count++; }
}
if ($right <= $x){
if ($minefield[$height][$right] === 'X'){ $count++; }
}
if ($down <= $y){
if ($left >= 1){
if ($minefield[$down][$left] === 'X'){ $count++; }
}
if ($minefield[$down][$width] === 'X'){ $count++; }
if ($right <= $x){
if ($minefield[$down][$right] === 'X'){ $count++; }
}
}
$minefield[$height][$width] = $count;
}
}
}
// Toon het veld in een tabel
print '<table border="1">';
foreach ($minefield as $keyRow => $row){
print '<tr>';
foreach($row as $keyColumn => $column){
if ($minefield[$keyRow][$keyColumn] === 'X'){ $set = '💣'; } else { $set = $minefield[$keyRow][$keyColumn]; }
print '<td width="50px" height="50px" valign="middle" align="center">'.$set.'</td>';
}
print '</tr>';
}
print '</table>';
?>
// Breedde van het veld
$x = 10;
// Hoogte van het veld
$y = 10;
// Aantal bommen
$b = 30;
// Bereken het veld met de aantal bommen
if ($x < 2){ $x = rand(2, 10); }
if ($y < 2){ $y = rand(2, 10); }
$f = $x * $y;
$field = array_fill(1, $f, 0);
if ($b < 1 || $b > $f){
// Keuze uit delen door of optellen
$b = rand(1, floor($f / 6));
// $b = rand(1, $x + $y);
}
print 'Breedde: '.$x.'<br>';
print 'Hoogte: '.$y.'<br>';
print 'Bommen: '.$b.'<hr>';
// Bommen plaatsen op willekeurig
$random = array_rand($field, $b);
if (!is_array($random)){ $random = array($random); }
foreach ($random as $key){
$field[$key] = 'X';
}
// Bouw het veld
$minefield = array();
for ($height = 1; $height <= $y; $height++){
for ($width = 1; $width <= $x; $width++){
$thisfield = (($height - 1) * $x) + $width;
$minefield[$height][$width] = $field[$thisfield];
}
}
// Start het tellen van de bommen/vakjes
for ($height = 1; $height <= $y; $height++){
for ($width = 1; $width <= $x; $width++){
$up = $height - 1; // Lijn hoger
$down = $height + 1; // Lijn lager
$left = $width - 1; // Kolom links
$right = $width + 1; // Kolom rechts
// Start de telling
if ($minefield[$height][$width] === 0){
$count = 0;
if ($up >= 1){
if ($left >= 1){
if ($minefield[$up][$left] === 'X'){ $count++; }
}
if ($minefield[$up][$width] === 'X'){ $count++; }
if ($right <= $x){
if ($minefield[$up][$right] === 'X'){ $count++; }
}
}
if ($left >= 1){
if ($minefield[$height][$left] === 'X'){ $count++; }
}
if ($right <= $x){
if ($minefield[$height][$right] === 'X'){ $count++; }
}
if ($down <= $y){
if ($left >= 1){
if ($minefield[$down][$left] === 'X'){ $count++; }
}
if ($minefield[$down][$width] === 'X'){ $count++; }
if ($right <= $x){
if ($minefield[$down][$right] === 'X'){ $count++; }
}
}
$minefield[$height][$width] = $count;
}
}
}
// Toon het veld in een tabel
print '<table border="1">';
foreach ($minefield as $keyRow => $row){
print '<tr>';
foreach($row as $keyColumn => $column){
if ($minefield[$keyRow][$keyColumn] === 'X'){ $set = '💣'; } else { $set = $minefield[$keyRow][$keyColumn]; }
print '<td width="50px" height="50px" valign="middle" align="center">'.$set.'</td>';
}
print '</tr>';
}
print '</table>';
?>
Gewijzigd op 18/04/2020 01:38:55 door G P
Inderdaad, toch niks te doen. Dus hiero ook maar wat commentaar:
Persoonlijk begin ik altijd bij 0 te tellen. Niet altijd handig, maar het heeft zo zijn voordelen. Het genereren van het veld wordt dan eenvoudig:
Het tellen van de bommen heb je vrij plat uitgewerkt. $up/$down/enz zou nog na de if(...) daaronder kunnen, scheelt weer een paar pico seconde. Ook kan het wat korter door de bocht (niet perse efficiënter):
Persoonlijk begin ik altijd bij 0 te tellen. Niet altijd handig, maar het heeft zo zijn voordelen. Het genereren van het veld wordt dan eenvoudig:
Code (php)
1
2
3
4
2
3
4
// Bommen plaatsen op willekeurig
$random = array_fill_keys((array)array_rand($field, $b), 'X');
// Bouw het veld
$minefield = array_chunk(array_replace($field,$random),$x);
$random = array_fill_keys((array)array_rand($field, $b), 'X');
// Bouw het veld
$minefield = array_chunk(array_replace($field,$random),$x);
Het tellen van de bommen heb je vrij plat uitgewerkt. $up/$down/enz zou nog na de if(...) daaronder kunnen, scheelt weer een paar pico seconde. Ook kan het wat korter door de bocht (niet perse efficiënter):
Spamkickbericht verwijderd!



