尝试将dices对象保存为单独的会话变量,但不起作用(yahtzeegame php)
我有3个骰子对象,在抛出一次之后,我希望用户能够选择其中的1个骰子,无论选择哪一个,都将保存到一个新的单独会话变量中,以保存代表用户输入的骰子编号的骰子对象 然后,我的意图是将剩下的骰子对象保存到另一个单独的会话变量中,以保存要再次抛出的对象 但这似乎不是我想要的方式->尝试结合使用$\u GET-variables来保存通过隐藏表单textfield输入的数字,以及会话变量来保存骰子对象 代码如下所示:尝试将dices对象保存为单独的会话变量,但不起作用(yahtzeegame php),php,session,variable-assignment,simulation,Php,Session,Variable Assignment,Simulation,我有3个骰子对象,在抛出一次之后,我希望用户能够选择其中的1个骰子,无论选择哪一个,都将保存到一个新的单独会话变量中,以保存代表用户输入的骰子编号的骰子对象 然后,我的意图是将剩下的骰子对象保存到另一个单独的会话变量中,以保存要再次抛出的对象 但这似乎不是我想要的方式->尝试结合使用$\u GET-variables来保存通过隐藏表单textfield输入的数字,以及会话变量来保存骰子对象 代码如下所示: <?php error_reporting(-1); require_once('
<?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;
}
我对整个PHP语言编码还是很陌生,对会话也不是很熟悉,但在我看来,这个“框架”的逻辑——对于yahtzee/代码来说应该足够了,我不知道到底出了什么问题:/
请帮忙!这个问题已经困扰了4天多了
打个比方说,我的头撞到了墙上,以至于我再也认不出来了,因为这个问题我一直把头发拔到秃顶,然后开始爬墙——不管我多么努力地盯着代码看,都没有想到:/
-换句话说,极度需要启蒙:)
Thx提前,顺便说一句。如果有更好的方法来做我想做的事情,我愿意接受建议。如果我错了,有人会纠正我,但我不确定
++
累加器运算符是否可以与$\u GET
变量一起使用。如果您有:
$_GET['roundcounter']++;
$throwCounter = $_GET['roundcounter'];
.
.
.
if($_GET['roundcounter'] == 2)
{
...
}
我认为你必须做一些类似的事情:
$throwCounter = $_GET['roundcounter'];
$throwCounter++;
.
.
.
if($throwCounter == 2)
{
...
}
这似乎是可行的,因为当我运行代码时,throwCounter在接下来的代码阶段会增加到2。但我不确定,但我不认为这是我正在经历的问题,或者可能是什么;o
$_GET['roundcounter']++;
$throwCounter = $_GET['roundcounter'];
.
.
.
if($_GET['roundcounter'] == 2)
{
...
}
$throwCounter = $_GET['roundcounter'];
$throwCounter++;
.
.
.
if($throwCounter == 2)
{
...
}