Avatar of artemkorsakov

artemkorsakov's solution

to Scrabble Score in the Java Track

Published at Feb 11 2019 · 0 comments
Instructions
Test suite
Solution

Given a word, compute the scrabble score for that word.

Letter Values

You'll need these:

Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10

Examples

"cabbage" should be scored as worth 14 points:

  • 3 points for C
  • 1 point for A, twice
  • 3 points for B, twice
  • 2 points for G
  • 1 point for E

And to total:

  • 3 + 2*1 + 2*3 + 2 + 1
  • = 3 + 2 + 6 + 3
  • = 5 + 9
  • = 14

Extensions

  • You can play a double or a triple letter.
  • You can play a double or a triple word.

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ScrabbleScoreTest.java

import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ScrabbleScoreTest {

    @Test
    public void testALowerCaseLetter() {
        Scrabble scrabble = new Scrabble("a");
        assertEquals(1, scrabble.getScore());
    }

    @Ignore("Remove to run test")
    @Test
    public void testAUpperCaseLetter() {
        Scrabble scrabble = new Scrabble("A");
        assertEquals(1, scrabble.getScore());
    }

    @Ignore("Remove to run test")
    @Test
    public void testAValuableLetter() {
        Scrabble scrabble = new Scrabble("f");
        assertEquals(4, scrabble.getScore());
    }

    @Ignore("Remove to run test")
    @Test
    public void testAShortWord() {
        Scrabble scrabble = new Scrabble("at");
        assertEquals(2, scrabble.getScore());
    }

    @Ignore("Remove to run test")
    @Test
    public void testAShortValuableWord() {
        Scrabble scrabble = new Scrabble("zoo");
        assertEquals(12, scrabble.getScore());
    }

    @Ignore("Remove to run test")
    @Test
    public void testAMediumWord() {
        Scrabble scrabble = new Scrabble("street");
        assertEquals(6, scrabble.getScore());
    }

    @Ignore("Remove to run test")
    @Test
    public void testAMediumValuableWord() {
        Scrabble scrabble = new Scrabble("quirky");
        assertEquals(22, scrabble.getScore());
    }

    @Ignore("Remove to run test")
    @Test
    public void testALongMixCaseWord() {
        Scrabble scrabble = new Scrabble("OxyphenButazone");
        assertEquals(41, scrabble.getScore());
    }

    @Ignore("Remove to run test")
    @Test
    public void testAEnglishLikeWord() {
        Scrabble scrabble = new Scrabble("pinata");
        assertEquals(8, scrabble.getScore());
    }

    @Ignore("Remove to run test")
    @Test
    public void testAnEmptyInput() {
        Scrabble scrabble = new Scrabble("");
        assertEquals(0, scrabble.getScore());
    }

    @Ignore("Remove to run test")
    @Test
    public void testEntireAlphabetAvailable() {
        Scrabble scrabble = new Scrabble("abcdefghijklmnopqrstuvwxyz");
        assertEquals(87, scrabble.getScore());
    }

}
class Scrabble {
    private String word;

    Scrabble(String word) {
        this.word = word.toUpperCase().replaceAll("\\W", "");
    }

    int getScore() {
        return word.chars().map(this::getScore).sum();
    }

    private int getScore(int ch) {
        return getScore(Character.toString((char) ch));
    }

    private int getScore(String letter) {
        return letter.isEmpty() ? 0
                : "AEIOULNRST".contains(letter) ? 1
                : "DG".contains(letter) ? 2
                : "BCMP".contains(letter) ? 3
                : "FHVWY".contains(letter) ? 4
                : "K".contains(letter) ? 5
                : "JX".contains(letter) ? 8
                : "QZ".contains(letter) ? 10
                : 0;
    }

}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?