Jump to content

Learning PHP, and I built a simple hangman game

- - - - -

  • Please log in to reply
2 replies to this topic

#1
bakhtn

bakhtn

    Newbie

  • Members
  • Pip
  • 8 posts
I have to admit it was a challenge you have to know what methods to use, in_array was very useful here


<?php 

 $internalword = "dad";

 //echo $_POST["previousletter"];

 //echo $_POST["letter"];

 $internalword = str_split($internalword);

 $externalword = str_split($_POST["letter"].$_POST["previousletter"]);

 

 if(isset($_POST["life"]))

 {

 	$life = $_POST["life"];

 }

 else

 {

    $life=7;	

 }

 if(in_array(strtolower($_POST["letter"]), $internalword)||(!isset($_POST["letter"])))

 {

 	//do nothing

 }

 else

 {

 	$life--;

 }

 

 if($life>0)

 { 	

 $i=0;

 

 foreach ($internalword as $value)

 {

 	if(in_array(strtoupper($value), $externalword))

	{		

		$displayword .= ' '.$value.' ';

	}

	else

	{

		$displayword .= ' _ ';

		

	}

	$i++;

 }

 echo $displayword;

 }

 else

 {

 	echo 'you loose';

 }

 echo ' '.$life

?>

<html>

<body>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<?php

 //display a-z buttons

 $az = range(a, z);

 

 

 foreach ($az as $lettervalue)

 {

 	$lettervalue = strtoupper($lettervalue);

	echo "<input name=\"letter\" type=\"submit\" value=\"$lettervalue\">";

 }

?>

<input name="previousletter" type="hidden" value="<?php echo $_POST["previousletter"] . $_POST["letter"]; ?>">


<input name="life" type="hidden" value="<?php echo $life ?>">

</form>

 

</body>

</html>



#2
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Most methods starts with datatype . Ex: strlen, array_intersect, array_keys, intval...or methods like is_(datatype) which is check a datatype of argument.
So it's no a big deal - just go to manual and search for right one.
Bassically method name describes what it's for, so you can real quick find it in manual.
You will get used to it, when will be writing a lot.
Oh, point of my post was that
  • First of all - you want to handle the win condition.Some kind of text. But then you need to reset game status, or show a button like - start again. And you definitelly don't want All the letters appear after win, cause if user will continue clicking them - he find himself losing.
  • You might need a better checking what user is trying to do. for example - you absolutely don't want that the user mess with "life". So maybe you will find a way to store that data not in a page, but more secure way.
  • You might considering defining the constants for number values - example life and range of letters.
  • I don't really like to use $_SERVER variable. So if you want, for example - land on the same page - i think better to hardcode a file name or leave blank.
  • It's a good practice to use echo($var); instead of echo $var; - for consistency. think of echo like a function.
  • You are doing many modification on strtoupper, tolower...maybe you need it just once at start - and than use just string that you have already normalized.
  • I don't like str_split. you still can think of a string as an array of char. so you can iterate through characters in a string.
  • //!do nothing - it's bad to leave if condition unfilled and then use else statement, maybe you need to recheck condition and try to built an opposite condition.
That's all i can remeber. This is my suggestions, so you can agree or disagree. As well as criticize me from your point of view)
For begginer you did great. Just try not to rush throught basics.
That's why i leave all function,classes,namespaces topics aside for this program.
Cheers.:c-thumbup:

#3
bakhtn

bakhtn

    Newbie

  • Members
  • Pip
  • 8 posts
thanks for the feedback




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users