I have one word. At first, instead of letters, there are only ?. So for example, word 'town' would be shown like this '????'. Then user guesses letter and if he nails it, it changes from ? to actual letter. For example, if he guesses t, it would look like this 't???'.
The problem is, that I have no idea, how to go through string and divide it to characters. And if I somehow do it, I cannot change it in the new string.
Should look somehow like this.
word do: [ :c |
c = guessedChar
ifTrue[mask := mask, guessedChar]
ifFalse[mask := mask, '?']
].
mask is initialized to nil, because the word length can change and word is String.
guessedChar is connected to inputField, however it contains only one character at a time.
And would it be better, do it once for every guessedChar or hold collection of all guessed characters and run it over every time?
You could use
which is equivalent to:
except that you don't have to initialize and increment
i(which is a little bit more error prone, and more verbose)Addendum
Given that instances of
Stringcannot grow or change their size, which is immutable, I assume that what might change is the variableword. In that case you should initializemaskaccordingly, so both strings keep always the same length. Like this:If you also want to preserve the characters already guessed:
Where
#try:is the method we had before(you may want to
uppercasethings if required)