Jump to content

joel24

Members
  • Posts

    760
  • Joined

  • Last visited

Everything posted by joel24

  1. ahh sorry, as the substr counts the first char as 0, it needs to go from 0-3 Also, my loop was removing the $i position character rather than a random one... I've put in a method which will return a random number which has not been removed from the string as yet. I've done some tests and it's working - let me know if you've any queries about the code function getRandomCharToRemove(array $alreadyRemoved, $strLength) { $random = rand(0,$strLength-1); //substr starts from char 0 so we need to subtract 1 from strlen return (!isset($alreadyRemoved[$random])) ? $random : getRandomCharToRemove($alreadyRemoved,$strLength); } $maxCharsToRemove = 2; $word = 'bees'; $strLength = strlen($word); $charsGettingRemoved = rand(1,$maxCharsToRemove+1); $charsRemoved=array(); for ($i=0;$i<$charsGettingRemoved;$i++){ $randomCharToRemove = getRandomCharToRemove($charsRemoved,$strLength); $charsRemoved[$randomCharToRemove]=substr($word,$randomCharToRemove,1); $word = substr_replace($word,'*',$randomCharToRemove,1); } echo "Your word is: $word<br/>The letters missing are: " . print_r($charsRemoved,1)."<br/>Len: $strLength<br/>Chars removed [position => letter]: $charsGettingRemoved";
  2. session_start(); $_SESSION['currentWord'] = 'bees'; $word = $_SESSION['currentWord']; $length = strlen($word); $maxCharsToRemove = 2; //you could swap this for a algorithm based on the strlen (i.e. ceil($length/3)) -- lets round up incase the division is less than 0 $charsGettingRemoved = rand(1,$maxCharsToRemove); $charsRemoved=array(); for ($i=1;$i<=$charsGettingRemoved;$i++){ $charsRemoved[$i]=substr($word,$i,1); $word = substr_replace($word,'*',$i,1); } echo "Your word is: $word<br/>The letters missing are: " . print_r($charsRemoved,1)."<br/>Len: $length<br/>Chars: $charsGettingRemoved"; //and when teh users posts the form, concatenate all of the posted characters and see if it equals to $_SESSION['currentWord'] and then for the html <input type="text" name="question_1_letters[]" class="inputs" maxlength="1" value="" /> <input type="text" name="question_1_letters[]" class="inputs" maxlength="1" value="" /> <input type="text" name="question_1_letters[]" class="inputs" maxlength="1" value="" /> <input type="text" name="question_1_letters[]" class="inputs" maxlength="1" value="" /> the above html will be accessible in an array of $_POST['question_1_letters'] and will automatically receive indexes... so you can implode and see that it equals to the question 1 word if (implode('',$_POST['question_1_letters'])==$_SESSION['currentWord']) //they got it right else //they got it wrong apologies for rewriting your code, i started out fixing yours though thought it easier to understand written this way... and easier to scale
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.