How do I search for “clean tiles” in a scrabble app? (Php)

I created this application a couple of months ago: http://www.mondofacto.com/word-tools/scrabble-solver.html

The application allows the user to enter a set of letters that they specify, and returns an echo of which real words they can use, as well as what score they will receive to use these letters.

Basically, I want to expand the application so that users can enter "empty tiles" - one that can be qualified as any of the 26 letters of the alphabet, as well as repeat words that are valid.

Below is a screenshot of the database structure.

http://i37.tinypic.com/28v6a8h.png 

You may need to copy this ^ file to your browser.

The query executed by this data when the user enters, for example, "aardvark", is as follows:

 SELECT * FROM scrabble WHERE a <= 3 AND b <= 0 AND c <= 0 AND d <= 1 AND e <= 0 AND f <= 0 AND g <= 0 AND h <= 0 AND i <= 0 AND j <= 0 AND k <= 1 AND l <= 0 AND m <= 0 AND n <= 0 AND o <= 0 AND p <= 0 AND q <= 0 AND r <= 2 AND s <= 0 AND t <= 0 AND u <= 0 AND v <= 1 AND w <= 0 AND x <= 0 AND y <= 0 AND z <= 0 AND length <= 8 ORDER BY scrabble DESC 

If you want to see the results, enter the word in the link that I posted at the top.

Right

So does anyone know how to do this? I started with the following code, which adds each character of the alphabet to the end of a line entered by the user if they put spaces (spaces are empty tiles).

  if (preg_match('/[\s]/', $string)) { $wild_string = $string; foreach (range('a','z') as $i) { $wild_string = $string; $wild_string .= $i; } 

The $ wild_string variable is such that each letter is added to the on loop. Dropping it to the start line in each loop, it stops the code from adding all 26 letters to the entered line.

I hope someone can help and sorry if I waffle :)

Andy.

+4
source share
4 answers

My suggestion is as follows:

Suppose user input is: ab* where * is a wildcard. Count all the known letters and wildcards and create an array where element 0 is the number of wildcards, and each other element key is a known letter with a value, the number of which is specified in user input:

 function GetArrayLetters($userInput) { ... // produces something like $letters = ( 0 => 1, 'a' => 1, 'b' => 1); return $letters; } 

Using this array, modify your query by adding the number of wildcards to each letter and word length:

 # with only one wildcard, the query will become: SELECT * FROM scrabble WHERE a <= 2 AND b <= 2 AND c <= 1 .... ... AND length <= 3 

Now put the results somewhere (array) and examine each word in turn. Go through each letter and subtract it for each known letter from your array of letters $; If the value of the known letter is zero, subtract 0 from the element instead of (wildcard). If you get a negative value for a wildcard, drop the word:

 foreach ($result_set AS $word) { $letters = GetArrayLetters($userInput); for ($i = 0; $i < str_len($word); $i++) { $letter = substr($word, $i, 1); if ( array_key_exists( $letter, $letters )) { if ($letters[$letter]) > 0 { $letters[$letter] -= 1; } else { $letters[0] -= 1; // else subtract from the wildcard } } else { $letters[0] -= 1; } if ($letters[0] < 0) { // if wildcard falls bellow zero, discard the word } } } 
+1
source

Thanks for inspiring me to create my own vocabulary! http://www.wireless-trend.com/wordfinder_index_eng.php

To search for empty tiles, I did the following: Count the number of spaces needed to create a word in the database. Limit this in the request to the number of spaces entered by the user.

You can use an unlimited number of spaces.

"length" is the length of each word in my database.
"a" is the number a in a word in my database.
$word_a is the number a in the word entered by the user.
Is $number_of_blanks a number? in the word entered by the user.

 SELECT * FROM $table WHERE length <= $length_of_word_searched_by_user AND ( (greatest(a,$word_a)-$word_a)+ (greatest(b,$word_b)-$word_b)+ (greatest(c,$word_c)-$word_c)+ ..<br> ..<br> (greatest(y,$word_y)-$word_y)+ (greatest(z,$word_z)-$word_z) ) <= $number_of_blanks 
+1
source

The only method I can think of is to run your query above 26 times, each time adding 1 to another letter of the alphabet, as well as 1 to the length limit, and then removing duplicates.

0
source

I would use an asterisk to mark an empty tile and do the following if the input was DOR *:

 SELECT * FROM scrabble WHERE d + o + r + $number_of_wildcards >= length 

If I understood your database structure correctly, this should have returned the door, the hostel, dork, smell, etc.

EDIT: this version should also match shorter words like do, rod, etc.

0
source

All Articles