dongyongju9560 2012-10-01 17:25
浏览 60

PHP中的计数器变量[重复]

Possible Duplicate:
Variable always resetting

so I'm not too familiar with html or php, but I've been working on a game like mastermind, and one persistent problem I have been having with my code is that the variable $attempts seems to always reset, so that it's value is always 1. Is there a way to fix this? Most of my code is down here:

<?php
$showtable = false;
$showform = false;
$showstart= true;
$process = false;
$black = 0;
$white = 0;
$answer = array(1,2,3,4);
$tries = array();
$attempts = 0;

if (isset($_POST['started']))
{
    $showform = true;
    $showstart = false;
}

if (isset($_POST['guessed']))
{
    $process = true;
}

if ($showstart == true)
{
    echo"
    <form action='' method='post'>
    <input type='submit' value='Start' name='start' />
    <input type='hidden' value='started' name='started'/>
    </form><Br /><br />
    ";
}

if ($showform == true)
{
    echo"
    <h3>Your guess:</h3>
    <form action='gogo.php' method='post'>
    <input type='text' placeholder='Your guess' maxlength='4' value='' name='guess' />
    <input type='submit' value='guess' name='submitt' />
    <input type='hidden' value='started' name='started' />
    <input type='hidden' value='guessed' name='guessed' />
    </form><Br /><br />
    ";
}

if ($process == true)
{
    $attempts += 1;
    $guess = str_split($_POST['guess']);
    if ($guess == $answer)
    {
        $black = 4;
    } else
    {
        for ($i=0;$i<4;$i++)
        {
            if ($guess[$i] == $answer[$i])
            {
                $black += 1;
                $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);
$try = $attempts.".".$chance.".".$white.".".$black;
array_push($tries, $try);
$showtable = true;

if ($showtable == true)
{
    echo"
    <table border='2' cellpadding='10'>
    <Tr><Td>Attempt:</td><td>Number guessed:</td><td>White:</td><Td>Black:</td></tr>
    ";
    for ($i=0;$i<$attempts;$i++)
    {
        $split = explode(".",$tries[$i]);
        echo"
        <tr><td>".$split[0]."</td><td>".$split[1]."</td><td>".$split[2]."</td>    <td>".$split[3]."</td></tr>
        ";
    }
    echo"
    </table>
    ";
}
?>
  • 写回答

2条回答 默认 最新

  • dongruidian3064 2012-10-01 17:29
    关注

    At the start of your code you initialise the variable to zero, so every time the code is run (namely every time a move is made) the variable is "reset".

    You should save the variables somehow, either with a database or with a $_SESSION variable. But note that you will have to add checks to make sure that what's sent in $_POST is consistent with what you expect at this point in the game.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题