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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。