-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrabble.c
61 lines (53 loc) · 1.45 KB
/
scrabble.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// 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 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 wins!\n");
}
else if (score2 > score1)
{
printf("Player 2 wins!\n");
}
else
{
printf("Tie!\n");
}
}
int compute_score(string word)
{
// Compute and return score for string
// Define the length of the word
int lenght = strlen(word), i = 0, score = 0;
// Itirates over every character of the word
for (i = 0; i < lenght; i++)
{
// Checks if each character are an uppercase one, and if so, converts it to lowercase
if (isupper(word[i]))
{
word[i] = tolower(word[i]);
}
// Converts a = 0, b = 1, c = 2 ...
int n = word[i] - 97;
// Adds to score the apropriate points for each letter
if (n >= 0 && n < 26)
{
score += POINTS[n];
}
}
// Once finished the iteration, returns the value of the score
return score;
}