Need help with php code...any insight..

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

Hi, I am a student learning PHP and I am totally stuck. I know this should be simple but I cannot seem to get it to work, it is supposed to be a simple word guess game. Guess the letters and they should fill in as you get them correct. Sort of like hangman but without the images and it only needs to be one word. I need the unguessed letters to stored in a hidden field. Here is what I have so far. Any help or input would be appreciated. <?php $word = "Harry Potter"; if ($guessLength < strlen($word)){ $message = "Guess Again"; $match = false; $guess = $_POST['guess'] [0]; $position = array(); $display = array(); while (count($display) < strlen($word)){ $display[] = '_'; } if (in_array(strtolower($guess), $alreadyGuessed)){ $message = "You already guessed'".$guess."'!"; $isGuessed = true; } if (!$isGuessed && $guess != null){ $alreadyGuessed[] = strtolower($guess); for ($h=0; $h < strlen($word); $h++) { if (strcasecmp(substr($word, $h, 1), $guess) ==0){ $match=true; $position[] = $h; } } if ($match == true){ foreach ($position as $k => $position){ if(strcmp($display[$position], '_') ==0){ $display[$position] = $guess; $guessLength++; if ($guessLength == strlen($word)){ $message = "You are Correct!"; } } } } }}}} $_SESSION['word'] = $word; $_SESSION['display'] = $display; $_SESSION['guessLength'] = $guessLength; $_SESSION['alreadyGuessed'] = $alreadyGuessed; foreach ($display as $l => $letter){ echo $letter.'_'; } echo '.$message.'; if ($_POST['restart'] === "Restart"){ session_destroy(); } ?>

fifaWC2010 · Aug 12, 2010 11:49 AM · 9,842 views

1 Reply

I think you need to modify all of your codes. It's a mess. The thing is simple but you are making it hard OK fallow what i say... Use: str_split("$word");   - This will split each letter of the words into arrays now after tha,t split each letter of the word into arrays and make one to one letter comparisons. It is better to check if a letter exist in a word with a built in function: substr_count($word,$letterEntered); if it exists and the position matches: return value=true; else false and, to store the letter that have been entered previously, it is better to use cookies instead of Sessions. To check whether the letter had been entered before, it is easy to call cookie and then check the values stored in it.

raju161 · Aug 16, 2010 9:51 AM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all