LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to compute the values assiged to each letter in a given string with an array cluster? As a scrabble game

Solved!
Go to solution

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?

riilol_0-1637199812361.png

riilol_2-1637200167496.png

 

0 Kudos
Message 1 of 8
(1,905 Views)

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.

 

0 Kudos
Message 2 of 8
(1,897 Views)

Thank you so much for your reply! Sorry that I just started Labview, what do you mean by a map?

0 Kudos
Message 3 of 8
(1,895 Views)

Look in the collections palette.

 

Here's how it could look like:

 

altenbach_1-1637205409801.png

 

 

and here's one way to generate that map constant (this needs to be done only once!):

 

altenbach_2-1637205501760.png

 

Message 4 of 8
(1,884 Views)

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

0 Kudos
Message 5 of 8
(1,875 Views)

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!

0 Kudos
Message 6 of 8
(1,836 Views)

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:

 

altenbach_0-1637267060437.png

 

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!)

 

 

Message 7 of 8
(1,813 Views)
Solution
Accepted by riilol

@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:

 

altenbach_1-1637354876751.png

 

(Of course this is silly for scrabble, but the technique is useful for other scenarios 😄 )

 

 

Message 8 of 8
(1,797 Views)