Jump to content

jshc86

New Members
  • Posts

    2
  • Joined

  • Last visited

    Never

Everything posted by jshc86

  1. Thanks JAY for your help. Right after I posted I actually thought that the foreach loop would work. I'm curious as to why array_shift gives unpredictable results though? To clarify, there are multiple versions of Jotto that are played. In my case, the exact placement of the letters is not important. So if the challenge word was "tenet", and my guess was "atlas", it would still count for 1 even though the "t" is not in the correct place. However, if exact placement DID matter, my guess of "atlas" would yield zero because none of the letters are in the correct place.
  2. I have an interesting dilemma. I am writing a script for a game called Jotto. If you haven't heard of it, a challenger selects a five letter word and people try to guess what the word is by choosing other five letter words; at that point, the challenger tells the guesser how many letters their word has in common with the challenger's word. Using logic, you can eventually deduce what the word is by eliminating letters of the alphabet. As an example, say the challenger's word is "class". A typical game would look something like this. Note: I'm including which letters are in common for demonstrative purposes...I wouldn't actually do that if a game is going on. Guessed word[/td]Letters in common slimy2 (l and s) saucy3 (a, c and s) sassy3 (a and s twice) class[td]5 (winner) You'll notice for "sassy", their are three letters in common, two of which come from the double "s". BUT, the extra "s" in "sassy" does not count because the challenge word only contains the letter "s" twice. Anyways, I am writing a script to take a guessed word and compare it to the challenger's word. Here's the original script I tried which DOESN'T work: <?php $guessed_word = "sassy"; $challenge_word = "class"; // Break up each word into its letters $g_array = str_split($guessed_word); $c_array = str_split($challenge_word); // Check for letters in common $common = array_intersect($g_array, $c_array); echo count($common); // Returns 4 and NOT 3 ?> Now I'm aware that switching the order of the arguments in the array_intersect part of my code WILL return a value of 3 like I wanted, but if you fiddle around with it enough, you'll see that the array_intersect function won't do. So, I decided to write a custom function instead. The method behind the function is: break up the two words into their letters, run a while loop for the guessed word while array_shift returns true, check if the letter from array_shift is found in the challenge word array, if it is found remove the letter from the challenge word array, add 1 to my counter variable and repeat. Here's what that looks like: <?php function jots($word1, $word2) { // The guessed word $g_word = str_split($word1); // The challenger's word $c_word = str_split($word2); // Initialize counter $i = 0; if($c_word == $g_word) { echo 6; // This is if the person gets the word correct...6 is my indicator for a winning game } else { while($letter = array_shift($g_word)) { if($key = array_search($letter, $c_word)) { // Remove the found letter from the challenge word array unset($c_word[$key]); $i++; } } echo "The word has {$i} letters in common"; } } ?> Theoretically this should work. However it still gives me problems. See the code below to see what I mean. <?php jots("linus", "linux"); // Returns "The word has 3 letters in common" when really it's 4 jots("light", "right"); // Returns "The word has 4 letters in common", which is correct...but it's similar to the above example where only 1 letter is different. GRRR!! ?> This is pretty frustrating. Does anyone have any ideas?
×
×
  • 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.