I’m developing a word game and basically I want to assign an integer value to each character of the alphabet.
Currently, I have a helper to return the value for each char but I’m wondering how I should construct the initial data structure.
At the moment it is a dictionary containing each of the letters of the alphabet as the key and I want the points to be the object for that key. What is the best practise to set the points object?
I want to avoid things like this:
if (_letter == 'a' || _letter == 'A') _points = 1;
else if (_letter == 'b' || _letter == 'B') _points = 4;
else if (_letter == 'c' || _letter == 'C') _points = 3;
Many Thanks
You could use a 26-element C array of integers, where each integer is the point value of that letter, with the 0th element being A, the 1st element being B, etc. You then just lowercase the letter before subtracting
'a'from it and use that as the index into the array. At this point, the only thing left is to prevent non-ASCII-alphabetic characters from being considered, which can be done with a simple range check after lowercasing (if (c >= 'a' && c <= 'z')).