Posted by: fifaWC2010 August 12, 2010
Need help with php code...any insight..
Login in to Rate this Post:     0       ?        
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();


}





?>
Read Full Discussion Thread for this article