drb56625 2014-04-07 22:02
浏览 67

PHP Session变量比较

:)

I tried to make a game for fun, but I got one problem.

Well, when you open the game, it shows one RAND number. rand(1,20) ..(Min=1 Max=20).. and you got 2 buttons (Smaller) (Bigger)

It has 2 random functions..

So, you have to guess if the next number is higher or lower than the default..

But there is one problem. Program does't compare two random functions properly. :(

<?php
session_start();
?>


<div id="pogodak">
<?php
$rand = rand(1,20);
$rand2 = rand(1,20);



if(isset($_POST['smaller']))
{
    if($rand2<=$rand)
    {
        echo "<font color=#0f0>Succeed</font>";
        $_SESSION['hit']=$_SESSION['hit']+1;
    }   
    else
    {
        echo "<font color=#DE001A>Fail</font>";
        unset($_SESSION['hit']);
        session_destroy;
    }
}
if(isset($_POST['bigger']))
{

    if($rand2>=$rand)
    {
        echo "<font color=#0f0>Succeed</font>";
        $_SESSION['hit']=$_SESSION['hit']+1;
    }   
    else
    {
        echo "<font color=#DE001A>fail</font>";
        unset($_SESSION['hit']);
        session_destroy;
    }
}
session_destroy;
?>
</div>

down there...

    <table width="1000">
<form action="" method="post">
  <tr>
    <td width="321" align="center" valign="middle">
        <input type="submit" name="smaller" value="Smaller" class="button"/></td>

    <td width="348" align="center" valign="middle" class="veliko"><? echo $rand;?></td>

    <td width="315" align="center" valign="middle" class="veliko">
        <input type="submit" name="bigger" value="Bigger" class="button"/></td>

  </tr>
  <tr>
    <td height="83" colspan="3" align="center" valign="middle" class="srednje"><? echo "Hits: " . $_SESSION['hits']"</br>";?></td>
    </tr>
    <tr>
    <td height="83" align="center" valign="middle" >Nickname:</td>
    <td height="83" align="center" valign="middle" ><input type="text" name="name"/></td>
    <td height="83" align="center" valign="middle" ><input type="submit" name="kraj" value="Finish!" />
</td>
    </tr>
    </form>
</table>
  • 写回答

1条回答 默认 最新

  • dongzi5673 2014-04-07 22:11
    关注

    $rand and $rand2 gets the same value, try put another seed between the 2 values and check the callback when the numbers have the same value

    $rand = rand(1,20);
    //srand goes here
    $rand2 = rand(1,20);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行