diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..f654986 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,17 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using csharp_scrabble_challenge.Main; +Console.WriteLine("Welcome to Scrabble!"); + + +while (true) +{ + Console.WriteLine("Write a Scrabble-word to get it's score(q to quit):"); + string input = Console.ReadLine(); + if (input == "q") + { + break; + } + + Scrabble scrabble = new Scrabble(input); + Console.WriteLine(scrabble.score()); +} \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..f39cc2b 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,104 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + private 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}, + {'{', 0}, + {'}', 0}, + {'[', 0}, + {']', 0} + }; + private string _word = ""; + private bool OnlyHasValidChars(string str) + { + string lowerStr = str.ToLower(); + foreach (char c in lowerStr) + { + if (!_scores.ContainsKey(c)) + { + return false; + } + } + return true; + } + public Scrabble(string word) - { + { + _word = word; //TODO: do something with the word variable } public int score() { + int _score = 0; + int _multiplier = 1; + int _balanced_curly = 0; + int _balanced_square = 0; + char _opening_bracket = ' '; + + + if (!OnlyHasValidChars(_word.ToLower())) { return 0; } + foreach(char c in _word.ToLower()) + { + if (c == '{') + { + _balanced_curly++; + _multiplier *= 2; + _opening_bracket = '{'; + } + else if (c == '[') + { + _balanced_square++; + _multiplier *= 3; + _opening_bracket = '['; + } + else if(c == ']') + { + if (_multiplier%3 != 0 || _opening_bracket == '{') { return 0;} + _balanced_square--; + _multiplier /= 3; + _opening_bracket = ' '; + + } + else if (c == '}') + { + if (_multiplier % 2 != 0 || _opening_bracket == '[') { return 0; } + _balanced_curly--; + _multiplier /= 2; + _opening_bracket = ' '; + + } + _score += _scores[c] * _multiplier; + } + if (_balanced_square != 0 || _balanced_curly != 0 || _multiplier != 1) { return 0; } + return _score; //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written } } } diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..b6330d6 100644 --- a/csharp-scrabble-challenge.Test/CoreTests.cs +++ b/csharp-scrabble-challenge.Test/CoreTests.cs @@ -6,6 +6,19 @@ namespace csharp_scrabble_challenge.Test [TestFixture] public class CoreTests { + [TestCase("", 0)] + [TestCase(" ", 0)] + [TestCase(" \t\n", 0)] + [TestCase("\n\r\t\b\f", 0)] + [TestCase("a", 1)] + [TestCase("f", 4)] + [TestCase("OXyPHEnBUTaZoNE", 41)] + [TestCase("quirky", 22)] + [TestCase("street", 6)] + [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)]