bot50 2024-02-23 20:46:34 +00:00
commit fd7c634fa8
1 changed files with 52 additions and 0 deletions

52
scrabble.c Normal file
View File

@ -0,0 +1,52 @@
#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;
}