Page 1 of 1

Re: Need help

Posted: Fri Feb 09, 2018 2:14 am
by Andora
Hello,

I need help. I am learning the php, and i am trying to create a hangman game. I have got 2 problems.
1. In the array every accented letter take 2 slots.
2. After the submission, the page does not change.

My code:
akasztofa. php:

Code: Select all

<!DOCTYPE html>
<?php header ('Content-type: text/html; charset=utf-8'); ?>
<html> <head>
<meta http-equiv="content-type"  content="text/html; charset=iso-8859-2" />
<style>
	.tokkozepre{ /*Teljesen középre helyezés */
	position: absolute; top: 50%; left: 50%; 
	transform: translate(-50%, -50%);
	}
</style> </head>
<body>
<?php
$generalSzavak=array("macska", "kerékpár", "folyosó", "tenisz", "nyelőcső", "ember", "citrus", "narancslé", "gyöngytyúk", "köszörü", "aknaszedő", "alábbhagy", "áhítat");
$betuk = ['a', 'á', 'b', 'c', 'd', 'e', 'é', 'f', 'g', 'h', 'i', 'í', 'j', 'k', 'l', 'm', 'n', 'o', 'ó', 'ö', 'ő', 'p', 'q', 'r', 's', 't',' u', 'ú', 'ü', 'ű', 'v', 'w', 'x', 'y', 'z'];
$rossz=array();
$helyes=array();

?> <div class="tokkozepre"><?php

function oldalbetolt($rossz, $randomszo, $helyes){
	?> <form method = "POST" action =" " id="beform">
		<b style="font-size: 20px;">A tippelendő betű: </b><br><br>
		<input type="text" style="height:30px; padding: 5px; font-size: 15px;" name="bevittbetu" pattern="[a-záéiíóöőúüű]{1}" title="A mező csak 1 kisbetűt tartalmazhat!!!"><br><br>
		<input type="hidden" name="info1" id="info1" value="<?php print $rossz; ?>" />  
		<input type="hidden" name="info2" id="info2" value="<?php print $randomSzo; ?>" />
		<input type="hidden" name="info3" id="info3" value="<?php print $helyes; ?>" />
		<br><input type="submit" name ="submit" value="Küld!" style="height:45px; width:60px; font-size: 16px" > 
	</form><?php
}

function ujjatek(){
	global $generalSzavak; global $rossz; global $randomSzo; 
	$randomSzo=$generalSzavak[array_rand($generalSzavak)]; 
	print $randomSzo."<br>";
	for ($i=0; $i<strlen($randomSzo); $i++) $valasz[]= $randomSzo[$i];
	print_r($valasz);
	?><h1 id="szo">
	<?php for ($y=0; $y<strlen($randomSzo); $y++) print $y; ?>
	</h1>
	<script>
	funtion atvalt(){ document.getElementById('beform').action = "akasztofa.php";}
	</script><?php
	oldalbetolt(0,$randomSzo,0);
}

function koztesoldal(){
	$ertek1=$_POST['info1']; $ertek2=$_POST['info2']; $ertek3=$_POST['info3'];
	$tippbetu=$_POST['bevittbetu'];
	
	for ($a=0; $a<strlen($randomSzo); $a++){ 
			if ($tippbetu==$randomSzo[$a]){
				$helyes[]=$tippbetu; $valasz[$a]=$tippbetu; 
			} else { $rossz[]=$tippbetu;}
	}
	oldalbetolt($ertek1, $ertek2, $ertek3);
	if (count($ertek3)==strlen($ertek2)) {print "<b style=\"color:green; font-size: 20px\"\">NYERTÉL - KITALÁLTAD! A keresett szó: ".$randomSzo."<br><br></b>";}
	else{
		
		print "<table>"; print "<tr>"; print "<td>";
		require_once 'abrak.php'; /* Akasztási ábrák */
		print "</td>"; print "</tr>";
		print "<tr>"; print "<td>"; print "<b>Hibás betűk: </b><br>";
		for ($y=0; $y<count($ertek1); $y++) print "<b>".$ertek1[$y].", </b>";
		print "</table>";		
	}
}

if (isset($_POST['info1']) && isset($_POST['info2']) && isset($_POST['info3']) && isset($_POST['bevittbetu']) && ($_POST['submit'] === 'submit')) koztesoldal();
else ujjatek();
?>
</div></body></html>

abrak.php:
<?php
switch (count($ertek1)){ /*Ábra fázisok */
				case "1": 
				print ("Hibás! Próbáld újra.<br><br>");
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
				print("_ _ _|_ _ _");
				break;

				case "2": 
				print ("Hibás! Próbáld újra<br><br>");
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print("_ _ _|_ _ _");
				break;

				case "3": 
				print ("Hibás! Próbáld újra<br><br>");
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print("_ _ _|_ _ _");
				break;

				case "4": 
				print ("Hibás! Próbáld újra<br><br>");
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp_|_<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp".chr(92)."".chr(92)."<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp".chr(92)."".chr(92)."_&nbsp; _/<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print("_ _ _|_ _ _");
				break;

				case "5": 
				print ("Hibás! Próbáld újra<br><br>");
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp_|_<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp".chr(92)."".chr(92)."<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp".chr(92)."".chr(92)."_&nbsp; _/<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
				print("_ _ _|_ _ _");
				break;

				case "6": 
				print ("<b style=\"color:red; font-size: 20px\"\">VESZTETTÉL-Nincs több lehetőség<br><br></b>");
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _<br>";
				print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp_|_<br>";
				print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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; / &nbsp;&nbsp".chr(92)."".chr(92)."<br>";
				print("_ _ _|_ _ _");
				break;	
			}
		?>

