From 4eb246c0aad7ac57a89632e90f853757ef6018f4 Mon Sep 17 00:00:00 2001 From: Oyvind Timian Dokk Husveg Date: Wed, 6 Aug 2025 11:59:08 +0200 Subject: [PATCH 1/5] Oyvind Husveg --- csharp-scrabble-challenge.Main/Program.cs | 10 +++- csharp-scrabble-challenge.Main/Scrabble.cs | 53 +++++++++++++++++-- .../ExtensionTests.cs | 1 + 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..a49113b 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,10 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); + +Console.WriteLine("Welcome to Scrabble! To exit press q and Enter."); + +while (true) +{ + Console.WriteLine("Enter a word and get the score! Add a {} or [] around a letter or multiple letters to multiply score by 2 or 3 respectively"); + Console.ReadLine(); + +} \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..caab1f0 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,60 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + public string _word; + private Dictionary _dictionary = new Dictionary + { + { 'A', 1 }, + { 'E', 1 }, + { 'I', 1 }, + { 'O', 1 }, + { 'U', 1 }, + { 'L', 1 }, + { 'N', 1 }, + { 'R', 1 }, + { 'S', 1 }, + { 'T', 1 }, + { 'D', 2 }, + { 'G', 2 }, + { 'B', 3 }, + { 'C', 3 }, + { 'M', 3 }, + { 'P', 3 }, + { 'F', 4 }, + { 'H', 4 }, + { 'V', 4 }, + { 'W', 4 }, + { 'Y', 4 }, + { 'K', 5 }, + { 'J', 8 }, + { 'X', 8 }, + { 'Q', 10 }, + { '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 sum = 0; + int BonusMultiplier = 1; // 1 -> no bonus, 2 -> double, 3 -> triple + foreach (var item in _word) + { + if (char.IsWhiteSpace(new String(item, 1), 0)) break; + + if (item == '{') BonusMultiplier = 2; + else if (item == '[') BonusMultiplier = 3; + else if (item == '}' || item == ']') BonusMultiplier = 1; + else + { + char letter = char.Parse(item.ToString().ToUpper()); + sum = sum + _dictionary[letter] * BonusMultiplier; + } + + } + return sum; } } } diff --git a/csharp-scrabble-challenge.Test/ExtensionTests.cs b/csharp-scrabble-challenge.Test/ExtensionTests.cs index 8eb37dc..d3b102f 100644 --- a/csharp-scrabble-challenge.Test/ExtensionTests.cs +++ b/csharp-scrabble-challenge.Test/ExtensionTests.cs @@ -18,6 +18,7 @@ public class ExtensionTests [TestCase("[quirky]", 66)] //extension triple word [TestCase("{OXyPHEnBUTaZoNE}", 82)] [TestCase("[OXyPHEnBUTaZoNE]", 123)] + [TestCase("A[Q]A", 32)] public void ExtendedCriteriaTests(string word, int targetScore) { Assert.AreEqual(this.GetWordScore(word), targetScore); From 513a4e5afa72161ade5abe1c7ddc5b2c957fa2a1 Mon Sep 17 00:00:00 2001 From: Oyvind Timian Dokk Husveg Date: Wed, 6 Aug 2025 12:53:56 +0200 Subject: [PATCH 2/5] Fixed for new test cases --- csharp-scrabble-challenge.Main/Program.cs | 9 +++++-- csharp-scrabble-challenge.Main/Scrabble.cs | 30 +++++++++++++++++---- csharp-scrabble-challenge.Test/CoreTests.cs | 10 +++++-- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index a49113b..6f0245f 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,10 +1,15 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Welcome to Scrabble! To exit press q and Enter."); +using csharp_scrabble_challenge.Main; +Console.WriteLine("Welcome to Scrabble! To exit press q and Enter."); while (true) { Console.WriteLine("Enter a word and get the score! Add a {} or [] around a letter or multiple letters to multiply score by 2 or 3 respectively"); - Console.ReadLine(); + string word = Console.ReadLine(); + Scrabble scrabble = new Scrabble(word); + scrabble.score(); + + } \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index caab1f0..8566921 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -47,14 +47,34 @@ public Scrabble(string word) public int score() { int sum = 0; + bool openCurly = false; + bool openHard = false; int BonusMultiplier = 1; // 1 -> no bonus, 2 -> double, 3 -> triple - foreach (var item in _word) + foreach (char item in _word) { - if (char.IsWhiteSpace(new String(item, 1), 0)) break; + if (char.IsWhiteSpace(item)) return 0; + if (char.IsNumber(item)) return 0; + if (item == '{') + { + openCurly = true; + BonusMultiplier = BonusMultiplier * 2; + } + else if (item == '[') + { + openHard = true; + BonusMultiplier = BonusMultiplier * 3; + } + else if (item == '}') + { - if (item == '{') BonusMultiplier = 2; - else if (item == '[') BonusMultiplier = 3; - else if (item == '}' || item == ']') BonusMultiplier = 1; + if (!openCurly) return 0; + BonusMultiplier = BonusMultiplier / 2; + } + else if (item == ']') + { + if (!openHard) return 0; + BonusMultiplier = BonusMultiplier / 3; + } else { char letter = char.Parse(item.ToString().ToUpper()); diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..ecdc8c0 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)] @@ -17,7 +21,9 @@ public class CoreTests [TestCase("street", 6)] public void WordScoreTests(string word, int targetScore) { - Assert.AreEqual(this.GetWordScore(word), targetScore); + Scrabble scrabble = new Scrabble(word); + + Assert.That(scrabble.score(), Is.EqualTo(targetScore)); } private int GetWordScore(string word) => new Scrabble(word).score(); From 0a410f685daf92be3a8970afae2da68bea7c2a9b Mon Sep 17 00:00:00 2001 From: Oyvind Timian Dokk Husveg Date: Wed, 6 Aug 2025 12:57:52 +0200 Subject: [PATCH 3/5] Added Console UI --- csharp-scrabble-challenge.Main/Program.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 6f0245f..cc6f282 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -2,14 +2,18 @@ using csharp_scrabble_challenge.Main; -Console.WriteLine("Welcome to Scrabble! To exit press q and Enter."); +Console.WriteLine("Welcome to Scrabble"); +Console.WriteLine("Enter a word and get the score! Add a {} or [] around a letter or multiple letters to multiply score by 2 or 3 respectively"); +Console.WriteLine("To exit press q and Enter."); while (true) { - Console.WriteLine("Enter a word and get the score! Add a {} or [] around a letter or multiple letters to multiply score by 2 or 3 respectively"); - string word = Console.ReadLine(); + string word = Console.ReadLine(); + if (word == "q") break; Scrabble scrabble = new Scrabble(word); - scrabble.score(); + Console.WriteLine($"{scrabble.score()}"); + +} -} \ No newline at end of file +Console.WriteLine("Exiting... Thanks for playing!"); \ No newline at end of file From 0d99a96c2ffb5f3609053d5fc6e0220b90e62d30 Mon Sep 17 00:00:00 2001 From: Oyvind Timian Dokk Husveg Date: Wed, 6 Aug 2025 13:26:18 +0200 Subject: [PATCH 4/5] Fixed some behavior --- csharp-scrabble-challenge.Main/Program.cs | 3 --- csharp-scrabble-challenge.Main/Scrabble.cs | 16 ++++++++-------- csharp-scrabble-challenge.Test/ExtensionTests.cs | 1 + 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index cc6f282..352f63f 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -7,13 +7,10 @@ Console.WriteLine("To exit press q and Enter."); while (true) { - string word = Console.ReadLine(); if (word == "q") break; Scrabble scrabble = new Scrabble(word); Console.WriteLine($"{scrabble.score()}"); - - } Console.WriteLine("Exiting... Thanks for playing!"); \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 8566921..43ea794 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -47,8 +47,8 @@ public Scrabble(string word) public int score() { int sum = 0; - bool openCurly = false; - bool openHard = false; + int openCurly = 0; + int openHard = 0; int BonusMultiplier = 1; // 1 -> no bonus, 2 -> double, 3 -> triple foreach (char item in _word) { @@ -56,23 +56,24 @@ public int score() if (char.IsNumber(item)) return 0; if (item == '{') { - openCurly = true; + openCurly++; BonusMultiplier = BonusMultiplier * 2; } else if (item == '[') { - openHard = true; + openHard++; BonusMultiplier = BonusMultiplier * 3; } else if (item == '}') { - - if (!openCurly) return 0; + if (openCurly == 0) return 0; + openCurly--; BonusMultiplier = BonusMultiplier / 2; } else if (item == ']') { - if (!openHard) return 0; + if (openHard == 0) return 0; + openHard--; BonusMultiplier = BonusMultiplier / 3; } else @@ -80,7 +81,6 @@ public int score() char letter = char.Parse(item.ToString().ToUpper()); sum = sum + _dictionary[letter] * BonusMultiplier; } - } return sum; } diff --git a/csharp-scrabble-challenge.Test/ExtensionTests.cs b/csharp-scrabble-challenge.Test/ExtensionTests.cs index d3b102f..7b07bb6 100644 --- a/csharp-scrabble-challenge.Test/ExtensionTests.cs +++ b/csharp-scrabble-challenge.Test/ExtensionTests.cs @@ -19,6 +19,7 @@ public class ExtensionTests [TestCase("{OXyPHEnBUTaZoNE}", 82)] [TestCase("[OXyPHEnBUTaZoNE]", 123)] [TestCase("A[Q]A", 32)] + [TestCase("{{A}}", 4)] public void ExtendedCriteriaTests(string word, int targetScore) { Assert.AreEqual(this.GetWordScore(word), targetScore); From fa7e7a9fba8d2e8ffafd8a5bccc2494b64d52f1a Mon Sep 17 00:00:00 2001 From: Oyvind Timian Dokk Husveg Date: Wed, 6 Aug 2025 13:59:32 +0200 Subject: [PATCH 5/5] Added check for too many open brackets --- csharp-scrabble-challenge.Main/Scrabble.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 43ea794..94e8e55 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -81,7 +81,10 @@ public int score() char letter = char.Parse(item.ToString().ToUpper()); sum = sum + _dictionary[letter] * BonusMultiplier; } + + } + if (openCurly > 0 || openHard > 0) return 0; return sum; } }