我正在使用表单创建一个类似于 mastermind 的游戏。我的问题是我有一个变量 $attempts,我希望每次用户猜测一个数字时它都会增加,但它似乎总是重置为零,所以我的尝试次数将始终显示为 1。如果有帮助,这里是我正在使用的代码:
$black = 0;
$white = 0;
$answer = array(1,2,3,4);
$tries = array();
$attempts = 0;
if ($process == true)
{
$guess = str_split($_POST['guess']);
if ($guess == $answer)
{
$black = 4;
} else
{
for ($i=0;$i<4;$i++)
{
if ($guess[$i] == $answer[$i])
{
$black = $black + 1;
$white = $white - 1;
}
}
$result = array();
foreach ($guess as $val)
{
if (($key = array_search($val, $answer))!==false)
{
$result[] = $val;
unset($answer[$key]);
}
}
$count = count($result);
$white = $white + $count;
}
}
$chance = implode(" ",$guess);
$attempts += 1;
$try = $attempts.".".$chance.".".$white.".".$black;
array_push($tries, $try);