0

我有 3 个骰子对象,在被抛出一次后,我希望用户能够选择其中 1 个骰子,无论选择哪个,都将保存到一个新的单独会话变量中,以保存代表骰子的骰子对象- 用户输入的数字。

然后我的意图是将其余的骰子对象保存到另一个单独的会话变量中,以保存要再次抛出的骰子对象。

但这似乎不像我想要的那样工作-> 尝试结合使用 $_GET-variables 来保存通过隐藏表单文本字段输入的数字,并使用 session-variables 来保存骰子对象。

代码如下所示:

<?php
error_reporting(-1);

require_once('../CDiceSvg.php');
require_once('../CDiceGame.php');

session_start();

//Initiate 3 variables to hold the dices
$_SESSION['yatzygame']['dices'] = Array();

//Initiate 2 variables to maintain the dices to save respectively reroll.
$_SESSION['yatzygame']['dicesToSave'] = Array();
$_SESSION['yatzygame']['dicesToReRoll'] = Array();


//Initiate the 3 dices.
for($i = 1; $i <= 3; $i++)
{
    $_SESSION['yatzygame']['dices'][$i] = new CDiceSvg();
}

//If game has not yet been started:
if(!isset($_GET['roundcounter']))
{
    //Then hide the continue and enough-buttons
    $status = "";
    $status2 = "style=\"display: none;\"";
    $throwCounter = 0;

}

//If the "kasta"-button has been pressed -> Start the game:
if(isset($_GET['button']) && $_GET['button'] == "kasta")
{
    $status = "style=\"display: none;\"";
    $status2 = "";
    $_GET['roundcounter']++;
    $throwCounter = $_GET['roundcounter'];

    //Throw the dices
    for($i = 1; $i <= 3; $i++)
    {
        $_SESSION['yatzygame']['dices'][$i]->ThrowDieEnhanced();
    }
}

//If game has been started/If the first dices has been tossed:
if($_GET['roundcounter'] == 1)
{
    //Fetch the dices values (given as integers):
    $diceValues = Array();

    for($i = 1; $i <= 3; $i++)
    {
        $diceValues[$i] = $_SESSION['yatzygame']['dices'][$i]->GetLastThrow();
    }

    //Print the dices generated values:
    print_r($diceValues);



}

if(isset($_GET['dicekeeping']))
{
    //Store the chosen dice to keep/save:
    $dicesToKeep = $_GET['dicekeeping'];
    echo "<br><br>" . $dicesToKeep;
}

/* 代码似乎一直有效,直到下面:这就像骰子对象没有正确保存到新的分离会话变量中...... */

//Save the dices until the next round accordingly to user-choice:
if(isset($_GET['dicekeeping']) && $_GET['button'] == "continue")
{
    for($i = 1; $i <= 3; $i++)
    {
        if($_GET['dicekeeping'] == $i)
        {
            $_SESSION['yatzygame']['dicesToSave'][$i] = $_SESSION['yatzygame']['dices'][$i];

        }else
        {
            $_SESSION['yatzygame']['dicesToReRoll'][$i] = $_SESSION['yatzygame']['dices'][$i];

        }
    }

    print_r($_SESSION['yatzygame']['dicesToSave']);

    //Store the values to check so everything goes according to plan:
    $printValuesForSave = Array();

    $counter = 0;
    foreach($_SESSION['yatzygame']['dicesToSave'] as $diceObj)
    {
        $printValuesForSave[$counter] = $diceObj->GetLastThrows();
        $printValuesForSave[$counter] = $printValuesForSave[$counter][0];
        $counter++;
    }
    echo "<br><br>";
    print_r($printValuesForSave);
    print_r($_SESSION['yatzygame']['dicesToReRoll']);

    $_GET['roundcounter']++;
    $throwCounter = $_GET['roundcounter'];
}



//Go to Round 2 and print-out new state of the dices and handle previous choices:
if($_GET['roundcounter'] == 2)
{
    foreach($_SESSION['yatzygame']['dicesToSave'] as $diceSaved)
    {
        echo "Dices saved from last round: {$diceSaved}";
    }

    //Reroll dices indirectly chosen to be rerolled earlier:
    foreach($_SESSION['yatzygame']['dicesToReRoll'] as $dice)
    {
        $dice->ThrowDieEnhanced();
    }

    //Get the new values from the rerolled dices:
    $newValuesArray = Array();
    foreach($_SESSION['yatzygame']['dicesToReRoll'] as $diceNmbr => $diceValue)
    {
        $newValuesArray[$diceNmbr] = $_SESSION['yatzygame']['dicesToReRoll'][$diceNmbr]->GetLastThrow();
    }

    foreach($newValuesArray as $newDiceValue => $newValue)
    {
        echo "<br><br>Here comes the {$newDiceValue} new value: {$newValue}";
    }
}

//Print the form..
echo <<<EOD
        <form action="framework_Test.php" method="get">
        <input type="hidden" name="roundcounter" value="{$throwCounter}">
        <button type="submit" name="button" {$status} value="kasta">Kasta</button>
        <input type="text" name="dicekeeping" {$status2} autofocus>
        <input type="hidden" name="dicestokeep" value="{$dicesToKeep}">

        <button type="submit" name="button" {$status2}value="continue">Fortsätt</button>
        <button type="submit" name="button" {$status2} value="enough">Nöjd</button>
        <a href="{$_SERVER['PHP_SELF']}" {$status}>Starta om</a>
EOD;
?>

对于正在使用的类,代码如下: CDiceSvg.php 扩展的核心代码,我在这个例子中主要使用它;

private $iLastThrow = 0;


//Function that throws a dice and returns a random value
private function ThrowDie()
{
    return rand(1,6);   
}

public function ThrowDieEnhanced()
{
    $this->iLastThrow = 0;
    $this->iLastThrow = $this->ThrowDie();
    return $this->iLastThrow;
}
    public function GetLastThrow()
{
    return $this->iLastThrow;
}

我对整个 PHP 语言编码还是很陌生,对 SESSION 不是很熟悉,但在我看来,这个“框架”的逻辑——对于 yahtzee/代码来说应该足够了,不知道出了什么问题:/ 请帮助!被这个问题困扰了4天多了...

打个比方说,我的头撞到墙上太多了,以至于无法辨认,我一直因为这个问题把头发拉到秃顶,然后开始爬墙 - 无论我多么努力地盯着代码看,都没有想到:/ -换句话说,迫切需要启蒙:)

提前谢谢,顺便说一句。如果有更好的方法来做我想做的事情,我愿意接受建议。

4

1 回答 1

0

如果我错了,有人纠正我,但我不确定++累加器运算符是否可以使用$_GET变量。你在哪里:

$_GET['roundcounter']++;
$throwCounter = $_GET['roundcounter'];

.
.
.

if($_GET['roundcounter'] == 2)
{
    ...
}

我认为您必须执行以下操作:

$throwCounter = $_GET['roundcounter'];
$throwCounter++;

.
.
.

if($throwCounter == 2)
{
    ...
}
于 2013-04-26T10:47:49.033 回答