[Show all top banners]
Back to: Kurakani General Refresh page to view new replies
 Need help with php code...any insight..
[VIEWED 3275 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
TPS Re-registration
What are your first memories of when Nepal Television Began?
TPS Re-registration case still pending ..
निगुरो थाहा छ ??
ChatSansar.com Naya Nepal Chat
Basnet or Basnyat ??
Sajha has turned into MAGATs nest
NRN card pros and cons?
मन भित्र को पत्रै पत्र!
Will MAGA really start shooting people?
Democrats are so sure Trump will win
Top 10 Anti-vaxxers Who Got Owned by COVID
TPS Work Permit/How long your took?
काेराेना सङ्क्रमणबाट बच्न Immunity बढाउन के के खाने ?How to increase immunity against COVID - 19?
Breathe in. Breathe out.
3 most corrupt politicians in the world
Dementia Joe has been selected to become the next President
Trumpism project 2025 everyone should read https://www.project2025.org/
आज बाट तिहारको सेल सकियो
"श्रीमतिको अर्कै संग सम्बन्ध रहेछ "
Nas and The Bokas: Coming to a Night Club near you
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