Jump to content

hangman game [was: Hello]


lilush

Recommended Posts

I am trying to make a hanging man game.

I have ten words with 5 letters each.

the program is supposed to choose one word out of the ten randomly and the user has to guess the letters and replace the underlines that appear on screen. the correct letters stay on screen. there is also a picture of the hanging man and with each mistake a body part appears- they get 6 tries or they fail.

This is my code so far, it's a mess because I don't really know what i'm doing... (my ten words are in hebrew at the top)

I'll really appreciate some help :)

<?php
session_start();

if ( !isset($_SESSION['word']) )
{

$wordList="שולחן
וילון
מדפסת
גיטרה
פסנתר
דרקון
תרמיל
בקבוק
חולצה
תמונה";
}
?>

<html dir="rtl">
<body>
	<?php
	if(isset($_POST["exit"]))
	{
	?>
		תודה ולהתראות!
		<br>
		<form action="game.php" method="post"><br><br>
			<input type="submit" value="חזור למשחק" /> 
		</form>
	<?php
	}
	elseif(isset($_POST["guessletter"])) 
	{
		if(!isset($_SESSION["totalGuess"]))
		{
		$_SESSION["totalGuess"] = 0;
		}
			$totalGuess= 0;		
	}
	$maxError=6;
	$wordList= rand(1,10);
	$pickedWord= $wordList
	if (!isset($_SESSION["word"]))

	?>

איש תלוי
<br><br>
<img src="0.jpg"/>
<form action="game.php" method="post" >
<font size="+6"><?php echo "_ _ _ _ _"; ?></font><br><br>
<input type="text" name="guessletter" />
<input type="submit" name="guess" value= "נחש"/>
<br><br>
<input type="submit" name="exit" value= "יציאה"/>
</form>









</body>
</html> 

 

mod edit: meaningful title and put ending php code tag after the end of the code (edit2: unfortunately, the php code tag processing displays the foreign language characters as entities.)

Link to comment
Share on other sites

ok- in english:

 

<?php

session_start();

 

if ( !isset($_SESSION['word']) )

{

 

$wordList="table

curtain

printer

guitar

piano

dragon

pack

bottle

shirt

picture";

}

?>

 

<html>

  <body>

      <?php

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

      {

      ?>

        thank you and goodbey!

        <br>

        <form action="game.php" method="post"><br><br>

            <input type="submit" value="חזור למשחק" />

        </form>

      <?php

      }

      elseif(isset($_POST["guessletter"]))

      {

        if(!isset($_SESSION["totalGuess"]))

        {

        $_SESSION["totalGuess"] = 0;

        }

            $totalGuess= 0;     

      }

      $maxError=6;

      $wordList= rand(1,10);

      $pickedWord= $wordList

      if (!isset($_SESSION["word"]))

     

      ?>

 

hanging man

<br><br>

<img src="0.jpg"/>

<form action="game.php" method="post" >

  <font size="+6"><?php echo "_ _ _ _ _"; ?></font><br><br>

  <input type="text" name="guessletter" />

  <input type="submit" name="guess" value= "guess"/>

  <br><br>

  <input type="submit" name="exit" value= "exit"/>

</form>

 

 

 

 

 

 

 

 

 

</body>

</html>

 

Link to comment
Share on other sites

$wordlist = array(
    'table'   ,
    'curtain' ,
    'printer' ,
    'guitar'  ,
    'piano'   ,
    'dragon'  ,
    'pack'    ,
    'bottle'  ,
    'shirt'   ,
    'picture'
);

for ($i = 0; $i < 10; ++$i) {
    $randomword = $wordlist[array_rand($wordlist)];   // get random word
    echo $randomword . '<br />';
}

Link to comment
Share on other sites

For the fun of it.

<?php
session_start(); 

$wordList = array(
'table',
'curtain',
'printer',
'guitar',
'piano',
'dragon',
'pack',
'bottle',
'shirt',
'picture'
);


