dqef7931 2017-04-11 13:05
浏览 46
已采纳

PHP调试猜测游戏:Mastermind以及我如何用两个玩家解决它

overview:

1 What this is about

2 What questions i have

3 What is bugging

1. What this is about

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

As you see i did player two as well. But i still have the problem that i cannot count how often player two typed the numbers. I have to count it because i want the game to quit if a certain number is reached.

Also i want to clear the screen for player two without clearing his formular, too. but the formular for player one should vanish after player ones submit.

3. Bugs/Debugging

i tried to use a loop but i couldn't figure out how i can use it without creating several formulars at once. it just should count + 1 for every player two submit of numbers.

       <html>
    <head>
      <title>guess a number</title>
    </head>
    <body>
     <h4> guess a number</h4>
     <form action="<?php echo $_SERVER['PHP_SELF'] ?>"method="post">

       <!-- player one is starting here -->
       <p>Spieler 1</p>
       <p>chose your five numbers/Wähle deine 5 Zahlen</p><br />

        <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>

    <?php
    session_start();

    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";
          }
        }

        $i = 0;   // how can i count the second submits?
      while ( $i < 5 )  {
        $i = $i + 1;
        echo "$i";


          //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['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";
              }

            }
          }


        ?>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • dszpyf4859 2017-04-11 13:17
    关注

    In order to do that you need to use session which keeps the data until you end the session, follow the below altered code:

    <?php
    session_start();    
    
    $eins = $_POST['eins'];
    $zwei = $_POST['zwei'];
    $drei = $_POST['drei'];
    $vier = $_POST['vier'];
    $fuenf = $_POST['fuenf'];
    $_SESSION['inputeins'][0] = $eins;
    $_SESSION['inputeins'][1] = $zwei;
    $_SESSION['inputeins'][2] = $drei;
    $_SESSION['inputeins'][3] = $vier;
    $_SESSION['inputeins'][4] = $fuenf;
    
    foreach ($_SESSION['inputeins'] as $ausgabe) {
    
      echo "$ausgabe<br>";
    }   
    /*          echo "<form>";
    echo "<form action='ratespiel.php' method='post'>";
    echo "<input type='submit' value='ok' name='verstecken'>";
    echo "</form>";
    
    if (isset($_POST['verstecken'])){
    $ausgabe = "";
    echo "$ausgabe";
    } */
    /* echo $input[0] . $input[1] . $input[2] . $input[3] . $input[4]; */
    
    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['inputzwei'][0] = $sechs;
        $_SESSION['inputzwei'][1] = $sieben;
        $_SESSION['inputzwei'][2] = $acht;
        $_SESSION['inputzwei'][3] = $neun;
        $_SESSION['inputzwei'][4] = $zehn;
    
        foreach ($_SESSION['inputzwei'] as $ausgabe) {
    
          echo "$ausgabe<br>";
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?