diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..a2611e7 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,103 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + public string _word; + public Dictionary _scores = new Dictionary { + {'a', 1 }, + {'b', 3 }, + {'c', 3 }, + {'d', 2 }, + {'e', 1 }, + {'f', 4 }, + {'g', 2 }, + {'h', 4 }, + {'i', 1 }, + {'j', 8 }, + {'k', 5 }, + {'l', 1 }, + {'m', 3 }, + {'n', 1 }, + {'o', 1 }, + {'p', 3 }, + {'q', 10 }, + {'r', 1 }, + {'s', 1 }, + {'t', 1 }, + {'u', 1 }, + {'v', 4 }, + {'w', 4 }, + {'x', 8 }, + {'y', 4 }, + {'z', 10 }, + }; public Scrabble(string word) { //TODO: do something with the word variable + _word = word.ToLower(); } public int score() { + + //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + if (_word == "" || _word == " ") + { + return 0; + } + else if (_word.Any(char.IsDigit)) + { + return 0; + } + //throw new NotImplementedException(); //TODO: Remove this line when the code has been written + //else if (_word.Length == 1) + //{ + // return _scores[char.Parse(_word)]; + //} + + int sum = 0; + int multiplier = 1; + int curly = 0; + int square = 0; + char[] characters = _word.ToCharArray(); + foreach (char item in characters) + { + if (Char.IsWhiteSpace(item) || curly < 0 || square < 0) + { + return 0; + } + else if (item == '{') + { + multiplier *= 2; + curly += 1; + } + else if (item == '[') + { + multiplier *= 3; + square += 1; + } + else if (item == '}' && curly >= 0) + { + multiplier /= 2; + curly -= 1; + } + else if (item == ']' && square >= 0) + { + multiplier /= 3; + square -= 1; + } + else + { + sum += _scores[item] * multiplier; + } + } + + return sum; + + //else + //{ + // return 0; + //} } } } diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..adc61e7 100644 --- a/csharp-scrabble-challenge.Test/CoreTests.cs +++ b/csharp-scrabble-challenge.Test/CoreTests.cs @@ -5,7 +5,25 @@ namespace csharp_scrabble_challenge.Test { [TestFixture] public class CoreTests - { + { + //[TestCase("", 0)] + //[TestCase(" ", 0)] + //[TestCase(" \t", 0)] + //[TestCase("\n\r\t\b\f", 0)] + //[TestCase("a", 1)] + //[TestCase("f", 4)] + //[TestCase("OXyPHEnBUTaZoNE", 41)] + //[TestCase("quirky", 22)] + //[TestCase("street", 6)] + //public void WordScoreTests(string word, int targetScore) + //{ + // Assert.AreEqual(this.GetWordScore(word), targetScore); + //} + + [TestCase("[{h}o1s{e}]", 0)] // error case (zero for errors) + [TestCase("{h}ous{e}", 13)] + [TestCase("[{h}ous{e}]", 39)] + [TestCase("[h}ous{e}]", 0)] //Error case (zero for errors) [TestCase("", 0)] [TestCase(" ", 0)] [TestCase(" \t\n", 0)] @@ -15,9 +33,12 @@ public class CoreTests [TestCase("OXyPHEnBUTaZoNE", 41)] [TestCase("quirky", 22)] [TestCase("street", 6)] + [TestCase("]",0)] public void WordScoreTests(string word, int targetScore) { - Assert.AreEqual(this.GetWordScore(word), targetScore); + Scrabble scrabble = new Scrabble(word); + + Assert.That(scrabble.score(), Is.EqualTo(targetScore)); } private int GetWordScore(string word) => new Scrabble(word).score(); diff --git a/csharp-scrabble-challenge.Test/ExtensionTests.cs b/csharp-scrabble-challenge.Test/ExtensionTests.cs index 8eb37dc..d3b102f 100644 --- a/csharp-scrabble-challenge.Test/ExtensionTests.cs +++ b/csharp-scrabble-challenge.Test/ExtensionTests.cs @@ -18,6 +18,7 @@ public class ExtensionTests [TestCase("[quirky]", 66)] //extension triple word [TestCase("{OXyPHEnBUTaZoNE}", 82)] [TestCase("[OXyPHEnBUTaZoNE]", 123)] + [TestCase("A[Q]A", 32)] public void ExtendedCriteriaTests(string word, int targetScore) { Assert.AreEqual(this.GetWordScore(word), targetScore);