I am working on a problem that is asking to determine which word has a higher point value according to the Scrabble board game. My thought process is to:
- take user inputs (i.e. two words) in strings
- convert user inputs to lowercase
- call functions that calculate scores for each input
- Output which word has a higher point value
Here is what I have so far:
// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
int compute_score(string word);
int main(void)
{
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
//convert each word inputs to lowercase using 'tolower()' function
for (int m=0; m<strlen(word1); m++)
{
word1[m]=tolower(word1[m]);
}
for (int n=0; n<strlen(word2); n++)
{
word2[n]=tolower(word2[n]);
}
// Score both words
int score1 = compute_score(word1);
int score2 = compute_score(word2);
if (score1<score2)
{
printf("Player2 wins!");
printf("\n");
}
if (score2<score1)
{
printf("Player1 wins!");
printf("\n");
}
if (score1==score2)
{
printf("Tie!");
printf("\n");
}
printf("this is the score for word 1: ""%i\n", score1);
printf("this is the score for word 2: ""%i\n", score2);
}
int compute_score(string word) //uppercase letters are converted in the main();
{
int wordScore=0;
//the loop below converts each char in 'word' into int values according to ASCII. Then, it converts each int values mod 97
for(int i=0; i<=strlen(word); i++)
{
word[i]=((int)word[i]);
word[i]=word[i]%97;
printf("each letter is now converted to this int value: ""%i\n",word[i]);
}
for(int j=0; j<=strlen(word); j++)
{
for (int k=0; k<26; k++)
{
if(word[j]==k)
{
word[j]=POINTS[k];
}
}
}
for(int l=0; l<strlen(word); l++)
{
if(word[l]<33)//word[l]<33 since special characters will have values from 33~47;
{
wordScore+=word[l];
}
}
return wordScore;
}
For some reason, some letters, such as b
, do not get calculated correctly. What is the error? For the life of me, I can't figure out why any words with b
calculate the character b
as 2 points.