if (!isset($_SESSION['word']) || isset($_POST['reset'])){
$wordkey=array_rand($wordList);
$word=$wordList[$wordkey];
$_SESSION['word']=$word;
$_SESSION["totalGuess"]=0;
$_SESSION["matches"]=array();	
}else{
$word=$_SESSION['word'];
}
$wordlength = strlen($word);
$totalGuess = (!isset($_SESSION["totalGuess"])? 0 : $_SESSION["totalGuess"]);

//create array of letters in word
$word_array = str_split($word);

//Check post against word_array
if (isset($_POST['guess']) && $totalGuess<=$wordlength){
$letter = trim($_POST['guessletter']);
$matchkey = (in_array($letter,$word_array) ? array_search($letter,$word_array) : '');
	for($i=0;$i<=($wordlength-1);$i++){
		if (isset($_SESSION["matches"][$matchkey]) && isset($_SESSION["matches"][$i]) && $_SESSION["matches"][$matchkey]==$_SESSION["matches"][$i]){
			$_SESSION["matches"][$i]=$_SESSION["matches"][$matchkey];
		}elseif($word_array[$i]==$letter){
			$_SESSION["matches"][$i]="$letter";
		}
	}
$_SESSION["totalGuess"]++;
header("location: game.php");
exit;
}
ksort($_SESSION["matches"]);

//style display section
$reset="";

if ($totalGuess==$wordlength || count($_SESSION["matches"])==$wordlength){
	if (count($_SESSION["matches"])==$wordlength){
		foreach($word_array as $key => $l){
			if ($_SESSION["matches"][$key]!=$word_array[$key]){
				$reset="<br />Reset for new game";
				$color="#ff0000";
				$fail= true;					
			}				
		}
		$color="#006600";
		$reset=(!isset($fail) ?	"<br /><span style=\"color:#006600;\">YOU WON</span><br />Reset for new game" : "<br />Reset for new game");
	}else{
	$color="#ff0000";
	$reset="<br />Reset for new game";
	}
}else{
	$color="#000000";
}
//display section
$display = "<span style=\"color:$color; font-size:28px;\">";
	for($d=0;$d<=($wordlength-1);$d++){
		$display .= (isset($_SESSION["matches"][$d]) ? "{$_SESSION["matches"][$d]}" : "_")." "; 
	}	
$display .= "</span>.$reset";
$display .= "<br />$totalGuess out of $wordlength";   
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
	<title>What's that word</title>
</head>
<body>
<center>
<form action="game.php" method="post" >
   <?php echo "$display"; ?><br /><br />
   <input type="text" name="guessletter" />
   <input type="submit" name="guess" value= "&#1504;&#1495;&#1513;"/>
   <br /><br />
   <input type="submit" name="reset" value="&#1497;&#1510;&#1497;&#1488;&#1492;"/>
</form>
</center>
</body>
</html>

Link to comment
Share on other sites

WOW

Thank you!!!

How do I get the images of the hanging man to appear whenever someone makes a mistake? I have 8 images the first one is 0 mistake so it is displayd on the start and with each mistake a body part appears (image mistake 2, mistake 3 etc)

Link to comment
Share on other sites

Something like this should work.

<?php
session_start(); 

$wordList = array(
'table',
'curtain',
'printer',
'guitar',
'piano',
'dragon',
'pack',
'bottle',
'shirt',
'picture'
);


if (!isset($_SESSION['word']) || isset($_POST['reset'])){
$wordkey=array_rand($wordList);
$word=$wordList[$wordkey];
$_SESSION['word']=$word;
$_SESSION["totalGuess"]=0;
$_SESSION["matches"]=array();	
}else{
$word=$_SESSION['word'];
}
$wordlength = strlen($word);
$totalGuess = (!isset($_SESSION["totalGuess"])? 0 : $_SESSION["totalGuess"]);

//create array of letters in word
$word_array = str_split($word);

