dslijian2015 2017-04-12 07:51
浏览 38
已采纳

PHP:如何计算/循环第二个输入

1 What this is about

2 What questions i have

3 What is bugging

1. What this is about

As this is still the same programm the intro stays the same

this is about programming a little number game. i think it is also known as mastermind. the game has the following rules.

there are two players

first the starting player types 5 numbers

then the second player types 5 numbers to guess the numbers from the first player

the programm has to echo how many numbers of the second player are at the right place and also how many numbers are correctly guessed.

the programm has to run a fiew times, because player two has several tries

i want to use one formular for both players if possible aka one submit button

i don't want to know how this programm is coded as a whole but i have specific questions for some steps.

2. What questions i have

Player twos input should be counted. I need that information to stop the game at a certain time. i rewrote my question because the first one was properly answered.

How can i do that? How does the loop have to look like? Is it possible to delete player ones formular and echo after it was shown on screen?

Normally i know how a loop works but i did not work with a loop for submissions before.

3. Bugs/Debugging Got my indexes right this time.

the code

  <?php


    session_start();
    $_SESSION['geraten'] = 1;
    if($_SESSION['geraten'] == 1) {
      echo "<br>";
      echo "Spieler 1";
      echo "<form method='post'>";
      echo "chose your five numbers/Wähle deine 5 Zahlen<br>";
      echo "<input type='text' name='one' size='1' maxlength='1'>";
      echo "<input type='text' name='two' size='1' maxlength='1'>";
      echo "<input type='text' name='three' size='1' maxlength='1'>";
      echo "<input type='text' name='four' size='1' maxlength='1'>";
      echo "<input type='text' name='five' size='1' maxlength='1'>";
      echo "<input type='submit' name='gesendet' value='OK'>";
      echo "</form>";
      echo "<br>";
      echo "{$_SESSION['geraten']}";




         //player one is starting here

        //<input type="password" name="one" size="1" maxlength="1">
        //<input type="password" name="two" size="1" maxlength="1">
        //<input type="password" name="three" size="1" maxlength="1">
        //<input type="password" name="four" size="1" maxlength="1">
        //<input type="password" name="five" size="1" maxlength="1">

            //<input type="submit" name="gesendet" value="ok"></button> <br />
        //</form> -->



    if(isset($_POST['gesendet'])){

      $one = $_POST['one'];
      $two = $_POST['two'];
      $three = $_POST['three'];
      $four = $_POST['four'];
      $five = $_POST['five'];
// array to safe the input of player one with sessions
      $_SESSION['anumberone'][0] = $one;
      $_SESSION['anumberone'][1] = $two;
      $_SESSION['anumberone'][2] = $three;
      $_SESSION['anumberone'][3] = $four;
      $_SESSION['anumberone'][4] = $five;

        foreach ($_SESSION['anumberone'] as $ausgabe) {

          echo "$ausgabe";

        }
      }
    }

        // how can i count the second submits?

      else {
          //start with player two here!
        echo "<br>";
        echo "Spieler 2";
        echo "<form method='post'>";
        echo "Ihre Ziffern:<br>";
        echo "<input type='text' name='sechs' size='1' maxlength='1'>";
        echo "<input type='text' name='sieben' size='1' maxlength='1'>";
        echo "<input type='text' name='acht' size='1' maxlength='1'>";
        echo "<input type='text' name='neun' size='1' maxlength='1'>";
        echo "<input type='text' name='zehn' size='1' maxlength='1'>";
        echo "<input type='submit' name='submitzwei' value='OK'>";
        echo "</form>";

        if(!empty($_POST['submitzwei'])){
          $sechs = $_POST['sechs'];
          $sieben = $_POST['sieben'];
          $acht = $_POST['acht'];
          $neun = $_POST['neun'];
          $zehn = $_POST['zehn'];

          $_SESSION['geraten']++;

          $_SESSION['anumber2'][0] = $sechs;
          $_SESSION['anumber2'][1] = $sieben;
          $_SESSION['anumber2'][2] = $acht;
          $_SESSION['anumber2'][3] = $neun;
          $_SESSION['anumber2'][4] = $zehn;

            foreach ($_SESSION['anumber2'] as $ausgabe) {

              echo "$ausgabe";
              echo "<br>";
              echo "{$_SESSION['geraten']}";
          }

        }
      }



        ?>
  • 写回答

3条回答 默认 最新

  • duannaoben8011 2017-04-12 08:29
    关注

    Player twos input should be counted. I need that information to stop the game at a certain time. i rewrote my question because the first one was properly answered.

    How can i do that?

    there are multiple approaches to do that, for instance :-
    - using sessions to store the tries count
    - using database to store and fetch the tries count
    - the hard/bad way with storing your count into a file and retrieve it when needed .
    - do it throw the client side using javascript.

    How does the loop have to look like?

    depends on how you will do it as described .

    Is it possible to delete player ones formular and echo after it was shown on screen?

    Yes, using javascript you can manipulate your DOM document

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