无法存储小游戏的数据

时间:2014-03-20 02:58:52

标签: php

好的,我正在努力解决一个项目问题。它在php中扫雷,但在游戏的基础上,我无法获得不同的单元块来存储他们的信息。香港专业教育学院尝试使用会话和获取,但我似乎无法弄清楚如何存储信息或在何处放置代码。以下是基本游戏代码:任何想法?

更新:我非常感谢我一直试图解决这个问题的帮助。我不确定在哪里放置一些会话值。这对我来说不合适:

<html>
<body>


<?php
// ~~~~~~~~~  VARIABLES  ~~~~~~~~~~
session_start();
$_SESSION[$piece] = $piece;

$default_board = "default_board";
$filename = "currentproject.php";


$width = "20px";
$height = "20px";
$y_coord = 50;
$how_many_spots = 25;

echo "<table border=1>";
$_SESSION['clicks'] = array();
for ($y=24;$y!=0;--$y) {
    $y_coord += $height;
    $x_coord = 50;

    for ($x=24;$x!=0;--$x){

        $x_coord += $width; $cell_sum = $x + $y; $remainder = $cell_sum%2; // some math
        if (isset($piece[$x][$y])) {
            $background_color = "#991122"; //shows a mine
        } else if ($remainder == 0) {
            $background_color = "#CCCCCC";
        } else $background_color = "#AAAAAA";

        echo "<div onclick=\"javascript:document.location.href='$filename?xcord=$x&ycord=$y';\" style=\"border: 1px solid black; background-color: $background_color; position: absolute;left: $x_coord; top: $y_coord; width: $width; height: $height;\">";
        echo "</div>";
        $_SESSION['clicks'][$xcord][$ycord] = False;
        $_SESSION['clicks'][$_GET['xcord']][$_GET['ycord']] = True;
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MINES GO HERE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        while ($how_many_spots!=0){
            $i = rand(1,24);
            $e = rand(1,24);
            if (!isset($piece[$i][$e])){
                $piece[$i][$e] = "mine";
                --$how_many_spots;

            }
        }
    }
    print ("<br>");
}
$_GET['xcord'] = $xcord;
$_GET['ycord'] = $ycord;

?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

可以使用$_SESSION$_GET变量来完成。以下是一个工作示例。锻炼很有趣,我希望它有所帮助。请注意,交替灰色框的代码不是很正确,我没有修复它。我还添加了网格大小和地雷数量的选项,因为使用25 x 25网格玩625个盒子和25个地雷 - 这是一个相当困难的生存机会。我将网格默认为4x4(我喜欢获胜),你当然可以改变它。

<html>
<head>
<script type="text/javascript">
    function gameover($won){
        if ($won){
            alert("Game Over - You won");
        }
        else {
            alert("Game Over - You lost");
        }
    }
</script>
</head>

<?php
    session_start();
    $filename = $_SERVER['PHP_SELF'];
    $gridsize = 4;
    $nummines = 1;

    if (isset($_SESSION['piece'])){
        // board has been set up previously - game in progress
        if (isset($_GET['new_game'])){
            // start a new game
            unset($_SESSION['piece']);
        }
    }
    if (isset($_SESSION['grid_size'])){
        // game in progress
        $gridsize = $_SESSION['grid_size'];
        $nummines = $_SESSION['num_mines'];
    }
    if (isset($_GET['grid_size'])){
        // New Game - set from form choice
        $gridsize = $_GET['grid_size'];
        $nummines = $_GET['num_mines'];
        $_SESSION['grid_size'] = $gridsize;
        $_SESSION['num_mines'] = $nummines;
    }
?>

<body>
    <div>
    <form name="minesweeper" action="<?php echo $filename ?>" method="GET">
        Enter Grid Size: <input type="input" value="<?php echo $gridsize ?>" name="grid size">
        Enter Number of Mines: <input type="input" value="<?php echo $nummines ?>" name="num_mines">
        <input type="submit" value="New Game" name="new_game"> 
    </form>
    </div>


<?php
// ~~~~~~~~~    VARIABLES   ~~~~~~~~~~
$default_board = "default_board";

$width = "20px";
$height = "20px";
$y_coord = 50;
$how_many_spots = $gridsize;

echo "<table border=1>";

if (isset($_SESSION['piece'])){
    // board has been set up previously - game in progress
    if (isset($_GET['x'])){
        // if cell clicked
        if ($_SESSION['piece'][$_GET['x']][$_GET['y']] == "mine"){
            // if mine - game over 
            $game_over = true;
            $_SESSION['piece'][$_GET['x']][$_GET['y']] = "explode";
        }
        else {
            // mark clicked
            $_SESSION['piece'][$_GET['x']][$_GET['y']] = "clicked";
        }
    }

    // draw board
    $clickcounter = 0;
    for ($y=$gridsize;$y!=0;--$y) {
        $y_coord += $height;
        $x_coord = 50;

        for ($x=$gridsize;$x!=0;--$x){              
                $x_coord += $width; $cell_sum = $x + $y; $remainder = $cell_sum%2; // some math

                if ($_SESSION['piece'][$x][$y] === "explode") {
                     $background_color = "Black"; 
                     $game_over = true;
                }
                elseif ($_SESSION['piece'][$x][$y] === "clicked") {
                    // clicked
                     $background_color = "Green"; 
                     $clickcounter++;
                } 
                elseif ($_SESSION['piece'][$x][$y] === "mine" && isset($game_over)) {
                    //shows all mines when game over
                    $background_color = "#991122"; 
                } 
                elseif ($remainder == 0) {
                    $background_color = "#CCCCCC";
                } 
                else {
                    $background_color = "#AAAAAA";
                }

                echo "<div onclick=\"javascript:document.location.href='$filename?x=$x&y=$y';\" style=\"border: 1px solid black; background-color: $background_color; position: absolute;left: $x_coord; top: $y_coord; width: $width; height: $height;\">";
                echo "</div>";

        }
    }
    if (isset($game_over)){
        echo "<script language='JavaScript'>gameover(false)</script>";                   
    }
    elseif ($clickcounter >= ($gridsize * $gridsize) - $nummines){
        echo "<script language='JavaScript'>gameover(true)</script>";                    
    }
}
else{

    for ($y=$gridsize;$y!=0;--$y) {
        $y_coord += $height;
        $x_coord = 50;

        for ($x=$gridsize;$x!=0;--$x){
                $x_coord += $width; $cell_sum = $x + $y; $remainder = $cell_sum%2; // some math
                // shows a mine for debugging
                if (isset($_SESSION['piece'][$x][$y]) && $_SESSION['piece'][$x][$y]=="mine") {
                    if ($remainder == 0){
                        $background_color = "#CCCCCC";
                    }
                    else {
                        $background_color = "#AAAAAA";
                    }
                } 
                elseif ($remainder == 0) {
                    $_SESSION['piece'][$x][$y] = false;
                    $background_color = "#CCCCCC";
                } 
                else {
                    $_SESSION['piece'][$x][$y] = false;
                    $background_color = "#AAAAAA";
                }

                echo "<div onclick=\"javascript:document.location.href='$filename?x=$x&y=$y';\" style=\"border: 1px solid black; background-color: $background_color; position: absolute;left: $x_coord; top: $y_coord; width: $width; height: $height;\">";
                echo "</div>";
                // ~~~~~~~~~MINES GO HERE ~~~~~
                while ($nummines!=0){
                        $i = rand(1,$gridsize);
                        $e = rand(1,$gridsize);
                        $_SESSION['piece'][$i][$e] = "mine";
                        --$nummines;
                }
        }
        print ("<br>");
    }
}
?>

</body>
</html>