I am doing CS50 and I have an assignment of creating a "Scrabble" like game. Here are the implementation details.
The goal is for two players to enter their words and the higher scoring player wins. Points are scored in an array called POINTS[]. Link to an image with the extra details.
There are some few lines of code already implemented that were not written by me but were provided instead.
What I tried: I realized that I first must convert a string to a character. I did that by doing like so:
`int main(void)
{
int score = 0;
// Get input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
for (int i = 0, n = strlen(word1); i < n; i++)
{
char c1 = word1[i];
char c2 = c1;`
Then, I declared a score variable and manually checked for some letters inputted, incremented the score variable just to see would my idea work. I did it this way, so I could get an idea of how a for loop should be done since I am new, I feel it's easier to get the idea for a loop this way. I checked for each letter like so:
int main(void)
{
int score = 0;
// Get input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
for (int i = 0, n = strlen(word1); i < n; i++)
{
char c1 = word1[i];
char c2 = c1;
if (c2 == 'a')
{
score = POINTS[0];
printf("%i", score);
}
else if (c2 == 'b')
{
score = POINTS[1];
printf("%i", score);
}
else if (c2 == 'c')
{
score = POINTS[2];
printf("%i", score);
}
else if (c2 == 'd')
{
score = POINTS[3];
printf("%i", score);
}
Now that worked, but only worked if the words user inputted in that order, ABCD, I would get the values from the array as assigned to the score variable.
Also, all the calculation has to be put inside a function and variable scopes also come in play there. Just for easier reference here is the full code that I have been given, with no edits of mine at all.
#include <cs50.h>
#include <stdio.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
}
int compute_score(string word)
{
// TODO: Compute and return score for string
}
I would prefer if no code was written as an answer but rather pseudo solution, otherwise I will not learn by using someone else's code.