From bf5664c5b229379e082255dcc2dd45beb14b84c9 Mon Sep 17 00:00:00 2001 From: bot50 Date: Fri, 23 Feb 2024 20:50:27 +0000 Subject: [PATCH] kukemuna-cs50/problems/2024/x/scrabble@20240223T205027.798065387Z --- scrabble.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 scrabble.c diff --git a/scrabble.c b/scrabble.c new file mode 100644 index 0000000..390dec7 --- /dev/null +++ b/scrabble.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +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; + } + score += POINTS[(int) word[i]]; + } + return score; +}