//Check post against word_array
//if (isset($_POST['guess']) && $totalGuess<=$wordlength){
if (isset($_POST['guess'])){
$letter = trim($_POST['guessletter']);
$matchkey = (in_array($letter,$word_array) ? array_search($letter,$word_array) : '');
	for($i=0;$i<=($wordlength-1);$i++){
		if (isset($_SESSION["matches"][$matchkey]) && isset($_SESSION["matches"][$i]) && $_SESSION["matches"][$matchkey]==$_SESSION["matches"][$i]){
			$_SESSION["matches"][$i]=$_SESSION["matches"][$matchkey];
		}elseif($word_array[$i]==$letter){
			$_SESSION["matches"][$i]="$letter";
		}
	}
$_SESSION["totalGuess"]++;
header("location: game.php");
exit;
}
ksort($_SESSION["matches"]);

//style display section
$reset="";
	   
$miss_total = $_SESSION["totalGuess"] - count($_SESSION["matches"]);

 if ($miss_total>={
	$color="#ff0000";
	$reset="<br />Reset for new game";
 }else{
	if (count($_SESSION["matches"])==$wordlength){
		foreach($word_array as $key => $l){
			if ($_SESSION["matches"][$key]!=$word_array[$key]){
				$reset="<br />Reset for new game";
				$color="#ff0000";
				$fail= true;					
			}				
		}
		$color="#006600";
		$reset=(!isset($fail) ?	"<br /><span style=\"color:#006600;\">YOU WON</span><br />Reset for new game" : "<br />Reset for new game");

	}else{
		$color="#000000";
	}
}

//display section
$man = "<div style=\"width:100%;\">
	<div style=\"margin:10px auto; width:30%;\">
		<div style=\"float:left; width:25%; text-align:center;\">
			<table border=0 cellpadding=0 cellspacing=0 width=50%>
				<tr>
					<td>".($miss_total>=7 ? "<img src=\"images/man7.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td>
					<td>".($miss_total>=8 ? "<img src=\"images/man8.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td>
				</tr>
				<tr>
					<td>".($miss_total>=5 ? "<img src=\"images/man5.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td>
					<td>".($miss_total>=6 ? "<img src=\"images/man6.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td>
				</tr>
				<tr>
					<td>".($miss_total>=3 ? "<img src=\"images/man3.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td>
					<td>".($miss_total>=4 ? "<img src=\"images/man4.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td>
				</tr>
				<tr>
					<td>".($miss_total>=1 ? "<img src=\"images/man1.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td>
					<td>".($miss_total>=2 ? "<img src=\"images/man2.jpg\" border=\"0\" width=\"15\" height=\"15\" />" : " ")."</td>
				</tr>
			</table>
	</div>
<div style=\"float:left; width:50%; text-align:center;\">";
$display = "<span style=\"color:$color; font-size:28px;\">";
	for($d=0;$d<=($wordlength-1);$d++){
		$display .= (isset($_SESSION["matches"][$d]) ? "{$_SESSION["matches"][$d]}" : "_")." "; 
	}	
$display .= "</span>.$reset";
//$display .= "</div></div>";   
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
	<title>What's that word</title>
</head>
<body>
<?php echo "$man"; ?>
				<form action="game.php" method="post" >
				   <?php echo "$display"; ?><br /><br />
				   <input type="text" name="guessletter" />
				   <input type="submit" name="guess" value= "&#1504;&#1495;&#1513;"/>
				   <br /><br />
				   <input type="submit" name="reset" value="&#1497;&#1510;&#1497;&#1488;&#1492;"/>
				</form>
			</div>
		</div>
	<div style="float:left; width:25%;"></div>
</div>
</body>
</html>

Link to comment
Share on other sites

You already have several sessions counting.

$miss_total = $_SESSION["totalGuess"] - count($_SESSION["matches"]);

So $_SESSION["totalGuess"] is going up with each answer and you would format the code much like I did using the $miss_total variable for showing each image.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.