1

I am working on creating a game of scrabble. I have created a function to iterate through each letter and add it the points for each player. I keep running into an error when as I am trying to convert the word argument (which will be a string) into an array of characters.

Here is the error I am getting (I am using the CS50 IDE)

scrabble.c:44:27: error: incompatible pointer to integer conversion assigning to 'char' from 'string' (aka 'char *'); dereference with * [-Werror,-Wint-conversion]
    wordarray[wordlength] = word;
                          ^ ~~~~
                            *
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Points assigned to each letter of the alphabet
int POINTS[26] = {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};
char LETTERS[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' , 'z'};

int compute_score(string word);

int main(void)
{
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    // Print the winner
    if (score1 > score2) {

        printf("Player 1 is the winner!");

    } else if (score1 < score2) {

        printf("Player 2 is the winner!");

    } else {

        printf("It is a tie!");

    }
}

int compute_score(string word)
{
    // TODO: Compute and return score for string
**    //Turn word into an array of characters
    int wordlength = strlen(word);
    char wordarray[wordlength]; 
    wordarray[wordlength] = word;**
    //Placeholder for players total score
    int playerscore = 0;
    //Placeholder for index value
    int index;
    //Placeholder for word value
    int wordvalue;
    
    //Loop throught the word array 
    for (int i = 0; wordarray[i] < strlen(word); i++) {

         //the current letter being iterated through. Convert it to lowercarse
         char letter = tolower(wordarray[i]);

        //iterate through all the letters in the alphabet
        for (int j = 0; j < 25; j++) {

        //if the letter is in the alphabet 
            if (letter == LETTERS[j]) {
        //assign its index value to index
                index = LETTERS[j];
            }
        }
        //match letter with value of points, assign to wordvalue
        wordvalue = POINTS[index];
        //push the wordvalue to the total player score 
        playerscore += wordvalue;

}      
        //iterate through all letters and return the playerscore 
        return playerscore;

}

  • Have you completed [week 4 of CS50](https://cs50.harvard.edu/college/2022/fall/weeks/4/) yet? I am asking because it will be easier to explain to you what is going wrong if I am able to use terms that you learn in week 4. – Andreas Wenzel Dec 10 '22 at 22:59
  • The craft of coding is to be aware of what variables store. `wordarray[i] < strlen(word)` is obviously wrong... And, how many letters are there in the English alphabet? 26, or 25? – Fe2O3 Dec 10 '22 at 23:00
  • @Fe2O3 no need to be rude :) I am learning I will be the first to say I am very bad at this. To your rhetorical question - I used 25 to account for 0 index but I may be mistaken in that – Michael Agunga Dec 10 '22 at 23:06
  • @AndreasWenzel I am on week 2 unfortunately – Michael Agunga Dec 10 '22 at 23:07
  • @MichaelAgunga `j < 25` ensures that `j` will never *be* `25` - always lower. You can use `j <= 25`, or the more common idiom of `j < 26`. – Oka Dec 10 '22 at 23:11
  • Using arrays dimensioned for 26 `int`'s and 26 `char`'s, and then using 0 as the first index in the loop that searches one array, indicates that you understand 1-26 should _map to_ 0-25... It's not rude to point out one of the problems in your code. You're asking for assistance, no? – Fe2O3 Dec 10 '22 at 23:19
  • OT: a minor aesthetic point (of little-or-no consequence) is that Scrabble tiles are printed with UPPECASE letters... Your code might be a smidgen closer to the real-world game if you'd also used uppercase... As said, it is a very small point, but attention to minor details like this becomes increasingly important as you tackle more difficult problems. (It is also worth noting that Scrabble rules prohibit the use of acronyms or words that always appear as all-caps... Funny, that! `:-)` ) – Fe2O3 Dec 10 '22 at 23:31

1 Answers1

2

char wordarray[wordlength]; is the declaration of an array of char, with a length of wordlength.

The valid indices for accessing this array are [0, wordlength - 1].

The following

wordarray[wordlength] = word;

accesses the array at the invalid index of worldlength, and attempts to place the value of word at that position in the array. word is a string (actually an alias of char *), not a char, so the compiler warns you about this.

This is not how you copy a string to an array. For that, use strcpy. Do note that you will need one extra char for the null terminating byte ('\0') required to make a string.

char wordarray[wordlength + 1];
strcpy(wordarray, word);

That said, creating a copy of the string is not at all a requirement for this task. Just loop through the contents of the string given, find the position of each element of word in the LETTERS array, and sum the values found at the same indices in POINTS.

You mostly had this, just overthought it a bit.

int compute_score(string word)
{
    int score = 0;

    for (size_t i = 0; word[i]; i++)
    {
        for (size_t j = 0; j < 26; j++)
        {                      
            if (tolower((unsigned char) word[i]) == LETTERS[j])   
            {                           
                score += POINTS[j];     
                break;                  
            }                                                               
        }
    }

    return score;
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • Note for OP: You will only learn how to copy strings in [this chapter](https://video.cs50.io/AcWIE9qazLI?start=6365&end=7645) of week 4 of CS50, so don't be surprised if the function `strcpy` does not make sense to you. – Andreas Wenzel Dec 10 '22 at 23:26