11-17-2021 07:53 PM - edited 11-17-2021 07:54 PM
Hi I am trying to do a scrabble game where I put in a string control and the code will extract all the letters from it. Then from the array cluster that I built in subvi (as shown below), the corresponding value for each letter will be found and added up together. For example, if type in ABC, the program should give an output of 7(1+3+3). But I am having trouble breaking down the string into letters and having the loop search for each letter. Can someone help me with finding the assigned value for each letter from a string control?
Solved! Go to Solution.
11-17-2021 08:42 PM
I would use "string to boolean array", autoindex over the U8 array, and use each element as key for a map with values for all possible letters. Sum the values.
11-17-2021 08:48 PM
Thank you so much for your reply! Sorry that I just started Labview, what do you mean by a map?
11-17-2021 09:20 PM
Look in the collections palette.
Here's how it could look like:
and here's one way to generate that map constant (this needs to be done only once!):
11-17-2021 09:37 PM
Wow thanks for your reply but we haven't learned those functions yet. I think we are only allowed to use "Match Pattern" to calculate the score. The values for each letter are already given in the subvi, we just need to extract that information and put it together. Sorry that we are only introduced with the basic string functions so far
11-18-2021 01:30 AM
Well, it won't be that much different, even without a map.
Most of your original code is way too convoluted, for example the score should remain blue. No need for all these string manipulations. You have 10x more code than necessary!
11-18-2021 02:35 PM - edited 11-18-2021 02:37 PM
I am sure you solved your problem in the meantime, so this is just to give you additional ideas:
If you are required to use "match pattern", you could for example do as follows:
For each entry in the array, we count how many of those letters exist in the Word (inner loop!) and add up the scores. Note that this code does not check for invalid characters in the input (spaces, tabs, periods, etc.) so if this is a possibility, validity checks need to be implemented
(And yes, if you are allowed to use additional string function beside "match pattern", many simpler solutions are possible, even without the use of maps!)
11-19-2021 02:49 PM
@altenbach wrote:
(And yes, if you are allowed to use additional string function beside "match pattern", many simpler solutions are possible, even without the use of maps!)
If performance is important (e.g. if you need to find the scrabble score of the collective works of Shakespeare 😄 , a small LUT (calculated once!) where the character is the index and the value is the score is probably most efficient:
(Of course this is silly for scrabble, but the technique is useful for other scenarios 😄 )