cs50/scrabble.c

53 lines
1.0 KiB
C
Raw Normal View History

#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
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 get_score(string word);
int main(void)
{
// Prompt user(s) for two words
string word_1 = get_string("Player 1: ");
string word_2 = get_string("Player 2: ");
// Compute the score of each word
int score_1 = get_score(word_1);
int score_2 = get_score(word_2);
// Print the winner
if (score_1 > score_2)
{
printf("Player 1 wins!\n");
}
else if (score_1 < score_2)
{
printf("Player 2 wins!\n");
}
else
{
printf("Tie!\n");
}
}
int get_score(string word)
{
int score = 0;
int length = strlen(word);
for (int i = 0; i < length; i++)
{
if (isalpha(word[i]))
{
word[i] = toupper(word[i]) - 65;
}
}
for (int j = 0; j < length; j++)
{
score += POINTS[(int) word[j]];
}
return score;
}