[Show all top banners]

fifaWC2010
Replies to this thread:

More by fifaWC2010
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Need help with php code...any insight..
[VIEWED 3206 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 30 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
nrn citizenship
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
Travelling to Nepal - TPS AP- PASSPORT
emergency donation needed
ढ्याउ गर्दा दसैँको खसी गनाउच
मन भित्र को पत्रै पत्र!
Morning dharahara
Guess how many vaccines a one year old baby is given
जाडो, बा र म……
lost $3500 on penny stocks !!!
Susta Susta Degree Maile REMIXED version
Elderly parents travelling to US (any suggestions besides Special Assistance)?
TPS Reregistration and EAD Approval Timeline.......
Changing job after i-140 approval
कल्लाई मुर्ख भन्या ?
1974 AD Pinjadako Suga Remixed
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