From 0114e1c8ccac5ae61e40cb03325282a725376dab Mon Sep 17 00:00:00 2001 From: Snorre Aldstedt Date: Wed, 6 Aug 2025 13:24:32 +0200 Subject: [PATCH 1/5] pass all core-tests and extension-test --- csharp-scrabble-challenge.Main/Program.cs | 7 ++ csharp-scrabble-challenge.Main/Scrabble.cs | 103 ++++++++++++++++++++- 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..85be4ae 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,9 @@ // See https://aka.ms/new-console-template for more information +using csharp_scrabble_challenge.Main; Console.WriteLine("Hello, World!"); + +Scrabble scrabble = new Scrabble("tEst123"); +scrabble.Test(); + +Scrabble scrabble2 = new Scrabble("\"\\n\\r\\t\\b\\f\"Halloooo"); +scrabble2.Test(); \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..6f42101 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -9,15 +9,114 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + private Dictionary _scores = 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}, + {'{', 0}, + {'}', 0}, + {'[', 0}, + {']', 0} + }; + private string _word = ""; + private bool OnlyHasValidChars(string str) + { + string lowerStr = str.ToLower(); + foreach (char c in lowerStr) + { + if (!_scores.ContainsKey(c)) + { + return false; + } + } + return true; + } + + public string RemoveDashes(string withDashes) + { if (!withDashes.Contains("\\")) { return withDashes; } + StringBuilder sb = new StringBuilder(withDashes); + if (withDashes.Contains("\\")) + { + int indexOfDash = withDashes.IndexOf('\\'); + //string notValidString = withDashes.Substring(indexOfDash, 2); + sb.Remove(indexOfDash, 2); + RemoveDashes(sb.ToString()); + } + return ""; + } + + private string RemoveNonValidChars(string str) + { + StringBuilder stringBuilder = new StringBuilder(); + foreach(char c in str.ToLower()) + { + if (_scores.ContainsKey(c)) + { + stringBuilder.Append(c); + } + } + return stringBuilder.ToString(); + } public Scrabble(string word) - { + { + _word = word; //TODO: do something with the word variable } + public void Test() + { + Console.WriteLine(RemoveDashes(_word)); + Console.WriteLine(RemoveNonValidChars(_word)); + } + public int score() { + int _score = 0; + int _multiplier = 1; + string notDashes = RemoveDashes(_word); + string cleanString = RemoveNonValidChars(notDashes); + foreach(char c in cleanString) + { + if (c == '{') + { + _multiplier = 2; + } + else if (c == '[') + { + _multiplier = 3; + } + else if (c == '}' || c == ']') + { + _multiplier = 1; + } + _score += _scores[c] * _multiplier; + } + return _score; //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written } } } From aff0828644723a17e3a3058885cad837f5eb15c1 Mon Sep 17 00:00:00 2001 From: Snorre Aldstedt Date: Wed, 6 Aug 2025 14:16:58 +0200 Subject: [PATCH 2/5] Fix handling of brackets --- csharp-scrabble-challenge.Main/Program.cs | 13 ++++-- csharp-scrabble-challenge.Main/Scrabble.cs | 44 +++++++++++++++++---- csharp-scrabble-challenge.Test/CoreTests.cs | 13 ++++++ 3 files changed, 60 insertions(+), 10 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 85be4ae..6cdd4cf 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,9 +1,16 @@ // See https://aka.ms/new-console-template for more information using csharp_scrabble_challenge.Main; -Console.WriteLine("Hello, World!"); - +Console.WriteLine("Welcome to Scrabble!"); +/* Scrabble scrabble = new Scrabble("tEst123"); scrabble.Test(); Scrabble scrabble2 = new Scrabble("\"\\n\\r\\t\\b\\f\"Halloooo"); -scrabble2.Test(); \ No newline at end of file +scrabble2.Test(); + +while (true) +{ + Console.WriteLine("Write a Scrabble-word to get it's score(q to quit):"); + +} +*/ \ No newline at end of file diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 6f42101..9936a4a 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -97,24 +97,54 @@ public int score() { int _score = 0; int _multiplier = 1; - string notDashes = RemoveDashes(_word); - string cleanString = RemoveNonValidChars(notDashes); - foreach(char c in cleanString) + int _balanced_curly = 0; + int _balanced_square = 0; + + //string notDashes = RemoveDashes(_word); + //string cleanString = RemoveNonValidChars(notDashes); + if (!OnlyHasValidChars(_word.ToLower())) { return 0; } + foreach(char c in _word.ToLower()) { if (c == '{') { - _multiplier = 2; + _balanced_curly++; + _multiplier *= 2; } else if (c == '[') { - _multiplier = 3; + _balanced_square++; + _multiplier *= 3; + } + else if(c == ']') + { + if (_multiplier%3 != 0) { return 0;} + _balanced_square--; + _multiplier /= 3; + + } + else if (c == '}') + { + if (_multiplier % 2 != 0) { return 0; } + _balanced_curly--; + _multiplier /= 2; + + } + /*else if (c == '}' && _multiplier != 3) + { + return 0; + } + else if (c == ')' && _multiplier != 3) + { + return 0; } else if (c == '}' || c == ']') { _multiplier = 1; - } - _score += _scores[c] * _multiplier; + }*/ + + _score += _scores[c] * _multiplier; } + if (_balanced_square != 0 || _balanced_curly != 0) { return 0; } return _score; //TODO: score calculation code goes here } diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..b6330d6 100644 --- a/csharp-scrabble-challenge.Test/CoreTests.cs +++ b/csharp-scrabble-challenge.Test/CoreTests.cs @@ -6,6 +6,19 @@ namespace csharp_scrabble_challenge.Test [TestFixture] public class CoreTests { + [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("[{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)] From dc8f2f3785f3f760dd8d388aaeb955273ba05a8b Mon Sep 17 00:00:00 2001 From: Snorre Aldstedt Date: Wed, 6 Aug 2025 14:22:55 +0200 Subject: [PATCH 3/5] Clean up comments and redundant code --- csharp-scrabble-challenge.Main/Scrabble.cs | 48 +--------------------- 1 file changed, 1 insertion(+), 47 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 9936a4a..ad851b0 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -56,43 +56,12 @@ private bool OnlyHasValidChars(string str) return true; } - public string RemoveDashes(string withDashes) - { if (!withDashes.Contains("\\")) { return withDashes; } - StringBuilder sb = new StringBuilder(withDashes); - if (withDashes.Contains("\\")) - { - int indexOfDash = withDashes.IndexOf('\\'); - //string notValidString = withDashes.Substring(indexOfDash, 2); - sb.Remove(indexOfDash, 2); - RemoveDashes(sb.ToString()); - } - return ""; - } - - private string RemoveNonValidChars(string str) - { - StringBuilder stringBuilder = new StringBuilder(); - foreach(char c in str.ToLower()) - { - if (_scores.ContainsKey(c)) - { - stringBuilder.Append(c); - } - } - return stringBuilder.ToString(); - } public Scrabble(string word) { _word = word; //TODO: do something with the word variable } - public void Test() - { - Console.WriteLine(RemoveDashes(_word)); - Console.WriteLine(RemoveNonValidChars(_word)); - } - public int score() { int _score = 0; @@ -100,8 +69,6 @@ public int score() int _balanced_curly = 0; int _balanced_square = 0; - //string notDashes = RemoveDashes(_word); - //string cleanString = RemoveNonValidChars(notDashes); if (!OnlyHasValidChars(_word.ToLower())) { return 0; } foreach(char c in _word.ToLower()) { @@ -129,20 +96,7 @@ public int score() _multiplier /= 2; } - /*else if (c == '}' && _multiplier != 3) - { - return 0; - } - else if (c == ')' && _multiplier != 3) - { - return 0; - } - else if (c == '}' || c == ']') - { - _multiplier = 1; - }*/ - - _score += _scores[c] * _multiplier; + _score += _scores[c] * _multiplier; } if (_balanced_square != 0 || _balanced_curly != 0) { return 0; } return _score; From 83fe5d1f41d05e5a695783e5097ac4a47bd46016 Mon Sep 17 00:00:00 2001 From: Snorre Aldstedt Date: Wed, 6 Aug 2025 14:36:36 +0200 Subject: [PATCH 4/5] Add very simple console UI --- csharp-scrabble-challenge.Main/Program.cs | 15 ++++++++------- csharp-scrabble-challenge.Main/Scrabble.cs | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 6cdd4cf..f654986 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,16 +1,17 @@ // See https://aka.ms/new-console-template for more information using csharp_scrabble_challenge.Main; Console.WriteLine("Welcome to Scrabble!"); -/* -Scrabble scrabble = new Scrabble("tEst123"); -scrabble.Test(); -Scrabble scrabble2 = new Scrabble("\"\\n\\r\\t\\b\\f\"Halloooo"); -scrabble2.Test(); while (true) { Console.WriteLine("Write a Scrabble-word to get it's score(q to quit):"); + string input = Console.ReadLine(); + if (input == "q") + { + break; + } -} -*/ \ No newline at end of file + Scrabble scrabble = new Scrabble(input); + Console.WriteLine(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 ad851b0..951db27 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -98,7 +98,7 @@ public int score() } _score += _scores[c] * _multiplier; } - if (_balanced_square != 0 || _balanced_curly != 0) { return 0; } + if (_balanced_square != 0 || _balanced_curly != 0 || _multiplier != 1) { return 0; } return _score; //TODO: score calculation code goes here } From 569dc0545b586e8a5be1aaa5e4914636dd63326f Mon Sep 17 00:00:00 2001 From: Snorre Aldstedt Date: Wed, 6 Aug 2025 15:00:02 +0200 Subject: [PATCH 5/5] Fix bug so altering type of brackets doesnt work --- csharp-scrabble-challenge.Main/Scrabble.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 951db27..f39cc2b 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -68,6 +68,8 @@ public int score() int _multiplier = 1; int _balanced_curly = 0; int _balanced_square = 0; + char _opening_bracket = ' '; + if (!OnlyHasValidChars(_word.ToLower())) { return 0; } foreach(char c in _word.ToLower()) @@ -76,24 +78,28 @@ public int score() { _balanced_curly++; _multiplier *= 2; + _opening_bracket = '{'; } else if (c == '[') { _balanced_square++; _multiplier *= 3; + _opening_bracket = '['; } else if(c == ']') { - if (_multiplier%3 != 0) { return 0;} + if (_multiplier%3 != 0 || _opening_bracket == '{') { return 0;} _balanced_square--; _multiplier /= 3; + _opening_bracket = ' '; } else if (c == '}') { - if (_multiplier % 2 != 0) { return 0; } + if (_multiplier % 2 != 0 || _opening_bracket == '[') { return 0; } _balanced_curly--; _multiplier /= 2; + _opening_bracket = ' '; } _score += _scores[c] * _multiplier;