Last active
January 2, 2021 14:59
-
-
Save jibachhydv/2dc8c9e34e41d382b446bd7a4cf42cd7 to your computer and use it in GitHub Desktop.
CS50x Lab 2020, Scrabble
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <ctype.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); | |
// Printing the Score | |
// printf("%i\n", score1); | |
// printf("%i\n", score2); | |
// Print the winner | |
if (score1 > score2) | |
{ | |
printf("Player 1 wins!\n"); | |
} | |
else if (score1 < score2) | |
{ | |
printf("Player 2 wins!\n"); | |
} | |
else { | |
printf("Tie!\n"); | |
} | |
} | |
int compute_score(string word) | |
{ | |
// score variable | |
int score = 0; | |
// Compute and return score for string | |
for (int i = 0; i < strlen(word); i++) | |
{ | |
// Check Whether the letter is alphabetical or not | |
if (isalpha(word[i]) != 0) | |
{ | |
// Convert them to lower and get ascii value | |
int ascii_value = (int) tolower(word[i]); | |
// Convert Ascii Value to the postional value | |
int position = ascii_value - 97; | |
// Increase The Score | |
score = score + POINTS[position]; | |
} | |
} | |
return score; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment