From 05abbc538fd93bd82569755c0b7d1cc85a869525 Mon Sep 17 00:00:00 2001 From: Jakub Mroz Date: Wed, 6 Aug 2025 11:37:38 +0200 Subject: [PATCH 1/4] feat: scrabble base case --- csharp-scrabble-challenge.Main/Scrabble.cs | 48 +++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..1213ed0 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,61 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + private Dictionary letterToScoreMap = new() + { + {'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} + }; + + private string processedWord; + public Scrabble(string word) { //TODO: do something with the word variable + + bool isValid = word.All(char.IsLetter); + + + processedWord = word.ToLower(); + } public int score() { + int totalScore = 0; + //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + foreach (var letter in processedWord) + { + letterToScoreMap.TryGetValue(letter, out var score); + totalScore += score; + } + + return totalScore; } } } From d511d6af34bf8c8f848664cf0bd84efdf0582d34 Mon Sep 17 00:00:00 2001 From: Jakub Mroz Date: Wed, 6 Aug 2025 12:59:32 +0200 Subject: [PATCH 2/4] feat: extended criteria implemented --- csharp-scrabble-challenge.Main/Program.cs | 13 ++++++ csharp-scrabble-challenge.Main/Scrabble.cs | 48 +++++++++++++++++++--- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..1ef17d6 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,15 @@ // See https://aka.ms/new-console-template for more information +using csharp_scrabble_challenge.Main; +using System.IO; + Console.WriteLine("Hello, World!"); + +Scrabble s = new Scrabble("{street}"); +var score = s.score(); + +Console.WriteLine($"score = {score}"); + +Scrabble s2 = new Scrabble("[street]"); +var score2 = s2.score(); + +Console.WriteLine($"score = {score2}"); \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 1213ed0..9e72c91 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -41,14 +41,25 @@ public class Scrabble private string processedWord; - public Scrabble(string word) - { - //TODO: do something with the word variable + // [] triple points + // {} double points + + private const char startDoublePoints = '{'; + private const char endDoublePoints = '}'; + + private const char startTriplePoints = '['; + private const char endTriplePoints = ']'; - bool isValid = word.All(char.IsLetter); + bool isDoublePointsActive = false; + bool isTriplePointsActive = false; + int doublePointsMultiplier = 2; + int triplePointsMultiplier = 3; - processedWord = word.ToLower(); + public Scrabble(string word) + { + processedWord = word.ToLowerInvariant(); + // assumes brackets are valid, ie no "{street{" or mixing double with triple points } @@ -56,10 +67,35 @@ public int score() { int totalScore = 0; - //TODO: score calculation code goes here foreach (var letter in processedWord) { + + switch (letter) + { + case startDoublePoints: + isDoublePointsActive = true; + break; + case endDoublePoints: + isDoublePointsActive = false; + break; + case startTriplePoints: + isTriplePointsActive = true; + break; + case endTriplePoints: + isTriplePointsActive = false; + break; + + } + letterToScoreMap.TryGetValue(letter, out var score); + + if (isDoublePointsActive) + score *= doublePointsMultiplier; + + if (isTriplePointsActive) + score *= triplePointsMultiplier; + + totalScore += score; } From 5d701f9e9586ea34d05a6a2dab9020fb0e2d42f7 Mon Sep 17 00:00:00 2001 From: Jakub Mroz Date: Wed, 6 Aug 2025 13:54:42 +0200 Subject: [PATCH 3/4] feat: new test cases --- csharp-scrabble-challenge.Main/Program.cs | 9 ++----- csharp-scrabble-challenge.Main/Scrabble.cs | 24 +++++++++++++++++++ .../ExtensionTests.cs | 18 ++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 1ef17d6..3791da9 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -4,12 +4,7 @@ Console.WriteLine("Hello, World!"); -Scrabble s = new Scrabble("{street}"); +Scrabble s = new Scrabble("[h}ous{e}]"); var score = s.score(); -Console.WriteLine($"score = {score}"); - -Scrabble s2 = new Scrabble("[street]"); -var score2 = s2.score(); - -Console.WriteLine($"score = {score2}"); \ No newline at end of file +Console.WriteLine($"score = {score}"); \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 9e72c91..481c2fb 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata.Ecma335; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; @@ -56,6 +57,8 @@ public class Scrabble int doublePointsMultiplier = 2; int triplePointsMultiplier = 3; + bool endedBonusBeforeStarting = false; + public Scrabble(string word) { processedWord = word.ToLowerInvariant(); @@ -75,16 +78,31 @@ public int score() case startDoublePoints: isDoublePointsActive = true; break; + case endDoublePoints: + if(!isDoublePointsActive) + endedBonusBeforeStarting = true; + isDoublePointsActive = false; break; + case startTriplePoints: isTriplePointsActive = true; break; + case endTriplePoints: + if (isTriplePointsActive == false) + endedBonusBeforeStarting = true; + isTriplePointsActive = false; break; + default: + bool isValid = char.IsLetter(letter); + if (!isValid) + return 0; + + break; } letterToScoreMap.TryGetValue(letter, out var score); @@ -99,6 +117,12 @@ public int score() totalScore += score; } + if (isDoublePointsActive || isTriplePointsActive) + return 0; + + if (endedBonusBeforeStarting) + return 0; + return totalScore; } } diff --git a/csharp-scrabble-challenge.Test/ExtensionTests.cs b/csharp-scrabble-challenge.Test/ExtensionTests.cs index 8eb37dc..07dcbac 100644 --- a/csharp-scrabble-challenge.Test/ExtensionTests.cs +++ b/csharp-scrabble-challenge.Test/ExtensionTests.cs @@ -14,10 +14,28 @@ public class ExtensionTests [TestCase("{street}", 12)] //extension double word [TestCase("[street]", 18)] //extension triple word + [TestCase("{s}treet", 7)] //extension double word + [TestCase("[st]reet", 10)] //extension triple word + [TestCase("[st]ree{t}", 11)] //extension triple word + [TestCase("[st}ree[t}", 0)] //extension triple word [TestCase("{quirky}", 44)] //extension double word [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) + [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("]street[", 0)] public void ExtendedCriteriaTests(string word, int targetScore) { Assert.AreEqual(this.GetWordScore(word), targetScore); From 752cda18cfb8cc61298c3f0a9c4159624052b39e Mon Sep 17 00:00:00 2001 From: Jakub Mroz Date: Wed, 6 Aug 2025 14:01:12 +0200 Subject: [PATCH 4/4] chore: comment cleanup --- csharp-scrabble-challenge.Main/Scrabble.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 481c2fb..bfc480e 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -62,8 +62,6 @@ public class Scrabble public Scrabble(string word) { processedWord = word.ToLowerInvariant(); - // assumes brackets are valid, ie no "{street{" or mixing double with triple points - } public int score()