2

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.

Nermin
  • 47
  • 1
  • 8
  • If you know a character is a letter, then you can score it as follows: `POINTS[tolower(c) - 'a']`. If you don't know, then you can first test with `isalpha(c)` to ensure you don't overflow your points array indexing. – paddy Oct 08 '20 at 22:18

3 Answers3

2

I think that the answer before mine kind of jumped the gun and gave away the code instead of the pseudo solution you asked for :P

But basically, what I ended up doing and it worked, was creating a for loop, in which I had an if/else statement that integrated through each of the letters in the words that were given at the prompt. If they were uppercase, I would subtract 65 (since 65 is the ASCII code for uppercase A), and if they were lowercase, I would subtract 97 (ASCII code for lowercase a).

I then added that value to the points in the array that was given, which would give me the score.

Then it was just a simple matter of making an if/else statement to print whoever won.

Sorry if my explanation is a bit confusing. I hope it makes sense! :D

I was super stoked that I managed to do this hahaha!

1

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.

You are on the right path! Characters are just integers and you can do math on them. You can convert them to a number starting at 0 by subtracting 'a'. 'a' - 'a' is 0. 'b' - 'a' is 1, 'z' - 'a' is 25.

Be sure to lower case the character first, and check that the result does not walk off the array bounds.

[Note: ASCII characters can be treated this way because they are a single byte. Variable-width encodings such as UTF-8 are more complicated.]

Schwern
  • 153,029
  • 25
  • 195
  • 336
1

That's my implementation with using CS50 library

#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);
if(score1>score2){
printf("Player 1 wins!\n");
}
else if(score1<score2){
printf("Player2 wins!\n");
}
else{
printf("Tie!\n");
}
// TODO: Print the winner
}

int compute_score(string word)
{
 int sum=0;
 int numb;
 int n = strlen(word);
 for(int i=0; i<n; i++){
   if(isupper(word[i])){
    numb = word[i]-65;
    numb = POINTS[numb];

   }
 if(islower(word[i])){
    numb = word[i]-97;
    numb = POINTS[numb];
}
else{
numb = 0;
}

sum+=numb;
}
return sum;
hIen hIen
  • 11
  • 2
  • 1
    Hello and welcome to SO! While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Tomer Shetah Dec 24 '20 at 15:49