Jump to content


Photo

Letters Game Help


  • Please log in to reply
14 replies to this topic

#1 thebiggreenie

thebiggreenie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 25 June 2006 - 08:52 PM

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

$letters = "AIKNFOLAP";

as opposed to being 9 separate strings.

I hope someone can help.

- Ralph

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 25 June 2006 - 09:32 PM

I've got a solution for you, but it's very ugly... there must be a nicer way.
Anyway:
$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");};
};

As I said, it's ugly, but should work.

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 25 June 2006 - 11:22 PM

(Needs PHP 5)

$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';

PHP 4
$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';

EDIT: and add
$arr3 = array_diff($arr2, $arr1);
echo join(', ', $arr3), ' were not in the original set';


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 thebiggreenie

thebiggreenie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 26 June 2006 - 11:48 PM

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

#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 27 June 2006 - 08:17 AM

Mine works in that case.
See my post before Barand.

Orio.
Think you're smarty?

(Gone until 20 to November)

#6 thebiggreenie

thebiggreenie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 June 2006 - 12:50 PM

Will do Orio. Where do I put the 'correct' and 'incorrect' bits?

- Ralph

#7 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 27 June 2006 - 12:56 PM

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.
Think you're smarty?

(Gone until 20 to November)

#8 thebiggreenie

thebiggreenie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 June 2006 - 01:49 PM

Ok.. what if I'm not running PHP5 :S

- Ralph

#9 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 27 June 2006 - 02:21 PM

Add this at the top/bottom:

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; 
       }      
   }  
};


Orio.
Think you're smarty?

(Gone until 20 to November)

#10 thebiggreenie

thebiggreenie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 June 2006 - 03:11 PM

search_array() is giving me problems... is it PHP5?

#11 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 27 June 2006 - 04:10 PM

Woops! Change search_array to in_array :)

Orio.
Think you're smarty?

(Gone until 20 to November)

#12 thebiggreenie

thebiggreenie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 28 June 2006 - 02:22 PM

Now one of the WHILEs is repeating in the script, when the guess uses correct letters

#13 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 28 June 2006 - 04:31 PM

Final script:

$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");};
};

Think you're smarty?

(Gone until 20 to November)

#14 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 28 June 2006 - 07:52 PM

This should work with PHP 4 or 5
$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>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#15 thebiggreenie

thebiggreenie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 28 June 2006 - 07:58 PM

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...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users