Assigning values to letters
-
Hello everyone !
So basically, I'm still kind of a beginner to coding, and I have in mind to create a Morse code training program. However, I have very quickly stumbled upon the problem of assigning morse code values to letters that the program inputs. What I would want the progam to do is to select random words and convert them in morse code. The player has to guess the word only by listening to the morse code.
But for that the program needs to know what sequence of dots and lines corresponds to any letter. Surprisingly this turned out to be way harder than what I thought it would be. I haven't tried anything since I didn't find anything really clear on the internet not using python dictionaries or stuff like that that Fuze doesn't have. I know how to write morse code but I just can't figure out a way to search for values assigned to a particular letter in a string.
Thanks for the help ! -
So, it's been years and years since I've even seen anything morse code related, but having just looked up an alphabet it looks like each letter has up to 5 dots or dashes. So I'd probably consider something like an 8 bit number where the highest 3 bits are never used and the lower 5 bits are either on for dash and off for dot. So according to the internet, the letter Z is 'dash, dash, dot, dot' so I'd convert that to binary '1100', pad it - '00001100' and convert it to decimal = 12.
Now you're going to need to store them so you can either go for an array of structs:
array codes = [ [ .letter = "A", .code = 1 ], [ etc ], [ .letter = "Z", .code = 12 ] ]
Or maybe just a 26 digit array (0 through 25).
Finally you need to have a dictionary of words and go through the letters of each word converting them.
-
Thanks !
But my question is how do you go from currentletter="z" to currentcode=12 ? How do you reference it ? -
If currentletter == ’z’ then currentcode = 12 endif
-
Well personally I'd probably just have a 26 element array (36 if you want numbers).
a = codes[0], b = codes[1], etc.
And you convert your letter like so:
int n = 0 for n = 0 to len(word) loop morseLetter = code[chrVal(word[n]) - 65] repeat
Basically, take the ascii value of A through Z which is 65 through 90 inclusive, subtract 65 from it and that gives you the index into your array.
You need to adjust for lower case numbers if you have them but since you're in control of that, make life easier for yourself and use all uppercase.
-
@toxibunny That would work, but writing this 26 times would not be very clean...
-
Thanks a lot ! I'll try that !
-
I use that for a similar sort of thing - printing text in a custom font. You use the ascii value of the letter minus 65 as the index to the tile in the font for the letter. A little planning up front and it becomes pretty straightforward
-
I foresee a little problem:
a = '.-' = '01' = 1
u ='..-' ='001' = 1
So morse isn't actually a binary system (binary, base 2, has
1
/0
), but it's a tenary system (tenary, base 3, it hasdot
/silence
/dash
)In the example below, I show how you could convert morse to a base 10 number (our counting system).
Some examples how this would work in a tenary system, however I don't recommend you use this in FUZE:
silence = 0 dot = 1 dash = 2 a = '.-' = 12 (base 3) = 5 (base 10) u = '..-' = 112 (base 3) = 14 (base 10) a '.-' > 12 > 5 b '-...' > 2111 > 67 c '-.-.' > 2121 > 70 d '-..' > 211 > 22 e '.' > 1 > 1 f '..-.' > 1121 > 43
In stead I think it's better to go with the idea by Martin, but in stead add a value for the amount of leading zero's. Reason being: FUZE has good features to handle binary values, but tenary values are not common or have supported features (as far as I know).
So in stead you could do:
[ [ .letter = "A", .code = 1, .leadingZeroes = 1 ], [ .letter = "U", .code = 1, .leadingZeroes = 2 ], [ .letter = "Z", .code = 12, .leadingZeroes = 0 ] ]
EDIT: but it does depend on what you want to use it for of course. If you need to store a single numeric value, then I guess the tenary system might be the option you're actually looking for...
-
Good spot! I overlooked that. But yeah, I guess since the highest 3 bits are not being used for the actual morse then maybe they could be used to indicate the number of leading zeros? I think that would work?