Y-X99
Y-X99

Reputation: 1

cs50 Scrabble game

I'm doing the cs50 scrabble, for some reason it only returns numbers instead of telling me which player wins. I know I must have screwed up somewhere in my for loop, but I can't locate the problems:( Also I added int i and int n in the compute_score function because it keeps saying undeclare identifiers, I don't understand because I think they are within the scope of for loop. Noobie here, please advise!

#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("Player1 wins!\n");
    }
    else if (score1 == score2)
    {
        printf("Ties!\n");
    }
    else
    {
        printf("Player2 wins!\n");
    }
}

int compute_score(string word)
{
    // TODO: Compute and return score for string
    int tem_point[] = {};
    int m; 
    int score = 0;
    int i;
    int n;
    for (i = 0, n = strlen(word); i < n; i++)
    {
        //Turning to ascii number, "A...Z" correspond "65...90", "a...z" correspond "97...122"
        m = get_int("%i", word[i]);
        //Comparing ascii numbers to decide whether upper case, lower case or not characters
        //Calculate index corresponding to POINTS array and assign points to characters 
        //If not character, get zero point
        if (m < 65 || (m > 90 && m < 97) || m > 122)
        {
            tem_point[i] = 0;
        }
        //If upper case 
        else if (m >= 65 && m <= 90)
        {
            tem_point[i] = POINTS[m - 65];
        }
        //If lower case
        else
        {
            tem_point[i] = POINTS[m - 97];
        }
        score += tem_point[i]; 
    }
    return score;
    
} 

And here's when I run it:

~/pset2/ $ make scrabble
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    scrabble.c  -lcrypt -lcs50 -lm -o scrabble
~/pset2/ $ ./scrabble
Player 1: abc
Player 2: abc1
97

Upvotes: 0

Views: 780

Answers (1)

CrazyKanav
CrazyKanav

Reputation: 45

Fixed your program, there were multiple errors.

1. Firstly you don't need to use:

m = get_int("%i", word[i])

This is unnecessary

2. Dont use a list like temp_points[]

Found this useless, instead just use a simple int value like score and keep adding it.

3. The main error i think you did was using temp_point[i] = 0

if you make it 0 at the start, then all the points u had will all go away and be 0

Your new source code:

#include <stdio.h>
#include <string.h>
#include <cs50.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("Player1 wins!\n");
    }
    else if (score1 == score2)
    {
        printf("Ties!\n");
    }
    else
    {
        printf("Player2 wins!\n");
    }
}

int compute_score(string word)
{
    // TODO: Compute and return score for string
    int m;
    int score = 0;
    int i;
    int n;
    for (i = 0, n = strlen(word); i < n; i++)
    {
        //Turning to ascii number, "A...Z" correspond "65...90", "a...z" correspond "97...122"
        //Comparing ascii numbers to decide whether upper case, lower case or not characters
        //Calculate index corresponding to POINTS array and assign points to characters
        //If not character, get zero point
        //If upper case
        if (word[i] > 64 && word[i] < 91)
        {
            score += POINTS[word[i] - 65];
        }
        //If lower case
        if (word[i] > 96 && word[i] < 123)
        {
            score += POINTS[word[i] - 97];
        }
        else {
            score += 0;
        }
    }
    return score;

}

Hope I helped :)

Upvotes: 2

Related Questions