Jump to content

Archived

This topic is now archived and is closed to further replies.

thebiggreenie

Letters Game Help

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
(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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Mine works in that case.
See my post before Barand.

Orio.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Woops! Change search_array to in_array :)

Orio.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.