[Show all top banners]
Back to: Kurakani General Refresh page to view new replies
 Need help with php code...any insight..
[VIEWED 3825 TIMES]
SAVE! for ease of future access.
Posted on 08-12-10 11:49 AM     Reply [Subscribe]
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();


}





?>

 
Posted on 08-16-10 9:51 AM     [Snapshot: 77]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

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.


 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 200 days
Recommended Popular Threads Controvertial Threads
ChatSansar.com Naya Nepal Chat
TPS Re-registration case still pending ..
Toilet paper or water?
Anybody gotten the TPS EAD extension alert notice (i797) thing? online or via post?
ढ्याउ गर्दा दसैँको खसी गनाउच
Sajha Poll: Who is your favorite Nepali actress?
Problems of Nepalese students in US
TPS EAD auto extended to June 2025 or just TPS?
whats wrong living with your parents ?
Mamta kafle bhatt is still missing
Ajay Kumar Dev sentenced to 378 yrs
Why Americans reverse park?
Now Trump is a convicted criminal .
Tourist Visa - Seeking Suggestions and Guidance
TPS advance parole Travel document i-131, Class of Admission ?
Nepali Passport Renewal
Biden said he will issue new Employment visa for someone with college degree and job offers
To Sajha admin
TPS Renewal Reregistration
मैले नबुझेका केहि गीत का lyrics हरु
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters