From da5a22e78cb790ae93e662a3a6d0d47df9181f5a Mon Sep 17 00:00:00 2001 From: Tonnes Date: Mon, 27 Jan 2025 14:52:39 +0100 Subject: [PATCH 1/2] Done Core --- csharp-scrabble-challenge.Main/Scrabble.cs | 47 ++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..f8a8a49 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,56 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + private string _word; + public Dictionary scoreMap = 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 } + }; public Scrabble(string word) { - //TODO: do something with the word variable + _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; + foreach (char letter in this._word) + { + int letterScore = scoreMap[Char.ToUpper(letter)]; + + if (letterScore < 0) + { + return 0; + } + score += letterScore; + } + + return score; } } } From e19e12a0cce4aece93656470c2058da924fe360c Mon Sep 17 00:00:00 2001 From: Tonnes Date: Mon, 27 Jan 2025 15:15:07 +0100 Subject: [PATCH 2/2] Done 1 --- csharp-scrabble-challenge.Main/Scrabble.cs | 9 +++++++++ csharp-scrabble-challenge.Test/CoreTests.cs | 2 +- csharp-scrabble-challenge.Test/ExtensionTests.cs | 2 +- .../csharp-scrabble-challenge.Test.csproj | 6 +++--- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index f8a8a49..9bdfee1 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -46,9 +46,18 @@ public Scrabble(string word) public int score() { + if (_word == "") + { + return 0; + } int score = 0; foreach (char letter in this._word) { + if (!Char.IsLetter(letter)) + { + return 0; + } + int letterScore = scoreMap[Char.ToUpper(letter)]; if (letterScore < 0) diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..19b0ecc 100644 --- a/csharp-scrabble-challenge.Test/CoreTests.cs +++ b/csharp-scrabble-challenge.Test/CoreTests.cs @@ -17,7 +17,7 @@ public class CoreTests [TestCase("street", 6)] public void WordScoreTests(string word, int targetScore) { - Assert.AreEqual(this.GetWordScore(word), targetScore); + Assert.That(this.GetWordScore(word), Is.EqualTo(targetScore)); } private int GetWordScore(string word) => new Scrabble(word).score(); diff --git a/csharp-scrabble-challenge.Test/ExtensionTests.cs b/csharp-scrabble-challenge.Test/ExtensionTests.cs index 8eb37dc..b79e0dd 100644 --- a/csharp-scrabble-challenge.Test/ExtensionTests.cs +++ b/csharp-scrabble-challenge.Test/ExtensionTests.cs @@ -20,7 +20,7 @@ public class ExtensionTests [TestCase("[OXyPHEnBUTaZoNE]", 123)] public void ExtendedCriteriaTests(string word, int targetScore) { - Assert.AreEqual(this.GetWordScore(word), targetScore); + Assert.That(this.GetWordScore(word), Is.EqualTo(targetScore)); } private int GetWordScore(string word) => new Scrabble(word).score(); diff --git a/csharp-scrabble-challenge.Test/csharp-scrabble-challenge.Test.csproj b/csharp-scrabble-challenge.Test/csharp-scrabble-challenge.Test.csproj index 95b8d89..670b6d3 100644 --- a/csharp-scrabble-challenge.Test/csharp-scrabble-challenge.Test.csproj +++ b/csharp-scrabble-challenge.Test/csharp-scrabble-challenge.Test.csproj @@ -8,9 +8,9 @@ - - - + + +