I am very stuck with scrabble.
I seem to have the everything working correctly EXCEPT when the words contain the letters 'a','b' or 'c'.
I will post a simplified snippet of the code below and just the compute score section, I have removed some parts of the full solution I had and added some parts that may seem unnecessary because I'm trying to pinpoint what is going wrong.
int compute_score(string word) {
// TODO: Compute and return score for string
int n = strlen(word);
for (int l = 0; l < n; l++)
if(isupper(word[l]))
{
word[l] = tolower(word[l]);
}
printf("%s\n", "word: "); printf("%s\n", word);
printf("\n\n");
for (int l = 0; l < n; l++)
for (int a = 0; a < 26; a++)
if(word[l] == (a + 97))
{
word[l] = POINTS[a];
printf("%i\n", word[l])
}
else if(word[l] == a)
{
word[l] = 0;
printf("%s\n", "Here");
printf("%i\n", word[l]);
printf("%i\n", a);
printf("\n");
}
printf("\n");
int sum = 0;
for (int l = 0; l < n; l++)
{
sum = sum + word[l];
}
return sum;
}
I am having the code print out the value of each word[l] to identify the problem, and every word[l] prints out the corresponding value of POINTS[a] correctly
Except the letters a,b & c.
So for example, if I enter "xyz" for word1 the program will print
8 4 10
Which is great, that's exactly what I want.
But if for word1 I enter "abc"
The program will output
1 0 3 0 3 0
So there are two outputs for each letter, the first is correct and the second is 0 for some reason.
And when calculating "sum" it only takes into account the '0' outputs.
I have tried so many different things to try to figure out what is causing this and I'm out of ideas.
As you can see in the else if(word[l] == a) section
I am now having it print out the value of a that 'a','b' & 'c' that are triggering this are and when I enter word1: abc
The output is
Here 0 1
Here 0 3
Here 0 3
So 'a' == 1, 'b' == 3 and 'c' == 3, I don't understand how this is possible or why this is happening, and why it doesn't happen to any other letters other than a,b and c.
Any help would be really appreciated.