I’m doing a cs50 problem set where I have to make a game of scrabble basically two people write a word and each letter in the word gives certain amount of points ie. A gives one point and Z gives 10
i was trying to make the storing system using a switch statement and a couple of char arrays containing each letter and how many points it gives ie. array “One” stores letters that give one point and “Two” two points and so on
but i figured out i can’t put arrays in x position the (case x:)
so i tried a few things and got this
is there a way to make this more efficient or should i go back to switch statements or what?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
//get the strings from the players and store them in a string data type
string p1 = get_string("Player 1: ");
string p2 = get_string("Player 2: ");
//make score data types for each player
int score1 = 0;
int score2 = 0;
char one[20] = {'a','A','e','E','i','I','l','L','n','N','o','O','r','R','s','S','t','T','u','U'};
char two[4] = {'d','D','g','G'};
char three[8] = {'b','B','c','C','m','M','p','P'};
char four[10] = {'f','F','h','H','v','V','w','W','y','Y'};
char five[2] = {'k','K'};
char eight[4] = {'j','J','x','X'};
char ten[4] = {'q','Q','z','Z'};
int str = strlen(p1);
for(int N = 0; N < str; N++)
{
if(p1[N] == one[0] || p1[N] == one[1] || p1[N] == one[2] || p1[N] == one[3] || p1[N] == one[4] || p1[N] == one[5] || p1[N] == one[6] || p1[N] == one[7] || p1[N] == one[8] || p1[N] == one[9] || p1[N] == one[10] || p1[N] == one[11] || p1[N] == one[12] || p1[N] == one[13] || p1[N] == one[14] || p1[N] == one[15] || p1[N] == one[16] || p1[N] == one[17] || p1[N] == one[18] || p1[N] == one[19])
{
score1++;
}else if(p1[N] == two[0] || p1[N] == two[1] || p1[N] == two[2] ||p1[N] == two[3])
{
score1 += 2;
}else if(p1[N] == three[0] || p1[N] == three[1] || p1[N] == three[2] || p1[N] == three[3] || p1[N] == three[4] || p1[N] == three[5] || p1[N] == three[6] || p1[N] == three[7])
{
score1 += 3;
}
}
}
user26580059 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.