Are you looking for the solution of Harvard’s CS50 PSet 2? Do you want the CS50 Scrabble Solution of week 2?
If so you’re in the right place.
In this article, I’m going to share with you exactly how to solve the CS50 Scrabble problem and will also share the CS50 Scrabble solution with you.
Here’s how to solve the CS50 Scrabble problem.
The first thing to do is to understand what we are trying to build. You can read the instructions from CS50’s website.
In the game of Scrabble, players create words to score points, and the number of points is the sum of the point values of each letter in the word.
For example, if we wanted to score the word “CODE”, we would note that the ‘C’ is worth 3 points, the ‘O’ is worth 1 point, the ‘D’ is worth 2 points, and the ‘E’ is worth 1 point. Summing these, we get that “CODE” is worth 7 points.
We have to implement a program in C that determines the winner of a short Scrabble-like game. Your program should prompt for input twice: once for “Player 1” to input their word and once for “Player 2” to input their word.
Then, depending on which player scores the most points, your program should either print “Player 1 wins!”, “Player 2 wins!”, or “Tie!” (in the event the two players score equal points).
Here’s what your program should do:
$ ./scrabble
Player 1: Question?
Player 2: Question!
Tie!
$ ./scrabble
Player 1: red
Player 2: wheelbarrow
Player 2 wins!
$ ./scrabble
Player 1: COMPUTER
Player 2: science
Player 1 wins!
$ ./scrabble
Player 1: Scrabble
Player 2: wiNNeR
Player 1 wins!
Here’s my entire code for the CS50 Scrabble Solution:
#include <cs50.h>
#include <ctype.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 (score2 > score1)
{
printf("Player 2 wins!\n");
}
else
{
printf("Tie!\n");
}
}
int compute_score(string word)
{
// TODO: Compute and return score for string
int score = 0;
for (int n = 0; n < strlen(word); n++)
{
if (isupper(word[n]))
{
score = score + POINTS[(word[n] - 65)];
}
else if (islower(word[n]))
{
score = score + POINTS[(word[n] - 97)];
}
else
{
score = score + 0;
}
}
return score;
}
If you have any confusion or problem, feel free to drop a comment down below. In the meantime, you can read these articles.