diff --git a/csharp-scrabble-challenge.Main/MyDictionary.cs b/csharp-scrabble-challenge.Main/MyDictionary.cs new file mode 100644 index 0000000..64dc804 --- /dev/null +++ b/csharp-scrabble-challenge.Main/MyDictionary.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace csharp_scrabble_challenge.Main +{ + public class MyDictionary + { + + Dictionary dictionary; + + public MyDictionary() { + dictionary = new Dictionary(); + dictionary.Add('a', 1); + dictionary.Add('b', 3); + dictionary.Add('c', 3); + dictionary.Add('d', 2); + dictionary.Add('e', 1); + dictionary.Add('f', 4); + dictionary.Add('g', 2); + dictionary.Add('h', 4); + dictionary.Add('i', 1); + dictionary.Add('j', 8); + dictionary.Add('k', 5); + dictionary.Add('l', 1); + dictionary.Add('m', 3); + dictionary.Add('n', 1); + dictionary.Add('o', 1); + dictionary.Add('p', 3); + dictionary.Add('q', 10); + dictionary.Add('r', 1); + dictionary.Add('s', 1); + dictionary.Add('t', 1); + dictionary.Add('u', 1); + dictionary.Add('v', 4); + dictionary.Add('w', 4); + dictionary.Add('x', 8); + dictionary.Add('y', 4); + dictionary.Add('z', 10); + dictionary.Add(' ', 0); + } + + public int GetValue(char c) + { + if (!dictionary.ContainsKey(c)) + { + return 0; + } + return dictionary[char.ToLower(c)]; + } + + public bool KeyIsContained(char c) + { + return dictionary.ContainsKey(c); + } + } +} diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..2f06a02 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,11 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); + +using csharp_scrabble_challenge.Main; + +Console.WriteLine("enter a word: "); +string input = Console.ReadLine(); +Scrabble scr = new Scrabble(input); + +int score = scr.score(); + +Console.WriteLine($"{score}"); diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..a046566 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -7,17 +7,135 @@ namespace csharp_scrabble_challenge.Main { + + public class Scrabble { + public MyDictionary myDictionary = new MyDictionary(); + + private string _word; + private int multiplier = 1; + + + + public Scrabble(string word) - { - //TODO: do something with the word variable + { + _word = word.ToLower(); } + + + public int score() + { + int _score = 0; + int _multiplier = 1; + + int open3x = 0; + int open2x = 0; + //TODO: score calculation code goes here + //throw new NotImplementedException(); //TODO: Remove this line when the code has been written + foreach (var c in _word) + { + + int tempScore = 0; + + if (c == '{') + { + open2x++; + _multiplier *= 2; + } + else if (c == '}') + { + if (open2x == 0) + { + return 0; + } + open2x--; + _multiplier /= 2; + } + else if (c == '[') + { + open3x++; + _multiplier *= 3; + } + else if (c == ']') + { + if (open3x == 0) + { + return 0; + } + open3x--; + _multiplier /= 3; + } + else if (myDictionary.KeyIsContained(char.ToLower(c))) + { + tempScore = myDictionary.GetValue(char.ToLower(c)) * _multiplier; + } + else + { + return 0; + } + _score += tempScore; + } + + + + + + return _score; + } + + + + /* public int score() { + //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + //throw new NotImplementedException(); //TODO: Remove this line when the code has been written + return ScoreRecursive(_word); } - } + public int ScoreRecursive(string subString) + { + if (subString == null) return 0; + else if (subString.Length <= 1) + { + return myDictionary.GetValue(subString[0]); + } + else if (subString[0] == '{') + { + multiplier *= 2; + return ScoreRecursive(subString.Substring(1)); + } + else if (subString[0] == '[') + { + multiplier *= 3; + return ScoreRecursive(subString.Substring(1)); + } + else if (subString[0] == '}') + { + multiplier /= 2; + return ScoreRecursive(subString.Substring(1)); + } + else if (subString[0] == ']') + { + multiplier /= 3; + return ScoreRecursive(subString.Substring(1)); + } + + else + { + return multiplier * myDictionary.GetValue(subString[0]) + ScoreRecursive(subString.Substring(1)); + } + } + + + + */ + + + +} } + diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..57dff72 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)]