Avatar of artemkorsakov

artemkorsakov's solution

to Scrabble Score in the C# Track

Published at Feb 06 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

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit ScrabbleScore.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

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.cs

// This file was auto-generated based on version 1.1.0 of the canonical data.

using Xunit;

public class ScrabbleScoreTest
{
    [Fact]
    public void Lowercase_letter()
    {
        Assert.Equal(1, ScrabbleScore.Score("a"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Uppercase_letter()
    {
        Assert.Equal(1, ScrabbleScore.Score("A"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Valuable_letter()
    {
        Assert.Equal(4, ScrabbleScore.Score("f"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Short_word()
    {
        Assert.Equal(2, ScrabbleScore.Score("at"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Short_valuable_word()
    {
        Assert.Equal(12, ScrabbleScore.Score("zoo"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Medium_word()
    {
        Assert.Equal(6, ScrabbleScore.Score("street"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Medium_valuable_word()
    {
        Assert.Equal(22, ScrabbleScore.Score("quirky"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Long_mixed_case_word()
    {
        Assert.Equal(41, ScrabbleScore.Score("OxyphenButazone"));
    }

    [Fact(Skip = "Remove to run test")]
    public void English_like_word()
    {
        Assert.Equal(8, ScrabbleScore.Score("pinata"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Empty_input()
    {
        Assert.Equal(0, ScrabbleScore.Score(""));
    }

    [Fact(Skip = "Remove to run test")]
    public void Entire_alphabet_available()
    {
        Assert.Equal(87, ScrabbleScore.Score("abcdefghijklmnopqrstuvwxyz"));
    }
}
using System;
using System.Linq;

/// <summary>
/// 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
/// </summary>
public static class ScrabbleScore
{
    public static int Score(string input)
    {
        return input.ToUpper().Select(GetScore).Sum();
    }

    private static int GetScore(char c)
    {
        return OnePoint.Contains(c) ? 1 :
            TwoPoint.Contains(c) ? 2 :
            ThreePoint.Contains(c) ? 3 :
            FourPoint.Contains(c) ? 4 :
            FivePoint.Contains(c) ? 5 :
            EightPoint.Contains(c) ? 8 :
            TenPoint.Contains(c) ? 10 :
            0;
    }

    private static readonly char[] OnePoint = { 'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T' };
    private static readonly char[] TwoPoint = { 'D', 'G' };
    private static readonly char[] ThreePoint = { 'B', 'C', 'M', 'P' };
    private static readonly char[] FourPoint = { 'F', 'H', 'V', 'W', 'Y' };
    private static readonly char[] FivePoint = { 'K' };
    private static readonly char[] EightPoint = { 'J', 'X' };
    private static readonly char[] TenPoint = { 'Q', 'Z' };
}

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?