From f5a2fb99916000cc3fb1a5cb244dbbbde5362334 Mon Sep 17 00:00:00 2001 From: Johan Reitan Date: Wed, 6 Aug 2025 13:32:43 +0200 Subject: [PATCH 1/3] double and triple words working --- .../MyDictionary.cs | 59 +++++++++++++++++++ csharp-scrabble-challenge.Main/Program.cs | 11 +++- csharp-scrabble-challenge.Main/Scrabble.cs | 54 ++++++++++++++++- 3 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 csharp-scrabble-challenge.Main/MyDictionary.cs diff --git a/csharp-scrabble-challenge.Main/MyDictionary.cs b/csharp-scrabble-challenge.Main/MyDictionary.cs new file mode 100644 index 0000000..707fe4f --- /dev/null +++ b/csharp-scrabble-challenge.Main/MyDictionary.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace csharp_scrabble_challenge.Main +{ + public class MyDictionary + { + + Dictionary dictionary; + + public MyDictionary() { + dictionary = new Dictionary(); + dictionary.Add('a', 1); + dictionary.Add('b', 3); + dictionary.Add('c', 3); + dictionary.Add('d', 2); + dictionary.Add('e', 1); + dictionary.Add('f', 4); + dictionary.Add('g', 2); + dictionary.Add('h', 4); + dictionary.Add('i', 1); + dictionary.Add('j', 8); + dictionary.Add('k', 5); + dictionary.Add('l', 1); + dictionary.Add('m', 3); + dictionary.Add('n', 1); + dictionary.Add('o', 1); + dictionary.Add('p', 3); + dictionary.Add('q', 10); + dictionary.Add('r', 1); + dictionary.Add('s', 1); + dictionary.Add('t', 1); + dictionary.Add('u', 1); + dictionary.Add('v', 4); + dictionary.Add('w', 4); + dictionary.Add('x', 8); + dictionary.Add('y', 4); + dictionary.Add('z', 10); + dictionary.Add(' ', 0); + } + + public int GetValue(char c) + { + if (!dictionary.ContainsKey(c)) + { + return -1; + } + return dictionary[char.ToLower(c)]; + } + + public bool KeyIsContained(char c) + { + return dictionary.ContainsKey(c); + } + } +} diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..2f06a02 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,11 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); + +using csharp_scrabble_challenge.Main; + +Console.WriteLine("enter a word: "); +string input = Console.ReadLine(); +Scrabble scr = new Scrabble(input); + +int score = scr.score(); + +Console.WriteLine($"{score}"); diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..8e9adfa 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -7,17 +7,65 @@ namespace csharp_scrabble_challenge.Main { + + public class Scrabble { + public MyDictionary myDictionary = new MyDictionary(); + + private string _word; + + + + public Scrabble(string word) - { - //TODO: do something with the word variable + { + _word = word; } public int score() { + int _score = 0; + int _multiplier = 1; //TODO: score calculation code goes here - throw new NotImplementedException(); //TODO: Remove this line when the code has been written + //throw new NotImplementedException(); //TODO: Remove this line when the code has been written + foreach (var c in _word) + { + int tempScore = 0; + + if (c == '{') + { + _multiplier = 2; + } + else if (c == '}') + { + _multiplier = 1; + } + else if (c == '[') + { + _multiplier = 3; + } + else if (c == ']') + { + _multiplier = 1; + } + else if (myDictionary.KeyIsContained(char.ToLower(c))) + { + tempScore = myDictionary.GetValue(char.ToLower(c)) * _multiplier; + } + else + { + return 0; + } + _score += tempScore; + } + + + + + + return _score; } } } + From 5571107e2bac917c641ada25d3e507f669f7b31f Mon Sep 17 00:00:00 2001 From: Johan Reitan Date: Wed, 6 Aug 2025 14:54:16 +0200 Subject: [PATCH 2/3] attempting recursion --- .../MyDictionary.cs | 2 +- csharp-scrabble-challenge.Main/Scrabble.cs | 63 ++++++++++++++++++- csharp-scrabble-challenge.Test/CoreTests.cs | 6 +- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/csharp-scrabble-challenge.Main/MyDictionary.cs b/csharp-scrabble-challenge.Main/MyDictionary.cs index 707fe4f..64dc804 100644 --- a/csharp-scrabble-challenge.Main/MyDictionary.cs +++ b/csharp-scrabble-challenge.Main/MyDictionary.cs @@ -46,7 +46,7 @@ public int GetValue(char c) { if (!dictionary.ContainsKey(c)) { - return -1; + return 0; } return dictionary[char.ToLower(c)]; } diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index 8e9adfa..ff7a7be 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -14,6 +14,7 @@ public class Scrabble public MyDictionary myDictionary = new MyDictionary(); private string _word; + private int multiplier = 1; @@ -22,31 +23,48 @@ public Scrabble(string word) { _word = word; } - + /* public int score() { int _score = 0; int _multiplier = 1; + + int open3x = 0; + int open2x = 0; //TODO: score calculation code goes here //throw new NotImplementedException(); //TODO: Remove this line when the code has been written foreach (var c in _word) { + int tempScore = 0; + if (c == '{') { + open2x++; _multiplier = 2; } else if (c == '}') { + if (open2x == 0) + { + return 0; + } + open2x--; _multiplier = 1; } else if (c == '[') { + open3x++; _multiplier = 3; } else if (c == ']') { + if (open3x == 0) + { + return 0; + } + open3x--; _multiplier = 1; } else if (myDictionary.KeyIsContained(char.ToLower(c))) @@ -66,6 +84,49 @@ public int score() return _score; } + */ + + + public int score() + { + + //TODO: score calculation code goes here + //throw new NotImplementedException(); //TODO: Remove this line when the code has been written + return ScoreRecursive(_word); + } + public int ScoreRecursive(string subString) + { + if (subString == null) return 0; + else if (subString.Length <= 1) + { + return myDictionary.GetValue(subString[0]); + } + else if (subString[0] == '{') + { + multiplier *= 2; + return ScoreRecursive(subString.Substring(1)); + } + else if (subString[0] == '[') + { + multiplier *= 3; + return ScoreRecursive(subString.Substring(1)); + } + else if (subString[0] == '}') + { + multiplier /= 2; + return ScoreRecursive(subString.Substring(1)); + } + else if (subString[0] == ']') + { + multiplier /= 3; + return ScoreRecursive(subString.Substring(1)); + } + + else + { + return multiplier * myDictionary.GetValue(subString[0]) + ScoreRecursive(subString.Substring(1)); + } + } } } diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..57dff72 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)] From 63c0da3d8ab5c973e9f9e97b1d4a69c9cd2f651f Mon Sep 17 00:00:00 2001 From: Johan Reitan Date: Wed, 6 Aug 2025 15:28:55 +0200 Subject: [PATCH 3/3] all good, no recursion --- csharp-scrabble-challenge.Main/Scrabble.cs | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index ff7a7be..a046566 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -21,9 +21,10 @@ public class Scrabble public Scrabble(string word) { - _word = word; + _word = word.ToLower(); } - /* + + public int score() { int _score = 0; @@ -42,7 +43,7 @@ public int score() if (c == '{') { open2x++; - _multiplier = 2; + _multiplier *= 2; } else if (c == '}') { @@ -51,12 +52,12 @@ public int score() return 0; } open2x--; - _multiplier = 1; + _multiplier /= 2; } else if (c == '[') { open3x++; - _multiplier = 3; + _multiplier *= 3; } else if (c == ']') { @@ -65,7 +66,7 @@ public int score() return 0; } open3x--; - _multiplier = 1; + _multiplier /= 3; } else if (myDictionary.KeyIsContained(char.ToLower(c))) { @@ -84,9 +85,10 @@ public int score() return _score; } - */ - + + + /* public int score() { @@ -127,6 +129,13 @@ public int ScoreRecursive(string subString) return multiplier * myDictionary.GetValue(subString[0]) + ScoreRecursive(subString.Substring(1)); } } - } + + + + */ + + + +} }