thebiggreenie Posted June 25, 2006 Share Posted June 25, 2006 Hi guys,I'm trying to make a letters game, whereby a user is given 9 random letters, and must try to form the longest valid word from those letters.My problem is that I cannot create a code to validate that the letters in the user's guess are actually letters which they were given. If a user uses letters he was not given, or uses letters more than once (if they only appear once), then they haven't given a valid answer.The letters which the user receives are entirely random, and make up one string, so[code]$letters = "AIKNFOLAP";[/code]as opposed to being 9 separate strings.I hope someone can help.- Ralph Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/ Share on other sites More sharing options...
Orio Posted June 25, 2006 Share Posted June 25, 2006 I've got a solution for you, but it's very ugly... there must be a nicer way.Anyway:[code]$letters = strtolower("AIKNFOLAP"); //the random letters$input=strtolower($_POST['answer']); //the string the user submitted$letters_len=strlen($letters);$input_len=strlen($input);if($input_len>$letters_len){die("Error.");}; //some validation, but more validation needs to be done$arr_letters=str_split($letters);$arr_input=str_split($input);$i=0;while($i<$input_len){ if(in_array($arr_input[$i],$arr_letters)){ $key=search_array($arr_input[$i],$arr_letters); $arr_letters[$key]=""; $i++; } else{die("Error");};};[/code]As I said, it's ugly, but should work.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-49460 Share on other sites More sharing options...
Barand Posted June 25, 2006 Share Posted June 25, 2006 (Needs PHP 5)[code]$letters = "AIKNFOLAP";$reply = "apinkfoal";// get lexical roots of both (ie letters in alpha order)$arr1 = str_split($letters);sort($arr1);$arr2 = str_split(strtoupper($reply));sort($arr2);echo ($arr1 == $arr2) ? 'OK' : 'Not same letters';[/code]PHP 4[code]$letters = "AIKNFOLAP";$reply = "opinkfoal";// get lexical roots of both (ie letters in alpha order)for ($i=0,$k=strlen($letters); $i<$k; $i++)$arr1[] = $letters{$i};sort($arr1);for ($i=0,$k=strlen($reply); $i<$k; $i++)$arr2[] = strtoupper($reply{$i});sort($arr2);echo ($arr1 == $arr2) ? 'OK' : 'Not same letters';[/code]EDIT: and add[code]$arr3 = array_diff($arr2, $arr1);echo join(', ', $arr3), ' were not in the original set';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-49489 Share on other sites More sharing options...
thebiggreenie Posted June 26, 2006 Author Share Posted June 26, 2006 Thanks Barand, but the idea is that users don't have to guess a nine-letter word, instead, they can have words of any length from 2-9 letters.- Ralph Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-49867 Share on other sites More sharing options...
Orio Posted June 27, 2006 Share Posted June 27, 2006 Mine works in that case.See my post before Barand.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-49992 Share on other sites More sharing options...
thebiggreenie Posted June 27, 2006 Author Share Posted June 27, 2006 Will do Orio. Where do I put the 'correct' and 'incorrect' bits?- Ralph Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-50065 Share on other sites More sharing options...
Orio Posted June 27, 2006 Share Posted June 27, 2006 The script will stop and echo "Error" if there was a problem. That means that you can continue your script right after the part I added, because it'll only continue to there if there was no error.Just be sure you add some validation before my part :) Like- input isnt empty, only letters were submitted etc'.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-50067 Share on other sites More sharing options...
thebiggreenie Posted June 27, 2006 Author Share Posted June 27, 2006 Ok.. what if I'm not running PHP5 :S- Ralph Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-50084 Share on other sites More sharing options...
Orio Posted June 27, 2006 Share Posted June 27, 2006 Add this at the top/bottom:[code]if(!function_exists('str_split')){ function str_split($string,$split_length=1){ $count = strlen($string); if($split_length < 1){ return false; } elseif($split_length > $count){ return array($string); } else { $num = (int)ceil($count/$split_length); $ret = array(); for($i=0;$i<$num;$i++){ $ret[] = substr($string,$i*$split_length,$split_length); } return $ret; } } };[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-50096 Share on other sites More sharing options...
thebiggreenie Posted June 27, 2006 Author Share Posted June 27, 2006 search_array() is giving me problems... is it PHP5? Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-50115 Share on other sites More sharing options...
Orio Posted June 27, 2006 Share Posted June 27, 2006 Woops! Change search_array to in_array :)Orio. Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-50126 Share on other sites More sharing options...
thebiggreenie Posted June 28, 2006 Author Share Posted June 28, 2006 Now one of the WHILEs is repeating in the script, when the guess uses correct letters Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-50411 Share on other sites More sharing options...
Orio Posted June 28, 2006 Share Posted June 28, 2006 Final script:[code]$letters = strtolower("AIKNFOLAP"); //the random letters$input=strtolower($_POST['answer']); //the string the user submitted$letters_len=strlen($letters);$input_len=strlen($input);if($input_len>$letters_len){die("Error.");}; //some validation, but more validation needs to be done$arr_letters=str_split($letters);$arr_input=str_split($input);$i=0;while($i<$input_len){if(in_array($arr_input[$i],$arr_letters)){ $key=array_search($arr_input[$i],$arr_letters); $arr_letters[$key]=""; $i++; }else{die("Error");};};[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-50467 Share on other sites More sharing options...
Barand Posted June 28, 2006 Share Posted June 28, 2006 This should work with PHP 4 or 5[code]$letters = "AIKNFOLAP";for ($i=0,$k=strlen($letters); $i<$k; $i++) { $arr1[] = $letters{$i};}$test = array('apinkfoal', 'opinkfoal', 'apinkfool', 'apankfoal', 'knife', 'polka', 'plain', 'koala', 'painful');echo '<pre>';printf('| %-12s | %s |%3s | %-12s | %-12s |%s', 'Reply','Size','OK','Unused','Illegal',"\n\n");foreach ($test as $reply) { $tmp = $arr1; $arr3 = array(); for ($i=0,$k=strlen($reply); $i<$k; $i++) { $c = strtoupper($reply[$i]); if (($p = array_search($c,$tmp))!== false) unset($tmp[$p]); else $arr3[] = $c; } printf('| %-12s | %3d |%3s | %-12s | %-12s |%s', $reply, $k, count($arr3) ? '':'OK', join('',$tmp), join('', $arr3),"\n");}echo '</pre>';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-50554 Share on other sites More sharing options...
thebiggreenie Posted June 28, 2006 Author Share Posted June 28, 2006 Ok, I've got a code which checks a webpage with the word and validates it, but I get a 500 error code during fopen... Quote Link to comment https://forums.phpfreaks.com/topic/12881-letters-game-help/#findComment-50561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.