Hi. Willkommen im Forum!
Wie du das genau umsetzt, hängt von deiner Datenstruktur ab. Eine Möglichkeit wäre es, ein zweidimensionales Feld zu nutzen:
PHP:
<?php
function buildField($sizeX, $sizeY, $numberOfMines)
{
if ($numberOfMines > $sizeX * $sizeY) {
throw new Exception('Not all mines will fit on the board.');
}
// Create a field of size: $sizeX * $sizeY
$field = array_fill(
0,
$sizeX,
array_fill(
0,
$sizeY,
false
)
);
// Randomly place mines
$i = 0;
while ($i < $numberOfMines) {
$testX = array_rand($field);
$testY = array_rand($field[$testX]);
if (false === $field[$testX][$testY]) {
$field[$testX][$testY] = true;
$i++;
}
}
return $field;
}
$field = buildField(40, 40, 500);
header('Content-Type: text/plain');
foreach ($field as $row) {
foreach ($row as $cell) {
if (true === $cell) {
echo 'x ';
} else {
echo '. ';
}
}
echo "\n";
}
Interessanter ist übrigens die Frage, wie du die Daten eines Spiels über das Neuladen der Seite hinweg speichern möchtest. (Edit: Vergesst die Frage. :) -> Session.)
wie kann ich das jetzt in meinen array einbringen?
hier mein code:
<?php
$buttons1 = array(
0 => '.',
1 => '.',
2 => '.',
3 => '.',
4 => '.',
5 => '.',
6 => '.',
7 => '.',
8 => '.',
9 => '.',
10 => '.',
);
$buttons2 = array(
0 => '.',
1 => '.',
2 => '.',
3 => '.',
4 => '.',
5 => '.',
6 => '.',
7 => '.',
8 => '.',
9 => '.',
10 => '.',
);
$buttons3 = array(
0 => '.',
1 => '.',
2 => '.',
3 => '.',
4 => '.',
5 => '.',
6 => '.',
7 => '.',
8 => '.',
9 => '.',
10 => '.',
);
$buttons4 = array(
0 => '.',
1 => '.',
2 => '.',
3 => '.',
4 => '.',
5 => '.',
6 => '.',
7 => '.',
8 => '.',
9 => '.',
10 => '.',
);
$buttons5 = array(
0 => '.',
1 => '.',
2 => '.',
3 => '.',
4 => '.',
5 => '.',
6 => '.',
7 => '.',
8 => '.',
9 => '.',
10 => '.',
);
$buttons6 = array(
0 => '.',
1 => '.',
2 => '.',
3 => '.',
4 => '.',
5 => '.',
6 => '.',
7 => '.',
8 => '.',
9 => '.',
10 => '.',
);
$buttons7 = array(
0 => '.',
1 => '.',
2 => '.',
3 => '.',
4 => '.',
5 => '.',
6 => '.',
7 => '.',
8 => '.',
9 => '.',
10 => '.',
);
$buttons8 = array(
0 => '.',
1 => '.',
2 => '.',
3 => '.',
4 => '.',
5 => '.',
6 => '.',
7 => '.',
8 => '.',
9 => '.',
10 => '.',
);
$buttons9 = array(
0 => '.',
1 => '.',
2 => '.',
3 => '.',
4 => '.',
5 => '.',
6 => '.',
7 => '.',
8 => '.',
9 => '.',
10 => '.',
);
$buttons10 = array(
0 => '.',
1 => '.',
2 => '.',
3 => '.',
4 => '.',
5 => '.',
6 => '.',
7 => '.',
8 => '.',
9 => '.',
10 => '.',
);
?>
<form id="option" action="Minesweeper.php" name="option" method="post">
<?php
foreach ($buttons1 as $buttonValue)
{
echo '<input type="submit" value="'.$buttonValue.'" name="value">';
}
?>
<br>
<?php
foreach ($buttons2 as $buttonValue)
{
echo '<input type="submit" value="'.$buttonValue.'" name="value">';
}
?>
<br>
<?php
foreach ($buttons3 as $buttonValue)
{
echo '<input type="submit" value="'.$buttonValue.'" name="value">';
}
?>
<br>
<?php
foreach ($buttons4 as $buttonValue)
{
echo '<input type="submit" value="'.$buttonValue.'" name="value">';
}
?>
<br>
<?php
foreach ($buttons5 as $buttonValue)
{
echo '<input type="submit" value="'.$buttonValue.'" name="value">';
}
?>
<br>
<?php
foreach ($buttons6 as $buttonValue)
{
echo '<input type="submit" value="'.$buttonValue.'" name="value">';
}
?>
<br>
<?php
foreach ($buttons7 as $buttonValue)
{
echo '<input type="submit" value="'.$buttonValue.'" name="value">';
}
?>
<br>
<?php
foreach ($buttons8 as $buttonValue)
{
echo '<input type="submit" value="'.$buttonValue.'" name="value">';
}
?>
<br>
<?php
foreach ($buttons9 as $buttonValue)
{
echo '<input type="submit" value="'.$buttonValue.'" name="value">';
}
?>
<br>
<?php
foreach ($buttons10 as $buttonValue)
{
echo '<input type="submit" value="'.$buttonValue.'" name="value">';
}
?>
</form>