From 4490151c2d64527cb70e1e7b2f239a11564b691c Mon Sep 17 00:00:00 2001 From: Stian Forren Date: Wed, 6 Aug 2025 14:51:35 +0200 Subject: [PATCH 1/3] Stian Forren --- csharp-scrabble-challenge.Main/Scrabble.cs | 104 +++++++++++++++++++- csharp-scrabble-challenge.Test/CoreTests.cs | 15 +++ 2 files changed, 117 insertions(+), 2 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..3f53752 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -1,7 +1,10 @@ using System; using System.Collections.Generic; +using System.ComponentModel.Design; +using System.Diagnostics.Tracing; using System.Linq; using System.Reflection.Metadata.Ecma335; +using System.Runtime.ConstrainedExecution; using System.Text; using System.Threading.Tasks; @@ -9,15 +12,112 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + + private Dictionary charScore = new Dictionary(); + private Dictionary bracketPair = new Dictionary { { '{', '}' }, { '[', ']'} }; + private List legalBrackets = new List { '{', '}', '[', ']' }; + private string _word; + private int _mulitplier = 1; + public Scrabble(string word) - { + { //TODO: do something with the word variable + addAlphabetToDict(); + word = word.ToUpper(); + _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 finalScore = 0; + int open = 0; + int close = 0; + string subString = _word; + int _wordMultiplier = 1; + + if (_word.Length == 0) { return finalScore; } + if (checkLegalWord(_word)) + { + if (_word[0] == '{' && _word[_word.Length - 1] == '}') + { + subString = _word.Substring(1, _word.Length - 2); + + if (_word[2] == '}') {_wordMultiplier = 1; _mulitplier = 2; } + else { _wordMultiplier = 2; } + } + else if (_word[0] == '[' && _word[_word.Length - 1] == ']') + { + subString = _word.Substring(1, _word.Length - 2); + _wordMultiplier = 3; + } + + foreach (char ch in subString) + { + if (ch == '{') { _mulitplier += 1; open++; } + else if(ch == '}') { if (_mulitplier > 1) { _mulitplier -= 1; } close++; } + else if (ch == '[') { _mulitplier += 2; open++; } + else if (ch == ']') { if (_mulitplier > 2) { _mulitplier -= 2; } close++; } + + finalScore += charScore[ch] * _mulitplier; + } + } + if (open != close) + { + return 0; + } + return finalScore * _wordMultiplier; + } + + + + private bool checkLegalWord(string word) + { + foreach (char ch in word) + { + if (!charScore.ContainsKey(ch)) + { + return false; + } + } + return true; + } + private void addAlphabetToDict() + { + charScore.Add('A', 1); + charScore.Add('E', 1); + charScore.Add('I', 1); + charScore.Add('O', 1); + charScore.Add('U', 1); + charScore.Add('L', 1); + charScore.Add('N', 1); + charScore.Add('R', 1); + charScore.Add('S', 1); + charScore.Add('T', 1); + charScore.Add('D', 2); + charScore.Add('G', 2); + charScore.Add('B', 3); + charScore.Add('C', 3); + charScore.Add('M', 3); + charScore.Add('P', 3); + charScore.Add('F', 4); + charScore.Add('H', 4); + charScore.Add('V', 4); + charScore.Add('W', 4); + charScore.Add('Y', 4); + charScore.Add('K', 5); + charScore.Add('J', 8); + charScore.Add('X', 8); + charScore.Add('Q', 10); + charScore.Add('Z', 10); + charScore.Add('{', 0); + charScore.Add('}', 0); + charScore.Add('[', 0); + charScore.Add(']', 0); + } + + + } } diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..f182409 100644 --- a/csharp-scrabble-challenge.Test/CoreTests.cs +++ b/csharp-scrabble-challenge.Test/CoreTests.cs @@ -15,6 +15,21 @@ public class CoreTests [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)] + [TestCase("\n\r\t\b\f", 0)] + [TestCase("a", 1)] + [TestCase("f", 4)] + [TestCase("OXyPHEnBUTaZoNE", 41)] + [TestCase("quirky", 22)] + [TestCase("street", 6)] public void WordScoreTests(string word, int targetScore) { Assert.AreEqual(this.GetWordScore(word), targetScore); From 366abe1193ae814844ece07959d111dbda93f331 Mon Sep 17 00:00:00 2001 From: Stian Forren Date: Wed, 6 Aug 2025 15:18:50 +0200 Subject: [PATCH 2/3] Stian Forren --- csharp-scrabble-challenge.Main/Scrabble.cs | 59 +++++++++++++++------ csharp-scrabble-challenge.Test/CoreTests.cs | 3 ++ 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 3f53752..0169d9c 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -31,11 +31,11 @@ public int score() { //TODO: score calculation code goes here int finalScore = 0; - int open = 0; - int close = 0; string subString = _word; int _wordMultiplier = 1; + char currentOpenBracket = ' '; + if (_word.Length == 0) { return finalScore; } if (checkLegalWord(_word)) { @@ -43,29 +43,58 @@ public int score() { subString = _word.Substring(1, _word.Length - 2); - if (_word[2] == '}') {_wordMultiplier = 1; _mulitplier = 2; } + if (subString[1] == '}') + { + _wordMultiplier = 1; + _mulitplier = 2; + currentOpenBracket = '{'; + } else { _wordMultiplier = 2; } } else if (_word[0] == '[' && _word[_word.Length - 1] == ']') - { - subString = _word.Substring(1, _word.Length - 2); - _wordMultiplier = 3; - } + { + subString = _word.Substring(1, _word.Length - 2); + if (subString[1] == ']') + { + _wordMultiplier = 1; + _mulitplier = 3; + currentOpenBracket = '['; + } + else { _wordMultiplier = 3; } + } foreach (char ch in subString) { - if (ch == '{') { _mulitplier += 1; open++; } - else if(ch == '}') { if (_mulitplier > 1) { _mulitplier -= 1; } close++; } - else if (ch == '[') { _mulitplier += 2; open++; } - else if (ch == ']') { if (_mulitplier > 2) { _mulitplier -= 2; } close++; } + //if (ch == '{') { _mulitplier += 1; open++; } + //else if(ch == '}') { if (_mulitplier > 1) { _mulitplier -= 1; } close++; } + //else if (ch == '[') { _mulitplier += 2; open++; } + //else if (ch == ']') { if (_mulitplier > 2) { _mulitplier -= 2; } close++; } + + if (ch == '{') + { + if (currentOpenBracket == ' ') { _mulitplier += 1; currentOpenBracket = '{'; } + else { return 0; } + } + else if (ch == '}') + { + if (currentOpenBracket == '{') { _mulitplier -= 1; currentOpenBracket = ' '; } + else { return 0; } + } + else if (ch == '[') + { + if (currentOpenBracket == ' ') { _mulitplier += 2; currentOpenBracket = '['; } + else { return 0; } + } + else if (ch == ']') + { + if (currentOpenBracket == ']') { _mulitplier -= 2; currentOpenBracket = ' '; } + else { return 0; } + } finalScore += charScore[ch] * _mulitplier; } } - if (open != close) - { - return 0; - } + return finalScore * _wordMultiplier; } diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f182409..c4f5fc0 100644 --- a/csharp-scrabble-challenge.Test/CoreTests.cs +++ b/csharp-scrabble-challenge.Test/CoreTests.cs @@ -19,6 +19,9 @@ public class CoreTests [TestCase("[{h}o1s{e}]", 0)] // error case (zero for errors) [TestCase("{h}ous{e}", 13)] + [TestCase("{h}{{ous{e}", 0)] + [TestCase("{h}}ous{e}", 0)] + [TestCase("{h}{o]us{e}", 0)] [TestCase("[{h}ous{e}]", 39)] [TestCase("[h}ous{e}]", 0)] //Error case (zero for errors) [TestCase("", 0)] From 682c635c57d7ebce53c85209b89c3a63c089969f Mon Sep 17 00:00:00 2001 From: Stian Forren Date: Wed, 6 Aug 2025 15:48:49 +0200 Subject: [PATCH 3/3] Stian Forren --- csharp-scrabble-challenge.Main/Scrabble.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 0169d9c..0d16779 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -14,8 +14,6 @@ public class Scrabble { private Dictionary charScore = new Dictionary(); - private Dictionary bracketPair = new Dictionary { { '{', '}' }, { '[', ']'} }; - private List legalBrackets = new List { '{', '}', '[', ']' }; private string _word; private int _mulitplier = 1; @@ -65,11 +63,6 @@ public int score() foreach (char ch in subString) { - //if (ch == '{') { _mulitplier += 1; open++; } - //else if(ch == '}') { if (_mulitplier > 1) { _mulitplier -= 1; } close++; } - //else if (ch == '[') { _mulitplier += 2; open++; } - //else if (ch == ']') { if (_mulitplier > 2) { _mulitplier -= 2; } close++; } - if (ch == '{') { if (currentOpenBracket == ' ') { _mulitplier += 1; currentOpenBracket = '{'; } @@ -98,9 +91,6 @@ public int score() return finalScore * _wordMultiplier; } - - - private bool checkLegalWord(string word) { foreach (char ch in word) @@ -145,8 +135,5 @@ private void addAlphabetToDict() charScore.Add('[', 0); charScore.Add(']', 0); } - - - } }