DEV Community

Discussion on: Daily Challenge #8 - Scrabble Word Calculator

Collapse
 
n8chz profile image
Lorraine Lee • Edited

Good old C. Be advised though there are ways to bork this one with some inputs not consistent with the rubric.

int score(char *word) {
  // Digits are one less than score values for each letter
  // This maps 1-10 to 0-9, allowing one-character representation of each score
  char *letter_scores = "02210313074020029000033739";
  int score = 0, letter_count = 0, mult=1;
  for(char *ptr = word; *ptr; ptr++) {
    if (isalpha(*ptr)) {
      letter_count++;
      int letter_score = letter_scores[toupper(*ptr)-'A']-'0'+1;
      score += letter_score;
      if (*(ptr+1) == '*') {
        score += letter_score;
        if (*(ptr+2) == '*') {
          score += letter_score;
        }
      }
    }
    if (*ptr == '(') {
      mult = *(ptr+1) == 'd' ? 2 : 3;
      break;
    }
  }
  return score*mult+50*(letter_count == 7);
}