i need to program a scrabble game for the cs50 course. right now i have this code but the problem is at first it always printed "Tie!" and now it always prints out "Player 2 wins!" no matter what i type in. i think my mistake is in for loop where u assign the letters to the number but i dont really know how i can fix it. does anyone may have an idea how?
// 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 word1);
int compute_scores(string word2);
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 = (int) compute_score;
int score2 = (int) compute_scores;
// Print the winner
if (score1 == score2)
{
printf("Tie! \n");
}
if (score1 < score2)
{
printf("Player 2 wins! \n");
}
if (score1 > score2)
{
printf("Player 1 wins! \n");
}
}
int compute_score(string word1)
{
// Compute and return score for string
int compute_score = 0;
int numb;
for (int i = 0, n = strlen(word1); i < n; i++)
{
if(isupper(word1[i]))
{
numb = word1[i] - 65;
numb = POINTS[numb];
}
if(islower(word1[i]))
{
numb = word1[i] - 97;
numb = POINTS[numb];
}
else
{
numb = 0;
}
}
compute_score = numb;
return compute_score;
}
int compute_scores(string word2)
{
// Compute and return score for string
int compute_scores = 0;
int numb;
for (int i = 0, n = strlen(word2); i < n; i++)
{
if(isupper(word2[i]))
{
numb = word2[i] - 65;
numb = POINTS[numb];
}
if(islower(word2[i]))
{
numb = word2[i] - 97;
numb = POINTS[numb];
}
else
{
numb = 0;
}
}
compute_scores = numb;
return compute_scores;
}
When you are a beginner, there exists almost no situation where you need to use a cast, ever. Or in the rare case where you need one, you need to know what you are doing.
int score1 = (int) compute_score;converts the unsigned address of the function itself to a signed int where it might not fit. That's nonsense. You need to call the function and store the result in anint. No casting anywhere.I'd recommend reading about functions in your favourite C book. I also recommend dropping CS-50 since it's a bad class teaching bad practices. It has a poor reputation among C programmers.