Reputation: 49
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']}";
}
}
}
?>
Upvotes: 0
Views: 129
Reputation: 49
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']}";
Upvotes: 1
Reputation: 8288
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
Upvotes: 1
Reputation: 2581
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.
Upvotes: 1