Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Krshojestic · Oct 09, 2012 at 02:37 PM · regularexpressions

Match all words containing specific set of letters

I have got a wordlist text file with about 17 000 words. I have also got a randomly generated letter set, say

string[] letters = { "a", "b", "t", "e", "l", "t" };

I am looking for an efficient way for my game to find all word matches that contain three or more or ultimately all of the letters it is given in the letters array. Listing all possible combinations seems like a very slow and inefficient way to work that out, so I assume it would be best if I used Regular Expression.

So here are some of the valid matches I am supposed to get from the list above: bat, table, battle, tab, belt, able, ale, late, tale

If I exclude "l", the number of valid matches narrow down to two: "bat" and "tab" only.

So basically it's a Scrabble game and I'm working on the AI: I want the computer opponent to be able to detect when it has gotten enough letters (represented by lettered tiles) to construct a specific word(s) from the database. So it needs to find all words that are constructed with three or more of the letters available. Since a single tile represents a single letter, there must not be any matches where a letter is used more than once, unless it appears more than once in the letters array.

Example: When you have got { "a", "b", "e", "l", "t" }, "table" would be a valid match. "battle" would be an invalid match, since there is only one instance of the letter "T" available.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Oct 09, 2012 at 05:30 PM 0
Share

dude, you totally WANT SCRABBLE PROGRA$$anonymous$$$$anonymous$$ED?? !

whoa !

to be honest, that is not so much Unity3D specific you know ..

I reckon, you could try on gamedev or something like that.

good luck !!

avatar image Krshojestic · Oct 09, 2012 at 05:51 PM 0
Share

No no, I don't need the whole game programmed. The human gameplay of my Scrabble game is almost done all I need now is this word matching (for the AI specifically) I'm talking about here.

avatar image Fattie · Oct 09, 2012 at 06:39 PM 0
Share

lol for sure dude. that's the rather hard part ! enjoy !

shadedrop has given you an incredible tip

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Mander · Oct 09, 2012 at 07:36 PM

as shaedrop but with an slight variation. i would assign weights to the different letters and check for them in a db or something. like this

1st i would check the number of letters

num of letters =

 2 - 100
 3 - 200
 4 - 300 
 5 - 400
 6 - 500
 7 - 600
 8 - 800    


i would a ssign a value to every letter and count the result but with the index of how many letter it has for example : the words iwth the same amout of letters and the same letters should give u the same n umber.

 a - 11       roma (400) =  119+116+114+11 = 400360   
 b - 22         amor (400) =  11+114+116+119 = 400360
 c - 33       risa (400) =  119+99+220+11  = 400449
 d - 44       casa (400) =  33+11+220+11   = 400275
 e - 55       raro (400) =  119+11+119+116 = 400365
 f - 66       toro (400) =  221+116+119+116= 400572
 g - 77       pala (400) =  117+11+113+11  = 400252
 h - 88
 i - 99
 j - 111
 k - 112
 l - 113
 m - 114
 n - 115
 o - 116
 p - 117
 q - 118
 r - 119
 s - 220
 t - 221
 u - 222
 v - 223
 w - 224
 x - 225
 y - 226
 z - 227


idk if i explained myself but i hope u get the idea. probably u might need to test it 1st just to make sure that the values wont be the same in special cases. and test with different weights. hope i hepled :P

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Krshojestic · Oct 09, 2012 at 10:42 PM 1
Share

You are right, there are cases where values are equal (i.e. 117+221=116+222=115+223), but I get the idea and it is brilliant! Thank you very much.

avatar image
2

Answer by shaderop · Oct 09, 2012 at 05:23 PM

I don't think regular expressions are going to help you here. One possible approach is to process the list of words into some kind of data structure, like a multi-value dictionary, where the key is the letters of the word sorted alphabetically, and the value is a list of words that match those letters.

Using your own example, you store the list of words bat, tab, belt, battle late and tale like so:

 Key    Values
 =====  ======
 abt    bat, tab
 aelt   tale, late
 belt   belt
 abeltt battle

And so on. Then all you need to do for any list of letters is to sort them alphabetically, combine them into a string, and use that to access the dictionary.

Hope this makes sense :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Oct 09, 2012 at 06:39 PM 0
Share

the first rule of hash club, never give away secrets! :)

avatar image Krshojestic · Oct 09, 2012 at 11:00 PM 0
Share

This is also an excellent idea!

avatar image Fattie · Oct 10, 2012 at 06:26 AM 0
Share

FTR this is how you do it in the major scrabble engines we are familiar with !

avatar image
0

Answer by hvilela · Oct 09, 2012 at 02:38 PM

Regular Expressions

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

One small step ... 1 Answer

Search value in XML 2 Answers

Determine whether a polygon is CIRCA convex 0 Answers

SOLVED Exclude set of keywords from string (using Regex) 1 Answer

Check connection to ground algorithm? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges