From 12e3b6d2bdfc4cfd78a2602e8e09266f67a016f1 Mon Sep 17 00:00:00 2001 From: abdi Date: Wed, 8 Jan 2025 15:38:53 +0100 Subject: [PATCH 1/2] all done! --- csharp-scrabble-challenge.Main/Scrabble.cs | 65 ++++++++++++++++++- .../ExtensionTests.cs | 8 +++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..ca94245 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,76 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + String word; public Scrabble(string word) - { + { //TODO: do something with the word variable + this.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 score = 0; + 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},}; + + string newWord= word.ToUpper(); + + for(int i = 0; i < newWord.Length; i++) + { + + if (map.ContainsKey(newWord[i])) + { + score += map[newWord[i]]; + } + else if (!(map.ContainsKey(newWord[i]))) + { + if (newWord[i] == '{') + { + i++; + List list = new List(); + while (newWord[i] != '}') + { + list.Add(newWord[i]); + i++; + } + for (int j = 0; j < list.Count; j++) + { + int newscore = map[list[j]]; + score += newscore * 2; + } + } + else if (newWord[i] == '[') + { + i++; + List list = new List(); + while (newWord[i] != ']') + { + list.Add(newWord[i]); + i++; + } + for (int j = 0; j < list.Count; j++) + { + int newscore = map[list[j]]; + score += newscore * 3; + } + + + } + } + + + } + return score; + + } + } } diff --git a/csharp-scrabble-challenge.Test/ExtensionTests.cs b/csharp-scrabble-challenge.Test/ExtensionTests.cs index 8eb37dc..57fa599 100644 --- a/csharp-scrabble-challenge.Test/ExtensionTests.cs +++ b/csharp-scrabble-challenge.Test/ExtensionTests.cs @@ -11,6 +11,14 @@ 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("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 From faa7dfd3bf9719d8bdb36a8923d4ef14838c9b18 Mon Sep 17 00:00:00 2001 From: abdi Date: Wed, 8 Jan 2025 21:23:41 +0100 Subject: [PATCH 2/2] extensions are done --- csharp-scrabble-challenge.Main/Scrabble.cs | 86 ++++++++++++------- .../ExtensionTests.cs | 9 +- 2 files changed, 63 insertions(+), 32 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index ca94245..2853ea3 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -13,14 +13,16 @@ public class Scrabble public Scrabble(string word) { //TODO: do something with the word variable - this.word = word; + this.word = word.ToUpper(); } public int score() { //TODO: score calculation code goes here - + + //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}, @@ -28,49 +30,71 @@ public int score() { 'S', 1},{ 'T', 1},{ 'U', 1},{ 'V', 4},{ 'X', 8},{ 'Y', 4}, { 'Z', 10},}; - string newWord= word.ToUpper(); + - for(int i = 0; i < newWord.Length; i++) + for(int i = 0; i < word.Length; i++) { - if (map.ContainsKey(newWord[i])) + if (map.ContainsKey(word[i])) { - score += map[newWord[i]]; + score += map[word[i]]; } - else if (!(map.ContainsKey(newWord[i]))) + else if (!(map.ContainsKey(word[i]))) { - if (newWord[i] == '{') + if (word[i] == '{' || word[i] == '[') { + int outerMulti = word[i] == '{' ? 2 : 3; + //skrive expected end bracket + char expectedOuterBracket= word[i] == '{' ? '}' : ']'; i++; - List list = new List(); - while (newWord[i] != '}') - { - list.Add(newWord[i]); - i++; - } - for (int j = 0; j < list.Count; j++) + + + while (word[i] != '}' && word[i] != ']') { - int newscore = map[list[j]]; - score += newscore * 2; + + 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++; } - } - else if (newWord[i] == '[') - { - i++; - List list = new List(); - while (newWord[i] != ']') - { - list.Add(newWord[i]); - i++; + } - for (int j = 0; j < list.Count; j++) + //her kan man sjekke bracket + if (expectedOuterBracket != word[i]) { - int newscore = map[list[j]]; - score += newscore * 3; + return 0; } - - + } + } diff --git a/csharp-scrabble-challenge.Test/ExtensionTests.cs b/csharp-scrabble-challenge.Test/ExtensionTests.cs index 57fa599..da462a0 100644 --- a/csharp-scrabble-challenge.Test/ExtensionTests.cs +++ b/csharp-scrabble-challenge.Test/ExtensionTests.cs @@ -13,7 +13,9 @@ 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 @@ -26,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);