ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# arlm's solution

## to Scrabble Score in the C# Track

Published at Oct 19 2020 · 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

### ScrabbleScoreTests.cs

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

using Xunit;

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

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

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

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

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

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

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

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

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

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

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

public static class ScrabbleScore
{
private const string LEVEL_1 = "[AEIOULNRST]";
private const string LEVEL_2 = "[DG]";
private const string LEVEL_3 = "[BCMP]";
private const string LEVEL_4 = "[FHVWY]";
private const string LEVEL_5 = "[K]";
private const string LEVEL_8 = "[JX]";
private const string LEVEL_10 = "[QZ]";

public static int Score(string input)
{
var points = 0;

points += Regex.Matches(input, LEVEL_1, RegexOptions.IgnoreCase).Count;
points += Regex.Matches(input, LEVEL_2, RegexOptions.IgnoreCase).Count * 2;
points += Regex.Matches(input, LEVEL_3, RegexOptions.IgnoreCase).Count * 3;
points += Regex.Matches(input, LEVEL_4, RegexOptions.IgnoreCase).Count * 4;
points += Regex.Matches(input, LEVEL_5, RegexOptions.IgnoreCase).Count * 5;
points += Regex.Matches(input, LEVEL_8, RegexOptions.IgnoreCase).Count * 8;
points += Regex.Matches(input, LEVEL_10, RegexOptions.IgnoreCase).Count * 10;

return points;
}
}``````