[Show all top banners]
Back to: Kurakani General Refresh page to view new replies
 Need help with php code...any insight..
[VIEWED 3548 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 case still pending ..
What are your first memories of when Nepal Television Began?
निगुरो थाहा छ ??
ChatSansar.com Naya Nepal Chat
Basnet or Basnyat ??
TPS EAD auto extended to June 2025 or just TPS?
Biden out, Trump next president, so what’s gonna happen to TPS, termination?
मन भित्र को पत्रै पत्र!
Anybody gotten the TPS EAD extension alert notice (i797) thing? online or via post?
TPS Work Permit/How long your took?
काेराेना सङ्क्रमणबाट बच्न Immunity बढाउन के के खाने ?How to increase immunity against COVID - 19?
TPS Renewal Reregistration
अमेरिकामा बस्ने प्राय जस्तो नेपालीहरु सबै मध्यम बर्गीय अथवा माथि (higher than middle class)
Guess how many vaccines a one year old baby is given
चितवनको होस्टलमा १३ वर्षीया शालिन पोखरेल झुण्डिएको अवस्था - बलात्कार पछि हत्याको शंका - होस्टेलहरु असुरक्षित
Sajha Poll: Who is your favorite Nepali actress?
Biden said he will issue new Employment visa for someone with college degree and job offers
Why Americans reverse park?
Nepali Passport Renew
Driver license help ASAP sathiharu
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