I’ve been thinking about word games lately. I am an inept coder. The largest hurdle I face is that I don’t know how to ask questions because I am unfamiliar with the terms and language that I need to describe my goals.
At it’s most basic level, I want to give my players a set of letters and I want them to be able to spell a word with those letters.
What are the kinds of things scripting wise that I need to understand in order to accomplish this goal? So far I’ve got lists, dictionaries and strings as the terms I need to understand and research a little more intently.
With it being that simple. There’s only one or two things that you really need. Asides of course a way to put letters together and a place to store the letters.
It’s just simple addition really, and knowing about working with strings. So look up things like String.Split, and String.Trim and stuff. I mean if it’s that simple you’d need something as little as:
public string playersTestWord = "";
public string[] listOfWords = new string[4] { "Hi", "me", "yes", "no" };
// Just call this function with whatever letter you'd like it to be. This can be called by a button, or anything really.
public void AddLetter(string letter)
{
playersTestWord = playersTestWord + letter;
}
// And call this one to remove the last letter
public void RemoveLastLetter()
{
if(playersTestWord.Length > 0)
{
playersTestWord.TrimEnd(playersTestWord[playersTestWord.Length - 1]);
}
else
{
Debug.Log("No letters left to remove!");
}
}
public void CheckForWord()
{
for(int i=0; i < listOfWords.Length; i++)
{
if (playersTestWord == listOfWords[i])
{
// Do something really cool! They win something?
}
}
}
Attach that code to one object in your game. And all the letters let them call this one object’s scripts. In the editor Inspector you’d be able to manually write all the words you’d like in there.
As I’m thinking about this, I’m thinking about a way to generate a list of potential words based on the random combination of letters that I give the player.
Is brute force the only way to go about this? Say I give them seven letters, that’s what… 343 possible words. Do I need to check each one of these combinations against a word list or is there a better way to go about this?
This is probably immaterial to your question, but there are actually (7!) = 5,040 possible combinations of 7 letter tiles.
Pretty much, yeah. There are a few optimizations you can do to speed that up, but they’re probably all beyond a novice programmer’s skills. Don’t worry about it too much though - 5,000 combinations is nothing. When hackers brute-force a system password, for example, they will test hundreds of thousands, if not millions, of passwords per second. I’d worry more about the size of your dictionary, honestly.
If you code this and do find that it’s slow, two pieces of advice:
Come back here and ask for help optimizing.
Consider putting it into a coroutine that yields after checking every 500th letter combination or something. It’ll take multiple frames to churn through the combinations, but the game itself won’t freeze while you’re doing it. Since this is something that’s only going to have to run once per turn, you shouldn’t have issues there.
Also, this algorithm isn’t going to be easy - don’t be shy about asking for help on that. I would classify this at the difficulty level “this’ll be fun”, which is me-speak for “pretty tough”.
You should try to make the list of possible words as small as possible before checking them completely.
If we go with your example, 7 letters, you should have a list with words that only have 7 letters. (You could filter this at the start of the game).
Secondly you should check if the word contains all those letters. This way the list of possible words is a lot shorter already.
After these two things you can do some more checks, but this way it will be a lot faster.
I think you should not worry too much about performance though. The player does not know the answer in less than 10 seconds, and 10 seconds of calculating is a lot.
Bridin, if you want to be really cheeky via brute force. Google ‘words that have 7 letters’. There’s several websites that will list every word. You could copy and paste that into the script (if you wanted to be really cheeky). All you need to do then is then paste it in between those curly braces where those 4 words are in my example.
Writing this novicely would be easy enough for someone who’s used to coding, if you’re totally new to it though, and don’t want to commit to learning, do the ‘whatever works’ theory :).
The biggest challenge here will probably be to search for words.
There’s just above one million English words. If you want to include things like every possible tense of each verb, and the plural for each noun, you probably get a bunch more.
If you just put all of the valid words in an array, and try to naively iterate through them to check if an arbitrary word is valid, that’ll take quite some time. Usually you’d use a HashSet of some kind to check if something is in a set fast, but I think there’s enough words that there will be collisions. You’ll probably have to roll your own structure to check this fast.
That’s overkill. There are about 267,000 words in a standard Scrabble dictionary, which would be what’s applicable here. (That includes tense variations.) When you say “quite some time”, I think we’re looking at less than a second even with an unoptimized array search, which is passable in this scenario if it’s either threaded or split up among frames using a coroutine. You can optimize it really easily, too, and probably get it down to “let’s find words every frame just for fun” speeds after you’ve read in the dictionary to the right data structure.