hi leuteich hatte schon einen thread für minesweeper...dabei habe ich nen echt schönen lösungsansatz von merhaus bekommen...danke nochmal^^
ich wollte jetzt das spiel komplett machen und habe dabei fast alles probiert aber ich komm nicht weiter...dabei fehlen mir 2 sachen...
zu der ersten...die zahlen einsetzen...
mein ansatz:
auf minen prüfen:
x-1;y-1
x-1;y
x;y+1
x;y-1
x;y
x;y+1
x+1;y-1
x+1;y
x+1;y+1
soweit...is kein befehl dabei...da ich mit diesem isMine-> nicht so richtig vertraut bin^^
das ich kein geschriebenes array habe macht die sache dabei auch noch ne ecke schwerer^^
denn so wie oben nur mit ismine funktioniert is nicht...
ach ja der quelltext^^
<?php
class Minesweeper_Cell
{
public $isMine = false;
public $isRevealed = false;
public $posX = -1;
public $posY = -1;
public $adjacentMines = 0;
}
function buildField($sizeX, $sizeY, $numberOfMines)
{
if ($numberOfMines > $sizeX * $sizeY) {
throw new Exception('Minen>Feld');
}
//Feld
$field = array_fill(0, $sizeX, array());
$x = 0;
foreach ($field as &$row) {
for ($y = 0; $y < $sizeY; $y++) {
$cell = new Minesweeper_Cell();
$cell->posX = $x;
$cell->posY = $y;
$row[] = $cell;
}
$x++;
}
unset($row);
//Minen
$i = 0;
while ($i < $numberOfMines) {
$testX = array_rand($field);
$testY = array_rand($field[$testX]);
$cell = $field[$testX][$testY];
if (!$cell->isMine) {
$cell->isMine = true;
$i++;
}
}
return $field;
}
error_reporting(-1);
session_start();
header('content-type: text/html; charset=utf-8');
//Voreinstellungen
if (!isset($_SESSION['field'])) {
/* Start new game */
$_SESSION['field'] = buildField(8,8,10);
}
//Aktion
if (isset($_POST['form_id'])) {
if ($_POST['form_id'] === 'cell_clicked')
$tmp = array_keys($_POST['cell']);
$x = array_pop($tmp);
$tmp = array_keys($_POST['cell'][$x]);
$y = array_pop($tmp);
$cell = $_SESSION['field'][$x][$y];
if ($cell->isMine) {
$cell->isRevealed = true;
} else {
$cell->isRevealed = true;
}
} else if ($_POST['form_id'] === 'new_game') {
$_SESSION = array();
/* Start new game */
$_SESSION['field'] = buildField(8,8,10);
}
}
$field = $_SESSION['field'];
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Minesweeper</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
padding: 60px;
}
.minesweeper {
margin-bottom: 30px;
}
.minesweeper p.row {
clear: both;
}
.minesweeper input.cell {
width: 20px;
height: 20px;
float: left;
border: 1px outset #ccc;
}
.minesweeper input.cell.revealed {
border: 1px inset #ccc;
}
</style>
</head>
<body>
<div class="minesweeper">
<form method="post" action="">
<?php foreach ($field as $row): ?>
<p class="row">
<?php foreach ($row as $cell): ?>
<input class="cell<?php echo ($cell->isRevealed) ? ' revealed' : ''; ?>"
type="submit"
name="cell[<?php echo $cell->posX; ?>][<?php echo $cell->posY; ?>]"
value="<?php
if ($cell->isRevealed) {
if ($cell->isMine) {
echo 'X';
?>
<textarea name="textfeld"> <\"Verloren </textarea>
<?php
}
}
?>" />
<?php endforeach; ?>
</p>
<?php endforeach; ?>
<input type="hidden" name="form_id" value="cell_clicked" />
</form>
</div>
<form method="post" action="">
<p>
<input type="hidden" name="form_id" value="new_game" />
<input type="submit" value="Neues Spiel" />
</p>
</form>
</body>
</html>
ich wollte jetzt das spiel komplett machen und habe dabei fast alles probiert aber ich komm nicht weiter...dabei fehlen mir 2 sachen...
zu der ersten...die zahlen einsetzen...
mein ansatz:
auf minen prüfen:
x-1;y-1
x-1;y
x;y+1
x;y-1
x;y
x;y+1
x+1;y-1
x+1;y
x+1;y+1
soweit...is kein befehl dabei...da ich mit diesem isMine-> nicht so richtig vertraut bin^^
das ich kein geschriebenes array habe macht die sache dabei auch noch ne ecke schwerer^^
denn so wie oben nur mit ismine funktioniert is nicht...
ach ja der quelltext^^
<?php
class Minesweeper_Cell
{
public $isMine = false;
public $isRevealed = false;
public $posX = -1;
public $posY = -1;
public $adjacentMines = 0;
}
function buildField($sizeX, $sizeY, $numberOfMines)
{
if ($numberOfMines > $sizeX * $sizeY) {
throw new Exception('Minen>Feld');
}
//Feld
$field = array_fill(0, $sizeX, array());
$x = 0;
foreach ($field as &$row) {
for ($y = 0; $y < $sizeY; $y++) {
$cell = new Minesweeper_Cell();
$cell->posX = $x;
$cell->posY = $y;
$row[] = $cell;
}
$x++;
}
unset($row);
//Minen
$i = 0;
while ($i < $numberOfMines) {
$testX = array_rand($field);
$testY = array_rand($field[$testX]);
$cell = $field[$testX][$testY];
if (!$cell->isMine) {
$cell->isMine = true;
$i++;
}
}
return $field;
}
error_reporting(-1);
session_start();
header('content-type: text/html; charset=utf-8');
//Voreinstellungen
if (!isset($_SESSION['field'])) {
/* Start new game */
$_SESSION['field'] = buildField(8,8,10);
}
//Aktion
if (isset($_POST['form_id'])) {
if ($_POST['form_id'] === 'cell_clicked')
$tmp = array_keys($_POST['cell']);
$x = array_pop($tmp);
$tmp = array_keys($_POST['cell'][$x]);
$y = array_pop($tmp);
$cell = $_SESSION['field'][$x][$y];
if ($cell->isMine) {
$cell->isRevealed = true;
} else {
$cell->isRevealed = true;
}
} else if ($_POST['form_id'] === 'new_game') {
$_SESSION = array();
/* Start new game */
$_SESSION['field'] = buildField(8,8,10);
}
}
$field = $_SESSION['field'];
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Minesweeper</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
padding: 60px;
}
.minesweeper {
margin-bottom: 30px;
}
.minesweeper p.row {
clear: both;
}
.minesweeper input.cell {
width: 20px;
height: 20px;
float: left;
border: 1px outset #ccc;
}
.minesweeper input.cell.revealed {
border: 1px inset #ccc;
}
</style>
</head>
<body>
<div class="minesweeper">
<form method="post" action="">
<?php foreach ($field as $row): ?>
<p class="row">
<?php foreach ($row as $cell): ?>
<input class="cell<?php echo ($cell->isRevealed) ? ' revealed' : ''; ?>"
type="submit"
name="cell[<?php echo $cell->posX; ?>][<?php echo $cell->posY; ?>]"
value="<?php
if ($cell->isRevealed) {
if ($cell->isMine) {
echo 'X';
?>
<textarea name="textfeld"> <\"Verloren </textarea>
<?php
}
}
?>" />
<?php endforeach; ?>
</p>
<?php endforeach; ?>
<input type="hidden" name="form_id" value="cell_clicked" />
</form>
</div>
<form method="post" action="">
<p>
<input type="hidden" name="form_id" value="new_game" />
<input type="submit" value="Neues Spiel" />
</p>
</form>
</body>
</html>
Zuletzt bearbeitet: