Jump to content

Recommended Posts

Can someone point me to a layman's tutorial for a simple hangman game?

 

I downloaded one off the internet, but it doesn't do what I want. The one I use doesn't allow capitalized letters or spaces! I can live without the capitlization but MOST of the words will have spaces and therefore, this code will not work.

 

I want to start a brand new code from scratch but I can't seem to figure it out. I am new to coding and have been coding my site using MySQL, and that's fine, but ALL of the coding for hangman is very confusing for me!

 

 

$letters = array('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', ' ');

	if(empty($_POST)) 
	{

	$rightcount = '0';
	$wrongcount = '0';
	$words = explode("\n", file_get_contents('words.list.txt'));
    
	$right = array_fill_keys($letters, ' _ ');
    
	$wrong = array();

	shuffle($words);
    
	$word = strtolower($words[0]);

	$rightstr = serialize($right);
    
	$wrongstr = serialize($wrong);
    
	$wordletters = str_split($word);
    
	$show = '';
    
		foreach($wordletters as $letter) 
		{
        
		$show .= $right[$letter];
    
		}

	} 
		else 
		{

		$show = $_POST['show'];
		$word = $_POST['word'];

		$lettercount = strlen($word);
		echo $lettercount . "<br>";
		$word = htmlspecialchars($word);
		$word = mysql_real_escape_string($word);
		$rightcount = $_POST['rightcount'];
    
		$wrongcount = $_POST['wrongcount'];

		$guess = strtolower($_POST['guess']);

		$guess = ctype_alpha($guess);
		if($guess == TRUE)
		{
		$guess = strtolower($_POST['guess']);
		}
			else
			{
			echo "<b>Please use only letters.</b><br>";
			$guess = '';
			}

		$letterinstance = @substr_count($word, $guess);

		$right = unserialize($_POST['rightstr']);

		$wrong = unserialize($_POST['wrongstr']);
    
		$wordletters = str_split($word);


			if(!(empty($guess)))
			{
				if(stristr($word, $guess))
				{
        
				$show = '';
        
				$right[$guess] = $guess;

				$wordletters = str_split($word);

				$rightcount = $rightcount + (1*$letterinstance);
					foreach($wordletters as $letter) 
					{
            
					$show .= $right[$letter];
					}

				} 
					else 
					{
        
					$show = '';
        
					$wrong[$guess] = $guess;

					$wrongcount = $wrongcount + 1;
						if(count($wrong) == 6) 
						{
           
							$show = $word;

						echo "You lost this round of hangman.<br>";
						} 
							else 
							{
           
								foreach($wordletters as $letter) 
							{
                
					$show .= $right[$letter];
					}
        
			}	
		}

}
    
$rightstr = serialize($right);
    
$wrongstr = serialize($wrong);

echo "Right: $rightcount<br>Wrong: $wrongcount<br>";

	if($rightcount == $lettercount)
	{
	echo "You win!<br>";
	}


}
echo "Incorrect Guesses: ";
echo implode(', ', $wrong);
echo "<br>";
echo $show;
echo "<br />";
echo "<form method='post'>
<input name='guess'>";
echo "<input type='hidden' name='show' value='$show'>
";
echo "<input type='hidden' name='rightcount' value='$rightcount'>
";
echo "<input type='hidden' name='wrongcount' value='$wrongcount'>
";
echo "<input type='hidden' name='word' value='$word'>
";
echo "<input type='hidden' name='rightstr' value='$rightstr'>";
echo "<input type='hidden' name='wrongstr' value='$wrongstr'>";
echo "<input type='submit' value='guess'>
";
echo "</form>";
echo "<a href='hangman.php'>Reset</a>";

 

 

That is the code I am using now. The connect to the database and rewarding the user when they guess all the letters work fine, but I need for my other details to be in there and I have NO clue how to do that. I've tried a lot of things but everything just screws up.

 

Here's the code I have now that is from scratch, and it's very basic. I really have no freakin' clue where to go from there.

 

<?php

$words = explode(";", file_get_contents('words.list.txt'));

shuffle($words);
    
$word = $words[0];



$upletters = array('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');

$lowletters = array('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');

echo $word . "<br>";

$word1 = str_replace($upletters, ' _ ', $word);
$word1 = str_replace($lowletters, ' _ ', $word1);

echo $word1 . "<br><br>";


echo "<form action = '1.php' method = 'POST'>";
echo "<input type = 'text' name = 'guess' size = '10'>";
echo "<input type = 'hidden' name = 'word' value = '$word'>";
echo "<input type = 'hidden' name = 'word1' value = '$word1'>";
echo "<br><input type = 'submit' value = 'Guess!'></form>";
?>

 

 

Can someone PLEASE point me in the right direction! Or give me all the functions I need to use to get through this!

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/253716-wow-i-need-help-with-a-simple-game/
Share on other sites

I think if you knowledge of PHP is limited then I wouldn't dive so far in.

 

With PHP there are some basic things you need to take into account such as character matching so you could take a look at strcmp which does match case sensative letters.

 

Instead of making two arrays you could just make one and use strtolower or strtoupper

 

I'm not sure what you mean by "other details" so you will have to elaborate.

The frustrating thing for me is that I have coded the rest of my site just fine, using MySQL queries. I have a forum on there, messaging, friending, (it's a neopets type website) so I have shops, a number guessing game, pet and user profiles and everything else, and that's all using mysql queries..

 

but for some reason, when it comes to this, I can't seem to wrap my mind around it..?

 

Maybe my thought process is a little tainted from the downloaded code and I'm trying to figure that one out too much rather than starting from a true fresh start.. I know what needs to happen with all the little details, just not sure of all the functions I need to do as such..

 

By other details, I literally mean just the capitalization and spacing, which is a very huge issue with the downloaded code.. not sure why that guy didn't code the game allowing the site to use multiple words, at LEAST. :-/

 

 

Probably because 1 page has multiple states now across multiple requests which is otherwise not the case.

 

if (isset($_GET['new_game'])) {
    // user wants to start a new game
    // store info in sessions
    header('Location: /');
} else if (isset($_GET['guess'])) {
    // user has guessed a letter or word
    // store info in sessions
    header('Location: /');
} else if (isset($_SESSION['game_over'])) {
    if ($_SESSION['game_over']) {
        // game over, try again?
        // clear session data
    } else {
        // display game progress
    }
} else {
    // display main menu
}

I found a simple way to do the game, BUT, I can't seem to get an array to work..

 

What happens is that I delete letters form the array instead of going through all the fancy crap..

 

This is what I have so far:

 

<?php

if(!(isset($_POST['set'])))
{
$words = explode(";", file_get_contents('words.list.txt'));

shuffle($words);
    
$word = $words[0];


$arraya = '';
$arrayb = '';

$lowletters = array('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');

$upletters = array('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');

$arraya = ".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.";
$arrayb = ".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.";

}
else if(!(empty($_POST['guess'])))
{
$word = $_POST['word'];

$arraya = $_POST['arraya'];
$arrayb = $_POST['arrayb'];

$guessa = strtolower($_POST['guess']);
$guessb = strtoupper($_POST['guess']);

$arraya = str_replace('.', ''', $arraya);
$arrayb = str_replace('.', '\'', $arrayb);

$guessa = ", $guessa, ";
$guessb = ", $guessb, ";

$arraya = str_replace($guessa, '', $arraya);
$arrayb = str_replace($guessb, '', $arrayb);

$lowletters = array($arraya);
$upletters = array($arrayb);

echo $arraya . "<br>" . $arrayb . "<br>";
echo $guessa . "<br>" . $guessb;
echo "<br>$lowletters<br>$upletters<br>";
}
foreach($lowletters as $stuff)
{
echo $stuff . "<br>";
}
foreach($upletters as $stuff)
{
echo $stuff . "<br>";
}

$broken = explode(' ', $word);
$part1 = $broken[0];
$part2 = @$broken[1];
$part3 = @$broken[2];
$part4 = @$broken[3];

$word1 = str_replace($lowletters, ' _ ', $part1);
$word1 = str_replace($upletters, ' _ ', $word1);

$word2 = str_replace($lowletters, ' _ ', $part2);
$word2 = str_replace($upletters, ' _ ', $word2);

$word3 = str_replace($lowletters, ' _ ', $part3);
$word3 = str_replace($upletters, ' _ ', $word3);

$word4 = str_replace($lowletters, ' _ ', $part4);
$word4 = str_replace($upletters, ' _ ', $word4);

echo $word1 . " " .  $word2 . " " .  $word3 . " " .  $word4;

echo "<form action = '1.php' method = 'POST'>";
echo "<input type = 'text' name = 'guess' size = '5'>";
echo "<input type = 'hidden' name = 'word' value = '$word'>";
echo "<input type = 'hidden' name = 'arraya' value = '$arraya'>";
echo "<input type = 'hidden' name = 'arrayb' value = '$arrayb'>";
echo "<input type = 'hidden' name = 'set' value = 'TRUE'>";
echo "<br><input type = 'submit' value = 'Guess!'></form>";
?>

 

 

But for some reason, the edited 'array' doesn't work.. the entire string IS the array rather than the pieces within the string are parts of the array.. how do i fix that?

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.