diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..2853ea3 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,100 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + String word; public Scrabble(string word) - { + { //TODO: do something with the word variable + this.word = word.ToUpper(); } public int score() { //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + + //set score to be zero + int score = 0; + //initialise a dictionary with all characters and ther points + Dictionary map = 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},{ 'X', 8},{ 'Y', 4}, + { 'Z', 10},}; + + + + for(int i = 0; i < word.Length; i++) + { + + if (map.ContainsKey(word[i])) + { + score += map[word[i]]; + } + else if (!(map.ContainsKey(word[i]))) + { + if (word[i] == '{' || word[i] == '[') + { + int outerMulti = word[i] == '{' ? 2 : 3; + //skrive expected end bracket + char expectedOuterBracket= word[i] == '{' ? '}' : ']'; + i++; + + + while (word[i] != '}' && word[i] != ']') + { + + if (word[i] == '{' || word[i] == '[') + { + int innerMulti = word[i] == '{' ? 2 : 3; + char expectedInnerBracket = word[i] == '{' ? '}' : ']'; + i++; + while (word[i] != '}' && word[i] != ']') + { + if (!(map.ContainsKey(word[i]))){ + return 0; + } + int mdl = map[word[i]]; + //hva skal multiplieren være?? + score += mdl * innerMulti * outerMulti; + i++; + + } + //her kan man sjekke bracket + if (expectedInnerBracket != word[i]) + { + return 0; + } + i++; + + } + else { + if (!(map.ContainsKey(word[i]))) + { + return 0; + } + int newscore = map[word[i]]; + score += newscore * outerMulti; + i++; + } + + } + //her kan man sjekke bracket + if (expectedOuterBracket != word[i]) + { + return 0; + } + + } + + } + + + } + return score; + + } + } } diff --git a/csharp-scrabble-challenge.Test/ExtensionTests.cs b/csharp-scrabble-challenge.Test/ExtensionTests.cs index 8eb37dc..da462a0 100644 --- a/csharp-scrabble-challenge.Test/ExtensionTests.cs +++ b/csharp-scrabble-challenge.Test/ExtensionTests.cs @@ -11,6 +11,16 @@ namespace csharp_scrabble_challenge.Test [TestFixture] public class ExtensionTests { + [TestCase("d{o}g", 6)] //extension double letter + [TestCase("d[o]g", 7)] //extension triple letter + [TestCase("[d[1]g]", 0)] + [TestCase("[[zz]z]", 210)] + + [TestCase("j[o]h{n}", 17)] // "j[o]h{n}": o blir trippel, n blir dobbel + [TestCase("k[e]y", 12)] // "k[e]y": e blir trippel + [TestCase("{x}yz", 30)] // "{x}yz": x blir dobbel + [TestCase("k[e][y]", 20)] // "k[e]y": e blir trippel + [TestCase("{x}[y]z", 38)] // "{x}yz": x blir dobbel [TestCase("{street}", 12)] //extension double word [TestCase("[street]", 18)] //extension triple word @@ -18,6 +28,11 @@ public class ExtensionTests [TestCase("[quirky]", 66)] //extension triple word [TestCase("{OXyPHEnBUTaZoNE}", 82)] [TestCase("[OXyPHEnBUTaZoNE]", 123)] + + [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) public void ExtendedCriteriaTests(string word, int targetScore) { Assert.AreEqual(this.GetWordScore(word), targetScore);