Created
May 29, 2021 19:28
-
-
Save alrafiabdullah/810f82637d54ac47a4db23afe361e2ed to your computer and use it in GitHub Desktop.
A scrabble game made with C.
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 <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); | |
// TODO: Print the winner | |
if (score1 > score2) { | |
printf("Player 1 wins!\n"); | |
} else if (score1 < score2) { | |
printf("Player 2 wins!\n"); | |
} else { | |
printf("Tie!\n"); | |
} | |
return 0; | |
} | |
int compute_score(string word) | |
{ | |
// TODO: Compute and return score for string | |
int score = 0; | |
for(int i=0; i<strlen(word); i++) { | |
word[i] = tolower(word[i]); | |
if (word[i] > 96 && word[i] < 123) { | |
score += POINTS[(word[i]-97)]; | |
} | |
} | |
// printf("%d\n", score); | |
return score; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment