dsrw29618 2015-09-25 03:53
浏览 27
已采纳

PHP会话不适用于游戏

I am trying to imitate a game called SKUNK (you play it with dice) for an assignment.

I can't get the sessions to work. This is my first time using PHP. Also I was told it can be done without sessions.

Here is my code:

<form method="get" action= "skunk.php">
    <h1>PLAY SKUNK</h1>

    ROLL AGAIN? <br> 
    yes<input type="radio" name="role2" value="yes"/>
    no<input type="radio" name="role2" value="no"/>
    <br>
    <input type="submit"/>

    </form>

<?php 

session_start();
    $_SESSION["s"] = array(0);
    $_SESSION["k1"] =array(0);
    $_SESSION["u"] = array(0);
    $_SESSION["n"] = array(0);
    $_SESSION["k2"] = array(0);
    $rand = 0;
    $rand2 = 0;
    $_SESSION["round"] = 1;
    if($_REQUEST["role2"] == "yes"){
        $rand = rand (1, 6);
        $rand2 = rand(1, 6);
         if($rand == 1 and $rand ==1){
            switch ($_SESSION["round"]) {
                case 1:
                    $_SESSION["s"] = array(0);
                    break;
                case 2:
                    $_SESSION["s"] = array(0);
                    $_SESSION["k1"] =array(0);
                    break;
                case 3:
                    $_SESSION["s"] = array(0);
                    $_SESSION["k1"] =array(0);
                    $_SESSION["u"] = array(0);
                    break;
                case 4:
                    $_SESSION["s"] = array(0);
                    $_SESSION["k1"] =array(0);
                    $_SESSION["u"] = array(0);
                    $_SESSION["n"] = array(0);
                    break;
                case 5:
                    $_SESSION["s"] = array(0);
                    $_SESSION["k1"] =array(0);
                    $_SESSION["u"] = array(0);
                    $_SESSION["n"] = array(0);
                    $_SESSION["k2"] = array(0); 
                    break;
            }
         }
         if($rand > 1 and $rand2 > 1) {
            switch ($_SESSION["round"]) {
                case 1:
                    array_push($_SESSION["s"], $rand, $rand2);
                    break;
                case 2:
                    array_push($_SESSION["k1"], $rand, $rand2);
                    break;
                case 3:
                    array_push($_SESSION["u"], $rand, $rand2);
                    break;
                case 4:
                    array_push($_SESSION["n"], $rand, $rand2);
                    break;
                case 5:
                    array_push($_SESSION["k2"], $rand, $rand2);
                    break;
            }
            $_SESSION["round"]++;
        }

        if($_SESSION["round"] > 5){
            session_destroy();
        }
        echo "you rolled: " . $rand . "
";
        echo "you rolled: " . $rand2;

    }

?>
    <table style="width:100%" border="1" >
          <tr>
            <td>S</td>
            <td>K</td> 
            <td>U</td>
            <td>N</td>
            <td>K</td>
          </tr>
    </table>
    <table style="width:100%" border="1" >
          <tr>
            <td><?php echo array_sum($_SESSION["s"]); ?></td>
            <td><?php echo array_sum($_SESSION["k1"]); ?></td> 
            <td><?php echo array_sum($_SESSION["u"]); ?></td>
            <td><?php echo array_sum($_SESSION["n"]); ?></td>
            <td><?php echo array_sum($_SESSION["k2"]); ?></td>
          </tr>
    </table>
  • 写回答

1条回答 默认 最新

  • dras2334 2015-09-25 05:02
    关注

    if you use session at any point session_start() should be the first line of just before any printing functions.

    start session at the starting of the page then try to print echo " $_SESSION["s"]";

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