From df96ceec957099c1380e125634cb92214d93066b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elisabeth=20R=C3=B8ysland?= Date: Thu, 7 Aug 2025 08:19:36 +0200 Subject: [PATCH] =?UTF-8?q?All=20tests=20passed=20-=20Elisabeth=20R=C3=B8y?= =?UTF-8?q?sland?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- csharp-scrabble-challenge.Main/Program.cs | 7 +- csharp-scrabble-challenge.Main/Scrabble.cs | 132 +++++++++++++++++- .../ScrabbleDictionary.cs | 62 ++++++++ csharp-scrabble-challenge.Test/CoreTests.cs | 26 +++- 4 files changed, 222 insertions(+), 5 deletions(-) create mode 100644 csharp-scrabble-challenge.Main/ScrabbleDictionary.cs diff --git a/csharp-scrabble-challenge.Main/Program.cs b/csharp-scrabble-challenge.Main/Program.cs index 3751555..41ef282 100644 --- a/csharp-scrabble-challenge.Main/Program.cs +++ b/csharp-scrabble-challenge.Main/Program.cs @@ -1,2 +1,7 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +//Console.WriteLine("Hello, World!"); + +using csharp_scrabble_challenge.Main; + +Scrabble scrabble = new Scrabble("Test"); +scrabble.score(); diff --git a/csharp-scrabble-challenge.Main/Scrabble.cs b/csharp-scrabble-challenge.Main/Scrabble.cs index c1ea013..c895ad6 100644 --- a/csharp-scrabble-challenge.Main/Scrabble.cs +++ b/csharp-scrabble-challenge.Main/Scrabble.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Tracing; using System.Linq; using System.Reflection.Metadata.Ecma335; using System.Text; @@ -9,15 +10,142 @@ namespace csharp_scrabble_challenge.Main { public class Scrabble { + private string _word; public Scrabble(string word) - { + { //TODO: do something with the word variable + _word = word.ToUpper(); } public int score() { //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 + + ScrabbleDictionary scrabbleDictionary = new ScrabbleDictionary(); + + int sum = 0; + int tempValue = 0; + bool doubleLetter = false; + bool tripleLetter = false; + bool possibledoubleWord = false; + bool possibletripleWord = false; + int countIndex = 0; + int indexStartD = -1; + int indexStartT = -1; + + foreach (var letter in _word) + { + Console.WriteLine($"We are on letter: {letter}"); + + if (letter == '{') + { + if (countIndex == 0) + { + possibledoubleWord = true; + countIndex++; + continue; + } + + doubleLetter = true; + indexStartD = countIndex; + countIndex++; + continue; + } + + if (letter == '[') + { + if (countIndex == 0) + { + possibletripleWord = true; + countIndex++; + continue; + } + + tripleLetter = true; + indexStartT = countIndex; + countIndex++; + continue; + } + + if (letter == '}') + { + doubleLetter = false; + + if (indexStartD == countIndex - 2) + { + sum += tempValue; + } + else if (possibledoubleWord == true) + { + if (countIndex == 2) + { + possibledoubleWord = false; + sum *= 2; + } + } + else + { + return 0; + } + + indexStartD = 0; + countIndex++; + continue; + } + + if (letter == ']') + { + tripleLetter = false; + + if (indexStartT == countIndex - 2) + { + sum += tempValue * 2; + } + else if (possibletripleWord == true) + { + if (countIndex == 2) + { + possibletripleWord = false; + sum *= 3; + } + } + else + { + return 0; + } + + indexStartT = 0; + countIndex++; + continue; + } + + int value = scrabbleDictionary.LetterValue(letter); + if (value == 0) return 0; + sum += value; + + if ((doubleLetter || tripleLetter) == true) + { + tempValue = value; + } + + countIndex++; + } + + if (possibledoubleWord && (_word[_word.Length-1] == '}')) + { + sum *= 2; + } + + if (possibletripleWord && (_word[_word.Length - 1] == ']')) + { + sum *= 3; + } + + Console.WriteLine($"Sum: {sum}"); + + return sum; + } } } diff --git a/csharp-scrabble-challenge.Main/ScrabbleDictionary.cs b/csharp-scrabble-challenge.Main/ScrabbleDictionary.cs new file mode 100644 index 0000000..f6fffd9 --- /dev/null +++ b/csharp-scrabble-challenge.Main/ScrabbleDictionary.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace csharp_scrabble_challenge.Main +{ + public class ScrabbleDictionary + { + Dictionary scrabbledictionary; + public ScrabbleDictionary() + { + scrabbledictionary = new Dictionary(); + + scrabbledictionary.Add('A', 1); + scrabbledictionary.Add('B', 3); + scrabbledictionary.Add('C', 3); + scrabbledictionary.Add('D', 2); + scrabbledictionary.Add('E', 1); + scrabbledictionary.Add('F', 4); + scrabbledictionary.Add('G', 2); + scrabbledictionary.Add('H', 4); + scrabbledictionary.Add('I', 1); + scrabbledictionary.Add('J', 8); + scrabbledictionary.Add('K', 5); + scrabbledictionary.Add('L', 1); + scrabbledictionary.Add('M', 3); + scrabbledictionary.Add('N', 1); + scrabbledictionary.Add('O', 1); + scrabbledictionary.Add('P', 3); + scrabbledictionary.Add('Q', 10); + scrabbledictionary.Add('R', 1); + scrabbledictionary.Add('S', 1); + scrabbledictionary.Add('T', 1); + scrabbledictionary.Add('U', 1); + scrabbledictionary.Add('V', 4); + scrabbledictionary.Add('W', 4); + scrabbledictionary.Add('X', 8); + scrabbledictionary.Add('Y', 4); + scrabbledictionary.Add('Z', 10); + + /*foreach (KeyValuePair kvp in scrabbledictionary) + { + Console.WriteLine($"Key: {kvp.Key} Value: {kvp.Value}"); + }*/ + + } + + public int LetterValue(char letter) + { + if (letter >= 65 && letter <= 90) + { + int value = 0; + value = scrabbledictionary[letter]; + return value; + } + + return 0; + } + } +} diff --git a/csharp-scrabble-challenge.Test/CoreTests.cs b/csharp-scrabble-challenge.Test/CoreTests.cs index f42e402..f984983 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)] @@ -16,10 +20,28 @@ public class CoreTests [TestCase("quirky", 22)] [TestCase("street", 6)] public void WordScoreTests(string word, int targetScore) + { + Scrabble scrabble = new Scrabble(word); + + Assert.That(scrabble.score(), Is.EqualTo(targetScore)); + } + + // Old tests, which all passes. + + /*[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); } - private int GetWordScore(string word) => new Scrabble(word).score(); + private int GetWordScore(string word) => new Scrabble(word).score();*/ } } \ No newline at end of file