Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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>

  • #2
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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]

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    SuperMatrix78 (02-08-2013)

  • #7
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you for all your help. I will give it a try.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •