diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..352f63f 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,16 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); + +using csharp_scrabble_challenge.Main; + +Console.WriteLine("Welcome to Scrabble"); +Console.WriteLine("Enter a word and get the score! Add a {} or [] around a letter or multiple letters to multiply score by 2 or 3 respectively"); +Console.WriteLine("To exit press q and Enter."); +while (true) +{ + string word = Console.ReadLine(); + if (word == "q") break; + Scrabble scrabble = new Scrabble(word); + Console.WriteLine($"{scrabble.score()}"); +} + +Console.WriteLine("Exiting... Thanks for playing!"); \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..94e8e55 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,83 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + public string _word; + private Dictionary _dictionary = new Dictionary + { + { 'A', 1 }, + { 'E', 1 }, + { 'I', 1 }, + { 'O', 1 }, + { 'U', 1 }, + { 'L', 1 }, + { 'N', 1 }, + { 'R', 1 }, + { 'S', 1 }, + { 'T', 1 }, + { 'D', 2 }, + { 'G', 2 }, + { 'B', 3 }, + { 'C', 3 }, + { 'M', 3 }, + { 'P', 3 }, + { 'F', 4 }, + { 'H', 4 }, + { 'V', 4 }, + { 'W', 4 }, + { 'Y', 4 }, + { 'K', 5 }, + { 'J', 8 }, + { 'X', 8 }, + { 'Q', 10 }, + { 'Z', 10 } + }; public Scrabble(string word) - { - //TODO: do something with the word variable + { + _word = word; } public int score() { - //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + int sum = 0; + int openCurly = 0; + int openHard = 0; + int BonusMultiplier = 1; // 1 -> no bonus, 2 -> double, 3 -> triple + foreach (char item in _word) + { + if (char.IsWhiteSpace(item)) return 0; + if (char.IsNumber(item)) return 0; + if (item == '{') + { + openCurly++; + BonusMultiplier = BonusMultiplier * 2; + } + else if (item == '[') + { + openHard++; + BonusMultiplier = BonusMultiplier * 3; + } + else if (item == '}') + { + if (openCurly == 0) return 0; + openCurly--; + BonusMultiplier = BonusMultiplier / 2; + } + else if (item == ']') + { + if (openHard == 0) return 0; + openHard--; + BonusMultiplier = BonusMultiplier / 3; + } + else + { + char letter = char.Parse(item.ToString().ToUpper()); + sum = sum + _dictionary[letter] * BonusMultiplier; + } + + + } + if (openCurly > 0 || openHard > 0) return 0; + return sum; } } } diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..ecdc8c0 100644 --- a/csharp-scrabble-challenge.Test/CoreTests.cs +++ b/csharp-scrabble-challenge.Test/CoreTests.cs @@ -5,7 +5,11 @@ namespace csharp_scrabble_challenge.Test { [TestFixture] public class CoreTests - { + { + [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)] @@ -17,7 +21,9 @@ public class CoreTests [TestCase("street", 6)] 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..7b07bb6 100644 --- a/csharp-scrabble-challenge.Test/ExtensionTests.cs +++ b/csharp-scrabble-challenge.Test/ExtensionTests.cs @@ -18,6 +18,8 @@ public class ExtensionTests [TestCase("[quirky]", 66)] //extension triple word [TestCase("{OXyPHEnBUTaZoNE}", 82)] [TestCase("[OXyPHEnBUTaZoNE]", 123)] + [TestCase("A[Q]A", 32)] + [TestCase("{{A}}", 4)] public void ExtendedCriteriaTests(string word, int targetScore) { Assert.AreEqual(this.GetWordScore(word), targetScore);