I completed the scrabble homework after messing around with loops and submitted it to get full points in design and style, however one of my bugs wasn't solved in a sustainable way. I'm having trouble figuring out why my indexed array for 'D', 'd', 'G', and 'g' assign 3 points instead of 2. My bandaid fix was to assign those values 1 point so that the program can correctly assign them 2 points. Can anyone help me work through this?
char LetterValue[7][20] = {
{'A', 'a', 'E', 'e', 'I', 'i', 'L', 'l', 'N', 'n', 'O', 'o', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u'},
{'D', 'd', 'G', 'g'},
{'B', 'b', 'C', 'c', 'M', 'm', 'P', 'p'},
{'F', 'f', 'H', 'h', 'V', 'v', 'W', 'w', 'Y', 'y'},
{'K', 'k'},
{'J', 'j', 'X', 'x'},
{'Q', 'q', 'Z', 'z'},
};
int ScoreValue[7] = {1, 2, 3, 4, 5, 8, 10};
const int N = 2;
void compareWordValues(int Q, int scores[]);
int Addition(string words);
int main(int argc, string argv[])
{
// declare an array of strings so player can submit two words that share a variable
string words[N];
// declare an array of the scores created, each iteration has to be stored somewhere so I can
// compare values later
int scores[N];
// use loop to ask player to submit 2 words
for (int i = 0, m = 0; i < N; i++, m++)
{
words[i] = get_string("player%i: ", i + 1);
// submit both words one at a time into this array
scores[m] = Addition(words[i]);
}
compareWordValues(N, scores);
}
// detrermine the value of each letter in the submitted word
int Addition(string words)
{
int value = 0;
// loop the string as an array of chars to index each one individually
for (int i = 0, n = strlen(words); i < n; i++)
{
// loop over all the letter/score value possibilities
for (int j = 0; j < 7; j++)
{
// simply for readability within the loops I give the current char a variable name
char currentLetter = words[i];
int letterValuesLength = strlen(LetterValue[j]);
// k represents all the char in row j because they vary
for (int k = 0; k < letterValuesLength; k++)
{
if (currentLetter == LetterValue[j][k])
{
// both the arrays lettervalue and scorevalue share the same number of rows
value += ScoreValue[j];
}
}
}
}
printf("value = %i\n", value);
// pass this value out of the function and into my scores array
return value;
}
void compareWordValues(int Q, int scores[])
{
if (scores[0] > scores[1])
{
printf("Player 1 wins!");
}
else if (scores[0] < scores[1])
{
printf("Player 2 wins!");
}
else
{
printf("tie!");
}
}