I'm trying to create some kind of a word game using Unity with C#. I want my app to be working offline, so I can't use web API's. I found a text file of nearly 55k words, but what would be the most optimized way of using it? I just want to check if the String entered is a valid word. I'm guessing I can't just create a list of 55k elements and check every one of them, even with sorting algorithms. So, how can other apps achieve this without destroying my device? What is the secret? Thanks!
CodePudding user response:
Well, 55k English words are not that great volume. Assuming each word being 10 unicode characters (which seems to be an overestimation) we can expect
the required space to be 2 * 55000 * 10 ~ 1 MB. To validate words efficiently (scanning 55k lines can be time consuming), try using HashSet<string>:
using System.IO;
using System.Linq;
...
HashSet<string> allValidWords = new HashSet<string>(File
.ReadLines(@"c:\myWords.txt")
.Where(line => !string.IsNullOrWhiteSpace(line))
.Select(line => line.Trim()), StringComparer.OrdinalIgnoreCase);
Then use Contains for validating:
string myWord = "Jabberwocky";
...
if (allValidWords.Contains(myWord)) {
// myWord is valid
}
else {
// myWord is not valid
}
CodePudding user response:
Use SQLite and store all words in a table. It's commands are very similar to SQL server and deploying it requires nothing but a dll. It is very fast and usefull for such purposes.
Please refer to the following link: sqlite.org
Note: Be careful of saving data in local storages like text or dbs. Hackers can mode your game very easily.
CodePudding user response:
You might want to use SQLite package and store all these words in a table. It is much more efficient then reading from txt files as it is relational and easy to query.
just query it with the most simplest sql query
select COLUMNS from TABLE where 'your_word' IN (..,..)
