Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2013, 02:13 PM   PM User | #1
SuperMatrix78
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
SuperMatrix78 is an unknown quantity at this point
String manipulation

I really hope someone can point me in the right direction. I am in the 5th week of my PHP/MySQL class at school and this weeks assignment is to have us create a game that a user can guess the title of a movie. There is only one answer so I don't have to deal with an array or anything like that. My question is what sting function(s) do I use if I want to compare a letter a user enters to letters that are in the answer. Then which string function(s) do I use to display the correct letters guessed with the remaining letters shown as asterisks. I don't want anyone to write the code for me I am just trying to be pointed down the right path. I am going to include what I have already written. I would appreciate any advice or guidance. Thank you.
Code:
<?php
if(isset($_POST['submit'])) {
$triviaAnswer = $_POST['triviaAnswer'];
$playersGuess1 = $_POST['playersGuess1'];
}
$GameAnswer = "The Artist";
$NewGameAnswer = substr_replace($GameAnswer, "*** ******",0);
?>
 <h1><img src="Images/movie_reel.jpg" alt="" width="150" height="150" />&nbsp;&nbsp;The Movie Hut&nbsp;&nbsp;<img src="Images/movie_reel.jpg" alt="" width="150" height="150" /></h1>
 <h2>Guess the Oscar Movie Game</h2><hr />
 <h3>Guess the name of the movie responsible for winning best picture last year.<br />
  Enter the letter and click the check button to see if you guessed right.<br />
  The letters you guess correctly will appear at the bottom of the page<br />
  while the remaining letters will be starred out</h3><br />
  <form action="" method="post">
 <p>Enter a letter:&nbsp;<input name="playersGuess" type="text" maxlength="1" />
 <br /><br />
 <input name="submit" type="submit" value="Check" />
 <br />
<input name="triviaAnswer" type="hidden" value="<?php echo $GameAnswer ?>" />
<input name="playersGuess1" type="hidden" value="" />
 </form>
SuperMatrix78 is offline   Reply With Quote
Old 02-08-2013, 02:56 PM   PM User | #2
SuperMatrix78
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
SuperMatrix78 is an unknown quantity at this point
I posted my code wrong here is what I have so far (at least the important stuff).
Code:
PHP Code:
<?php if(isset($_POST['submit'])) { $triviaAnswer $_POST['triviaAnswer']; $playersGuess $_POST['playersGuess']; } $GameAnswer "The Artist"; $NewGameAnswer substr_replace($GameAnswer"*** ******",0); ?>
[HTML] <p>Enter a letter:&nbsp;<input name="playersGuess" type="text" maxlength="1" /> <br /><br /> <input name="submit" type="submit" value="Check" /> <br /> <input name="triviaAnswer" type="hidden" value="<?php echo $GameAnswer ?>" /> <input name="displayAnswer" type="hidden" value="<?php echo $NewGameAnswer?>" /> </form> [/HTML]
SuperMatrix78 is offline   Reply With Quote
Old 02-08-2013, 03:24 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Oops, something didn't work out there for the code blocks.
So, like hangman, but no limit?
First, having the $GameAnswer passed through the form itself kind of defeats the purpose. Since it's harcoded in script anyway, I'd suggest leaving it as that and not providing it to the user. Just compare what you have been given so far.
Which leads to the question of tracking the user entries so far. HTTP itself is stateless, so we need to persist the data in another fashion. Options are either to use sessions, or to pass them through the form.
Have you covered sessions? Have you covered arrays?
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-08-2013, 04:45 PM   PM User | #4
SuperMatrix78
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
SuperMatrix78 is an unknown quantity at this point
I have covered arrays and that was what I was thinking about using in order to display the answer with the correct letter and asterisks after each guess. I don't understand how you compare what the user types in to the letters in the answer. One of the stipulations of the assignment is to display after each attempt so the user can see the remaining letters.
SuperMatrix78 is offline   Reply With Quote
Old 02-08-2013, 04:48 PM   PM User | #5
SuperMatrix78
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
SuperMatrix78 is an unknown quantity at this point
The assignment tells me to store the users guess in a hidden field so I think that's why I had the hidden input fields in the form but I didn't name them properly.
SuperMatrix78 is offline   Reply With Quote
Old 02-08-2013, 05:06 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
There are a few different ways to check the selected items to the given string. Given a learning process, I'd suggest iteratively and using an array to contain the answers already given by the user.
You can iterate a string as it is simply an char array. It is mutable in PHP as well, so you can modify it iteratively.
PHP Code:
$sWord 'This is a word.';
$iLen strlen($sWord);
for (
$i 0$i $iLen; ++$i)
{
    
printf("Char at %d is %s" PHP_EOL$i$sWord[$i]);

Now during iteration, you can check the char against an array of user selected chars. If $sWord[$i] is in the selected array of chars, then show that letter. Otherwise, show *. You may want to push the space char as well otherwise you won't be able to see the spaces.
There are several ways to see if a value is in an array. Iterative is one, but there are functions for this as well. See here for a list of all the array functions: http://www.php.ca/manual/en/book.array.php
Finally, you need to figure out a way to persist previously selected chars. If you know sessions, that's a simple matter of creating an array within the sessions that has these. If not, you can pass an array through a hidden field via serialize() and unserialize() to read and write them. Then you simply add the newly provided char to the array of chars passed, and when they are presented with the form again you can provide it with the array serialized again. This way you get to always keep it in an array.

Add some simple logic beyond there to indicate that all letters were chosen, and even to indicate when a letter has already been chosen.

Hopefully that gives you some hints where to go. Like I said, there are several other options as well for you. str_[i]replace does accept arrays, but you'll need to reverse the logic in order to set *'s where there is no corresponding match as opposed to what is set. range() can be used to populate a list of chars a-z for example, and array_diff functions can be used to pull things out. This is actually a better solution overall, but doesn't have any looping involved, so I guess that depends on if you are doing an assignment for something like looping.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
SuperMatrix78 (02-08-2013)
Old 02-08-2013, 05:23 PM   PM User | #7
SuperMatrix78
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
SuperMatrix78 is an unknown quantity at this point
Thank you for all your help. I will give it a try.
SuperMatrix78 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:47 AM.


Advertisement
Log in to turn off these ads.