![]() |
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 |
I posted my code wrong here is what I have so far (at least the important stuff).
Code:
|
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? |
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.
|
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.
|
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:
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. |
Thank you for all your help. I will give it a try.
|
| All times are GMT +1. The time now is 06:59 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.