Guessing Game

Keith1985
Keith1985 used Ask the Experts™
on
I'm new to programming and I'm starting out with PHP. I'm messing around with some of PHP's methods and decision structures to create a guessing game. The guessing game is to guess last years winner of the best motion picture. The user inputs their guess into a textbox and for every letter they guess right, the letter is printed. However, for every letter that is wrong is replaced with an "*". I can't seem to get this to work and asking for a little help.
<?php
                if(isset($_GET['txtGuess']) && $_GET['txtGuess'] != "")
		{
                    // Set the answer to the guessing game.
                    $strAnswer = "The King's Speech";
                    $strGuess = $_GET['txtGuess'];
                    $arrAnswer = str_split($strAnswer);
                    $arrGuess = str_split($strGuess);
                    $Temp;
						
                    $arrIntersect = array_intersect($arrAnswer, $arrGuess);
                    $arrIntersect = array_values($arrIntersect);
                    
                    $arrDifference = array_diff($arrAnswer, $arrGuess);
                    $arrDifference = array_values($arrDifference);
                    
                    
                    for($i = 0; $i < count($arrAnswer); $i++)
                    {
                        for($j = 0; $j < count($arrDifference); $j++)
                        {
                            if($arrAnswer[$i] == $arrDifference[$j])
                            {
                                echo substr_replace($arrDifference[$j], "*", $arrDifference[$j]);
                            }
                            else
                            {
                                echo $arrDifference[$j];
                            }
                            break;
                        }
                    }
                }
                
		?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi Keith1985,

I think this is wrong:
   else
   {
      echo $arrDifference[$j];
   }

Open in new window

As I understood your question this should be
   else
   {
      echo $arrAnswer[$i];
   }

Open in new window

instead.

If this doesn't solve the problem please explain 'I can't seem to get this to work' more detailed ...

ZOPPO
Sorry, what I posted before is wrong - I guess got a bit confused by the way your code works - I re-read your question and think you can do it easier - just replace both 'for' loops with this:
     foreach($arrAnswer as $c)
     {
	 	if ( array_search( $c, $arrDifference ) )
		{
			echo "*";
		}
		else
		{
			echo $c;
		}
     }

Open in new window

ZOPPO

Author

Commented:
Zoppo, this worked. I guess I never considered the array_search function. I didn't think it was possible to get this in a single iteration structure.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial