0

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

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

how would that session tries count look like? I don't want to use javascript because i have to complete this programm in PHP (and html) only. is there another possibility to clean the screen or should i use a second file for html then?
'how would that session tries count look like?' i think that the example that @Bram mentioned explains that
'is there another possibility to clean the screen or should i use a second file for html then?', yes , but this will makes you refresh the whole page using php.
youre right. so i could split up the php file in one php.file and one htlm.file to only have one formular that is used for player one and player two. if i count that i could tell the programm with IF to change when session times is at a certain point. ufff -.- i think i see how this could work. I'll try it out.
you may do this, it will be better to make a fair enough analysis and researches to get the best way to do this. try this and comeback with a feed back, good luck
|
1

I think you want to take a look at PHP Session Variables. This way you can load the Session variable and do an increment on the number.

Something in the style of:

// Initialise the variable on top of the code somewhere.
// Should only run once.
$_SESSION['times_guessed'] = 0;

// In your logic where you want to do the increment.
$_SESSION['times_guessed']++;

You can also see a basic example here in the manual.

2 Comments

kind of: so i have to put the times guessed above player two or into the player twos submit? it seems i have to seperate the code from html to get this properly done...
I see you already figured out how to use the session variables as you posted the answer :) congrats.
1

as i used the code here like this it worked.

  session_start();

//should count up

if (!isset($_SESSION['geraten'])) {
  $_SESSION['geraten'] = 0;
} else {
  $_SESSION['geraten']++;
}

$runde = $_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']}";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.