Re: Re: Need help

Posted: Fri Feb 09, 2018 1:41 pm
by Christopher
I think you want strlen($ertek1) instead of count($ertek1).

Re: Re: Need help

Posted: Fri Feb 09, 2018 1:57 pm
by Andora
which row?
if this: "switch (count($ertek1)){ /*Ábra fázisok */"
then i used count, because i examine how many incorrect(guessed) letters contain the array.

Re: Re: Need help

Posted: Sun Feb 11, 2018 3:49 pm
by Christopher
Andora wrote:which row?
if this: "switch (count($ertek1)){ /*Ábra fázisok */"
then i used count, because i examine how many incorrect(guessed) letters contain the array.
count() gives you the length of an array. strlen() gives you the length of a string. The value of $ertek1 is from the $_POST which are strings.

Code: Select all

 $ertek1=$_POST['info1']; 

Re: Need help

Posted: Sun Feb 11, 2018 7:51 pm
by thinsoldier
Doesn't strlen give incorrent length for strings containing é ñ ø ü ä å etc ?

Re: Re: Need help

Posted: Sun Feb 11, 2018 7:54 pm
by thinsoldier
echo strlen("hello");
echo "<br>";
echo strlen("héllo");
echo "<br>";
echo mb_strlen("héllo",'UTF-8');

Re: Re: Need help

Posted: Mon Feb 12, 2018 5:14 am
by Andora
Thank you Christopher, I did not know that it changes to a string. I thought the array will stay from the post.

Thinsoldier: i tried with mb_strlen and it work, thank you.


And how can i change the appearance after the submit? Because now they are identical. Firstly i want the tip form to be displayed. If the tip is incorrect the gallows should appear but if its correct the guessed letter should appear in the right place.

Re: Re: Need help

Posted: Mon Feb 12, 2018 10:33 am
by Christopher
Andora wrote:And how can i change the appearance after the submit? Because now they are identical. Firstly i want the tip form to be displayed. If the tip is incorrect the gallows should appear but if its correct the guessed letter should appear in the right place.
I am not sure looking at your code, but you could check each guess by comparing

Code: Select all

// $guess = $_POST['guess']   // e.g., "hangm" (correct) or "hange" (incorrect)
$word = 'hangman';
if ($guess == substr($word, 0, strlen($guess)) ) {
    if (strlen($guess) == strlen($word)) {
        // word matched! you won!
    } else {
        // guesses matches so far, keep going
    }
} else {
    // a character in guess does not match word, show error
}

Re: Need help

Posted: Mon Feb 12, 2018 10:43 am
by Andora
I tried with "function ujatek()" and "function koztesoldal()" and with this:
if (isset($_POST['info1']) && isset($_POST['info2']) && isset($_POST['info3']) && isset($_POST['bevittbetu']) && ($_POST['submit'] === 'submit')) koztesoldal();
else ujjatek();

2 different page.
Sorry that is not entirely in the English language code. I am Hungarian, and variables, alerts i wrote in Hungarian.
Example= "NYERTÉL - KITALÁLTAD!" = "You won!"

Re: Need help

Posted: Mon Feb 12, 2018 11:04 am
by Christopher
Can you explain what this code is doing?

Code: Select all

if (isset($_POST['info1']) && isset($_POST['info2']) && isset($_POST['info3']) && isset($_POST['bevittbetu']) && ($_POST['submit'] === 'submit')) 
  koztesoldal();
else
  ujjatek();

Re: Need help

Posted: Mon Feb 12, 2018 12:19 pm
by Andora
Id rather tell what I want to do.

ujjarek() = start a new game, which generates a random word from the generalSzavak array and it stores it in the "randomSzo""($_POST['info2']). Furthermore, I create a input field which can be used to type in the selected letter ($_POST['bevittbetu']). Below this will be the submit button.
After the player submits the letter, the button activates the "koztesoldal()" on the same page with the previous. I change just the appearance of the website.
It also displays the input field and the submit button, and if the tip is incorrect, the next part of the gallows (which is contains in 'abrak.php') will also appear. If the tip is correct the letter should appear in the right position.
Also, if the tip is incorrect, it stores it in the "rossz" array($_POST['info1']), but if its correct then in the "helyes" array($_POST['info3']).
If the "helyes" array corresponds with the length of the "randomSzo", it writes out on the screen: You Won.

The hidden fields are required due to multiple programs running - after each guess letter it run again. And after every submit, I need to post the data.